--- a/server/python/django2/renkanmanager/models.py Wed Apr 27 16:36:30 2016 +0200
+++ b/server/python/django2/renkanmanager/models.py Tue May 03 14:32:28 2016 +0200
@@ -60,10 +60,7 @@
initial_revision.modification_date = initial_revision.creation_date
initial_revision.title = new_renkan_title if new_renkan_title else "Untitled Renkan"
if new_renkan_content:
- try:
- new_renkan_content_dict = json.loads(new_renkan_content)
- except ValueError:
- raise ValidationError("Provided content to create Renkan is not a JSON-serializable")
+ new_renkan_content_dict = json.loads(new_renkan.validate_json_content(new_renkan_content))
new_renkan_content_dict["created"] = str(initial_revision.creation_date)
new_renkan_content_dict["updated"] = str(initial_revision.modification_date)
else:
@@ -82,8 +79,7 @@
initial_revision.content = json.dumps(new_renkan_content_dict)
initial_revision.save()
return new_renkan
-
-
+
class Renkan(models.Model):
renkan_guid = models.CharField(max_length=256, default=uuid.uuid4, unique=True, blank=False, null=False) # typically UUID
@@ -142,11 +138,8 @@
else:
revision_to_update = Revision.objects.select_for_update().get(revision_guid=self.current_revision.revision_guid)
- updated_content = content if content else current_revision.content
- try:
- updated_content_dict = json.loads(updated_content)
- except ValueError:
- raise ValidationError(message="Provided content for Renkan is not a JSON-serializable")
+ updated_content = self.validate_json_content(content) if content else current_revision.content
+ updated_content_dict = json.loads(updated_content)
# If title is passed as arg to the method, update the title in the json
if title:
@@ -166,6 +159,49 @@
revision_to_update.last_updated_by = updator
revision_to_update.save()
+ def validate_json_content(self, content):
+ """
+ Checks that the json content is valid (keys and structures), raise a ValidationError if format is wrong or value is wrong (for ids),
+ if a key is missing, autocompletes with the empty default value
+
+ Returns the validated json string
+ """
+ try:
+ content_to_validate_dict = json.loads(content)
+ except ValueError:
+ raise ValidationError("Provided content to create Renkan is not a JSON-serializable")
+ if "id" not in content_to_validate_dict or content_to_validate_dict["id"] == "" :
+ content_to_validate_dict["id"] = str(self.renkan_guid)
+ if "title" not in content_to_validate_dict:
+ content_to_validate_dict["title"] = ""
+ if "description" not in content_to_validate_dict:
+ content_to_validate_dict["description"] = ""
+ if "created" not in content_to_validate_dict:
+ content_to_validate_dict["description"] = ""
+ if "updated" not in content_to_validate_dict:
+ content_to_validate_dict["description"] = ""
+ expected_workspace_id = str(self.workspace.workspace_guid) if self.workspace is not None else ""
+ if "space_id" not in content_to_validate_dict:
+ content_to_validate_dict["space_id"] = expected_workspace_id
+ if "nodes" not in content_to_validate_dict:
+ content_to_validate_dict["nodes"] = []
+ if "edges" not in content_to_validate_dict:
+ content_to_validate_dict["edges"] = []
+ if "views" not in content_to_validate_dict:
+ content_to_validate_dict["views"] = []
+ if "users" not in content_to_validate_dict:
+ content_to_validate_dict["users"] = []
+
+ if type(content_to_validate_dict["nodes"]) is not list:
+ raise ValidationError("Provided content has an invalid 'nodes' key: not a list")
+ if type(content_to_validate_dict["edges"]) is not list:
+ raise ValidationError("Provided content has an invalid 'edges' key: not a list")
+ if type(content_to_validate_dict["views"]) is not list:
+ raise ValidationError("Provided content has an invalid 'views' key: not a list")
+ if type(content_to_validate_dict["users"]) is not list:
+ raise ValidationError("Provided content has an invalid 'users' key: not a list")
+ return json.dumps(content_to_validate_dict)
+
@transaction.atomic
def delete(self):
"""