Skip to content

Commit

Permalink
Merge pull request #158 from lsst-ts/tickets/LOVE-108
Browse files Browse the repository at this point in the history
Add JSON file validation to ConfigFile admin form
  • Loading branch information
sebastian-aranda authored Sep 14, 2022
2 parents 4a3758a + 039ab44 commit fd62bcc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
37 changes: 37 additions & 0 deletions manager/api/migrations/0010_auto_20220914_1542.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 3.1.14 on 2022-09-14 15:42

import api.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("api", "0009_configfile_selected_by_users"),
]

operations = [
migrations.AlterField(
model_name="configfile",
name="config_file",
field=models.FileField(
default="configs/default.json",
upload_to="configs/",
validators=[
api.models.ConfigFile.validate_file_extension,
api.models.ConfigFile.validate_json_file,
],
),
),
migrations.AlterField(
model_name="configfile",
name="selected_by_users",
field=models.ManyToManyField(
blank=True,
related_name="selected_config_file",
to=settings.AUTH_USER_MODEL,
),
),
]
13 changes: 10 additions & 3 deletions manager/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
https://docs.djangoproject.com/en/2.2/topics/db/models/
"""
import os
import json
from django.conf import settings
from django.db import models
from django.db.models import F
Expand Down Expand Up @@ -82,10 +83,16 @@ class ConfigFile(BaseModel):

def validate_file_extension(value):
ext = os.path.splitext(value.name)[1] # [0] returns path+filename
valid_extensions = [".json", ".sh"]
valid_extensions = [".json"]
if not ext.lower() in valid_extensions:
raise ValidationError("Unsupported file extension.")

def validate_json_file(value):
try:
json.loads(value.read().decode("ascii"))
except Exception:
raise ValidationError("Malformatted JSON object.")

user = models.ForeignKey(
settings.AUTH_USER_MODEL,
related_name="config_files",
Expand All @@ -100,12 +107,12 @@ def validate_file_extension(value):
config_file = models.FileField(
upload_to="configs/",
default="configs/default.json",
validators=[validate_file_extension],
validators=[validate_file_extension, validate_json_file],
)
"""Reference to the config file"""

selected_by_users = models.ManyToManyField(
settings.AUTH_USER_MODEL, related_name="selected_config_file"
settings.AUTH_USER_MODEL, related_name="selected_config_file", blank=True,
)


Expand Down
2 changes: 1 addition & 1 deletion manager/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TimeDataSerializer(serializers.Serializer):


class ConfigSerializer(serializers.Serializer):
"""Custom Serializer to describe the confi file field for the Apidocs."""
"""Custom Serializer to describe the config file field for the Apidocs."""

config_file = serializers.JSONField()

Expand Down

0 comments on commit fd62bcc

Please sign in to comment.