From 5a2056f7386371bd84b9481380235bb0f06ca149 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Wed, 14 Aug 2013 17:00:26 +0100 Subject: [PATCH] Adds endpoint /api/image/ so that you can now view an image endpoint --- mediagoblin/federation/routing.py | 7 ++++++ mediagoblin/federation/views.py | 42 ++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) 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) -- 2.25.1