Changes serialization to .serialize method on object - MediaEntry
authorJessica Tallon <xray7224@googlemail.com>
Wed, 14 Aug 2013 16:51:36 +0000 (17:51 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 22 Jul 2014 22:13:14 +0000 (23:13 +0100)
mediagoblin/db/models.py
mediagoblin/federation/views.py

index e1b37aa000700ca86271a047924e20c2ff1cd1c5..404aaa94cd60e39f6e8ee4ae24ea4d86be78a5b3 100644 (file)
@@ -427,6 +427,37 @@ class MediaEntry(Base, MediaEntryMixin):
         # pass through commit=False/True in kwargs
         super(MediaEntry, self).delete(**kwargs)
 
+    @property
+    def objectType(self):
+        """ Converts media_type to pump-like type - don't use internally """
+        return self.media_type.split(".")[-1]
+
+    def serialize(self, request):
+        """ Unserialize MediaEntry to object """
+        author = self.get_uploader
+        url = request.urlgen(
+            "mediagoblin.user_pages.media_home",
+            user=author.username,
+            media=self.slug,
+            qualified=True
+            )
+
+        id = request.urlgen(
+            "mediagoblin.federation.object",
+            objectType=self.objectType,
+            uuid=self.uuid,
+            qualified=True
+            )
+
+        context = {
+            "id": id, 
+            "author": author.serialize(request),
+            "displayName": self.title,
+            "objectType": self.objectType,
+            "url": url,
+        }
+        return context 
+
 class FileKeynames(Base):
     """
     keywords for various places.
@@ -573,6 +604,18 @@ class MediaComment(Base, MediaCommentMixin):
                                                    cascade="all, delete-orphan"))
 
 
+    def serialize(self, request):
+        """ Unserialize to python dictionary for API """
+        media = MediaEntry.query.filter_by(self.media_entry).first()
+        context = {
+            "objectType": "comment",
+            "content": self.content,
+            "inReplyTo": media.unserialize(request),
+            "author": self.get_author.unserialize(request)
+        }
+
+        return context
+
 class Collection(Base, CollectionMixin):
     """An 'album' or 'set' of media by a user.
 
index de9084d098ae43ed18ec2c852ed3784fb9c0823d..3fe5b3b5899297868c0d52cf18091b39640073de 100644 (file)
@@ -39,25 +39,6 @@ def inbox(request):
     """ Handles the user's inbox - /api/user/<username>/inbox """
     raise NotImplemented("Yet to implement looking up user's inbox")
 
-def image_object(request, media):
-    """ Return image object - /api/image/<uuid> """
-    author = media.get_uploader
-    url = request.urlgen(
-        "mediagoblin.user_pages.media_home",
-        user=author.username,
-        media=media.slug,
-        qualified=True
-        )
-
-    context = {
-        "author": author.serialize(request),
-        "displayName": media.title,
-        "objectType": "image",
-        "url": url,
-    }
-
-    return json_response(context)
-
 @oauth_required
 def object(request):
     """ Lookup for a object type """
@@ -74,5 +55,4 @@ def object(request):
         error = "Can't find a {0} with ID = {1}".format(objectType, uuid)
         return json_response({"error": error}, status=404)
 
-    if objectType == "image":
-        return image_object(request, media)
+    return json_response(media.serialize(request))