From: Jessica Tallon Date: Wed, 14 Aug 2013 16:00:26 +0000 (+0100) Subject: Adds endpoint /api/image/ so that you can now view an image endpoint X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=5a2056f7386371bd84b9481380235bb0f06ca149;p=mediagoblin.git Adds endpoint /api/image/ so that you can now view an image endpoint --- diff --git a/mediagoblin/federation/routing.py b/mediagoblin/federation/routing.py index 9c3e0dff..9c870588 100644 --- a/mediagoblin/federation/routing.py +++ b/mediagoblin/federation/routing.py @@ -41,3 +41,10 @@ add_route( "/api/user//inbox", "mediagoblin.federation.views:inbox" ) + +# object endpoints +add_route( + "mediagoblin.federation.object", + "/api//", + "mediagoblin.federation.views:object" + ) diff --git a/mediagoblin/federation/views.py b/mediagoblin/federation/views.py index 337f28ed..de9084d0 100644 --- a/mediagoblin/federation/views.py +++ b/mediagoblin/federation/views.py @@ -1,5 +1,5 @@ from mediagoblin.decorators import oauth_required -from mediagoblin.db.models import User +from mediagoblin.db.models import User, MediaEntry from mediagoblin.tools.response import json_response @oauth_required @@ -37,6 +37,42 @@ def feed(request): @oauth_required def inbox(request): """ Handles the user's inbox - /api/user//inbox """ - pass - 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 """ + objectType = request.matchdict["objectType"] + uuid = request.matchdict["uuid"] + if objectType not in ["image"]: + error = "Unknown type: {0}".format(objectType) + # not sure why this is 404, maybe ask evan. Maybe 400? + return json_response({"error": error}, status=404) + + media = MediaEntry.query.filter_by(uuid=uuid).first() + if media is None: + # no media found with that uuid + 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)