From: Jessica Tallon Date: Wed, 14 Aug 2013 16:51:36 +0000 (+0100) Subject: Changes serialization to .serialize method on object - MediaEntry X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=bdde87a4b3a584a2dde5803b1a069496aee73daf;p=mediagoblin.git Changes serialization to .serialize method on object - MediaEntry --- diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index e1b37aa0..404aaa94 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -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. diff --git a/mediagoblin/federation/views.py b/mediagoblin/federation/views.py index de9084d0..3fe5b3b5 100644 --- a/mediagoblin/federation/views.py +++ b/mediagoblin/federation/views.py @@ -39,25 +39,6 @@ def inbox(request): """ Handles the user's inbox - /api/user//inbox """ raise NotImplemented("Yet to implement looking up user's inbox") -def image_object(request, media): - """ Return image object - /api/image/ """ - 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))