Skip to content

Commit

Permalink
Merge pull request #107 from ziotom78/empty_metadata
Browse files Browse the repository at this point in the history
Allow empty metadata in data files
  • Loading branch information
ziotom78 authored Aug 11, 2023
2 parents 696f712 + 3bf6e61 commit d15d9c6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# HEAD

- Allow empty metadata in data files when using the RESTful API [#107](https://github.com/ziotom78/instrumentdb/pull/107)

- Implement Swagger and Redoc web pages documenting the RESTful API [#106](https://github.com/ziotom78/instrumentdb/pull/106)

- Validate entity/quantity names in forms and RESTful API [#102](https://github.com/ziotom78/instrumentdb/pull/102), [#105](https://github.com/ziotom78/instrumentdb/pull/105)
Expand Down
25 changes: 25 additions & 0 deletions browse/migrations/0007_alter_datafile_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.4 on 2023-08-11 13:46

import browse.models
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("browse", "0006_alter_entity_name_alter_quantity_name"),
]

operations = [
migrations.AlterField(
model_name="datafile",
name="metadata",
field=models.TextField(
blank=True,
help_text="JSON record containing metadata for the file",
max_length=32768,
null=True,
validators=[browse.models.validate_json],
verbose_name="JSON-formatted metadata",
),
),
]
4 changes: 4 additions & 0 deletions browse/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@
def validate_json(value):
"""Check that `value` is a valid JSON record"""

if value is None or value == "":
return

try:
json.loads(value)
except json.JSONDecodeError as err:
Expand Down Expand Up @@ -364,6 +367,7 @@ class DataFile(models.Model):
"JSON-formatted metadata",
max_length=32768,
blank=True,
null=True,
help_text="JSON record containing metadata for the file",
validators=[validate_json],
)
Expand Down
2 changes: 1 addition & 1 deletion browse/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class DataFileSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name="datafile-detail", read_only=True
)
metadata = JSONField(validators=[serializer_validate_json])
metadata = JSONField(required=False, validators=[serializer_validate_json])

class Meta:
model = DataFile
Expand Down

0 comments on commit d15d9c6

Please sign in to comment.