Add unseralize for API objects
authorJessica Tallon <jessica@megworld.co.uk>
Wed, 16 Jul 2014 16:59:03 +0000 (17:59 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 22 Jul 2014 22:13:16 +0000 (23:13 +0100)
lazystarter.sh
mediagoblin/db/models.py
mediagoblin/federation/views.py
mediagoblin/init/celery/__init__.py

index d3770194cd33059eab1e28e950fd3d53dfd3c30b..41994015da8934c145f8955133e374f629e035a0 100755 (executable)
@@ -76,7 +76,7 @@ case "$selfname" in
     lazycelery.sh)
         MEDIAGOBLIN_CONFIG="${ini_file}" \
             CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_celery \
-            $starter "$@"
+            $starter -B "$@"
         ;;
     *) exit 1 ;;
 esac
index 0507161e88c24081e7263a10f30daef144204ced..8ea16b8081745fced2822365f5c7b8ba4aad2a50 100644 (file)
@@ -499,6 +499,19 @@ class MediaEntry(Base, MediaEntryMixin):
 
         return context
 
+    def unserialize(self, data):
+        """ Takes API objects and unserializes on existing MediaEntry """
+        if "displayName" in data:
+            self.title = data["displayName"]
+
+        if "content" in data:
+            self.description = data["content"]
+
+        if "license" in data:
+            self.license = data["license"]
+
+        return True
+
 class FileKeynames(Base):
     """
     keywords for various places.
@@ -658,6 +671,24 @@ class MediaComment(Base, MediaCommentMixin):
 
         return context
 
+    def unserialize(self, data):
+        """ Takes API objects and unserializes on existing comment """
+        # Do initial checks to verify the object is correct
+        required_attributes = ["content", "inReplyTo"]
+        for attr in required_attributes:
+            if attr not in data:
+                return False
+
+        # Validate inReplyTo has ID
+        if "id" not in data["inReplyTo"]:
+            return False
+
+        self.media_entry = data["inReplyTo"]["id"]
+        self.content = data["content"]
+        return True
+
+
+
 class Collection(Base, CollectionMixin):
     """An 'album' or 'set' of media by a user.
 
index c383b3ef5e2275e671564de8c112e712ae145362..8db04f3aad4aa2d9eeceba96b2131af48549917b 100644 (file)
@@ -55,8 +55,8 @@ def user(request):
         "nickname": user.username,
         "updated": user.created.isoformat(),
         "published": user.created.isoformat(),
-        "profile": user_profile
-        }
+        "profile": user_profile,
+    }
 
     return json_response(data)
 
@@ -120,12 +120,8 @@ def feed(request):
 
         if obj.get("objectType", None) == "comment":
             # post a comment
-            media = int(data["object"]["inReplyTo"]["id"])
-            comment = MediaComment(
-                media_entry=media,
-                author=request.user.id,
-                content=data["object"]["content"]
-                )
+            comment = MediaComment(author=request.user.id)
+            comment.unserialize(data["object"])
             comment.save()
             data = {"verb": "post", "object": comment.serialize(request)}
             return json_response(data)
@@ -139,17 +135,9 @@ def feed(request):
                 return json_response(error, status=404)
 
             media = media.first()
-            obj = data["object"]
-
-            if "displayName" in obj:
-                media.title = obj["displayName"]
-
-            if "content" in obj:
-                media.description = obj["content"]
-
-            if "license" in obj:
-                media.license = obj["license"]
-
+            if not media.unserialize(data["object"]):
+                error = {"error": "Invalid 'image' with id '{0}'".format(obj_id)}
+                return json_response(error, status=400)
             media.save()
             media.media_manager.api_add_to_feed(request, media)
 
@@ -195,13 +183,14 @@ def feed(request):
             if comment is None:
                 error = {"error": "No such 'comment' with id '{0}'.".format(obj_id)}
                 return json_response(error, status=400)
-            comment = comment[0]
 
-            # TODO: refactor this out to update/setting method on MediaComment
-            if obj.get("content", None) is not None:
-                comment.content = obj["content"]
+            comment = comment[0]
+            if not comment.unserialize(data["object"]):
+                error = {"error": "Invalid 'comment' with id '{0}'".format(obj_id)}
+                return json_response(error, status=400)
 
             comment.save()
+
             activity = {
                 "verb": "update",
                 "object": comment.serialize(request),
@@ -215,19 +204,11 @@ def feed(request):
                 return json_response(error, status=400)
 
             image = image[0]
-
-            # TODO: refactor this out to update/setting method on MediaEntry
-            if obj.get("displayName", None) is not None:
-                image.title = obj["displayName"]
-
-            if obj.get("content", None) is not None:
-                image.description = obj["content"]
-
-            if obj.get("license", None) is not None:
-                # I think we might need some validation here
-                image.license = obj["license"]
-
+            if not image.unserialize(obj):
+                error = {"error": "Invalid 'image' with id '{0}'".format(obj_id)}
+                return json_response(error, status=400)
             image.save()
+
             activity = {
                 "verb": "update",
                 "object": image.serialize(request),
index 214d00c3c961456a9b7dad35c500c1f4090ce2bc..2f2c40d31eb273d4b757317c15ece7b2f458dbf8 100644 (file)
@@ -62,7 +62,7 @@ def get_celery_settings_dict(app_config, global_config,
     # Garbage collection periodic task
     frequency = app_config.get('garbage_collection', 60)
     if frequency:
-        frequency = int(app_config['garbage_collection'])
+        frequency = int(frequency)
         celery_settings['CELERYBEAT_SCHEDULE'] = {
             'garbage-collection': {
                 'task': 'mediagoblin.federation.task.garbage_collection',