--- a/server/python/django2/renkanmanager/serializers.py Tue Jun 07 10:46:20 2016 +0200
+++ b/server/python/django2/renkanmanager/serializers.py Mon Jun 13 14:23:58 2016 +0200
@@ -25,15 +25,19 @@
modification_date = serializers.SerializerMethodField("get_current_revision_modification_date")
create_new_revision = serializers.BooleanField(write_only=True, required=False) # only used for updating
validation_timestamp = serializers.CharField(write_only=True, required=False) # only used for updating is a content json is not provided
-
+
# ADD ERROR HANDLING
def get_current_revision_last_updator(self, renkan):
- return getattr(renkan.current_revision.last_updated_by, RENKAN_USER_DISPLAY_FIELD)
-
-
+ last_updator = renkan.current_revision.last_updated_by
+ if last_updator:
+ return getattr(last_updator, RENKAN_USER_DISPLAY_FIELD)
+ else:
+ return ''
+
+
def get_current_revision_modification_date(self, renkan):
return renkan.current_revision.modification_date
-
+
@transaction.atomic
def create(self, validated_data):
"""
@@ -52,7 +56,7 @@
)
except ValidationError as ve:
raise serializers.ValidationError(ve.args[0])
-
+
@transaction.atomic
def update(self, renkan, validated_data):
"""
@@ -80,18 +84,18 @@
except ValidationError as ve:
raise serializers.ValidationError(str(ve.args[0]))
return renkan
-
+
def validate_workspace_id(self, value):
if self.instance is not None:
raise serializers.ValidationError("You cannot update workspace_guid")
return value
-
+
def validate_source_revision_id(self, value):
if self.instance is not None:
raise serializers.ValidationError("You cannot update source_revision_guid")
return value
-
- def validate_content(self, value):
+
+ def validate_content(self, value):
try:
loaded_json = json.loads(value)
except ValueError:
@@ -103,9 +107,10 @@
if (not "views" in loaded_json):
raise serializers.ValidationError("Content requires a 'views' entry")
return value
-
+
def validate_validation_timestamp(self, value):
- logger.debug("%r", value)
+ logger.debug("%r < %r", dateparse.parse_datetime(value), self.get_current_revision_modification_date(self.instance))
+
if self.instance and dateparse.parse_datetime(value) < self.get_current_revision_modification_date(self.instance):
raise serializers.ValidationError("Invalid timestamp was provided")
return value
@@ -121,19 +126,19 @@
revision_created_by = serializers.SlugRelatedField(source="creator", slug_field=RENKAN_USER_DISPLAY_FIELD, read_only=True)
revision_last_updated_by = serializers.SlugRelatedField(source="last_updated_by", slug_field=RENKAN_USER_DISPLAY_FIELD, read_only=True)
revision_modification_date = serializers.ReadOnlyField(source="modification_date")
-
+
def get_related_workspace_guid(self, revision):
if revision.parent_renkan.workspace:
return revision.parent_renkan.workspace.workspace_guid
else:
return ''
-
+
def get_related_renkan_creator(self, revision):
return getattr(revision.parent_renkan.creator, RENKAN_USER_DISPLAY_FIELD)
-
+
def get_related_renkan_creation_date(self, revision):
return revision.parent_renkan.creation_date
-
+
class WorkspaceSerializer(serializers.Serializer):
id = serializers.ReadOnlyField(source="workspace_guid")
@@ -141,15 +146,15 @@
creation_date = serializers.ReadOnlyField()
renkan_count = serializers.ReadOnlyField()
title = serializers.CharField()
-
+
def create(self, validated_data):
creator = validated_data.get('creator')
title = validated_data.get('title', '')
workspace = Workspace.objects.create(creator=creator, title=title)
workspace.save()
return workspace
-
+
def update(self, workspace, validated_data):
workspace.title = validated_data.get('title', '')
workspace.save()
- return workspace
\ No newline at end of file
+ return workspace