Adds endpoint /api/image/<uuid> so that you can now view an image endpoint
authorJessica Tallon <xray7224@googlemail.com>
Wed, 14 Aug 2013 16:00:26 +0000 (17:00 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 22 Jul 2014 22:13:14 +0000 (23:13 +0100)
mediagoblin/federation/routing.py
mediagoblin/federation/views.py

index 9c3e0dffedef611bd7093ae5c24c6c441fc303af..9c870588770495c85ae631d870767b4bfdac4314 100644 (file)
@@ -41,3 +41,10 @@ add_route(
     "/api/user/<string:username>/inbox",
     "mediagoblin.federation.views:inbox"
     )
+
+# object endpoints
+add_route(
+    "mediagoblin.federation.object",
+    "/api/<string:objectType>/<string:uuid>",
+    "mediagoblin.federation.views:object"
+    )
index 337f28ed2fb020d6c3bd6d5290997a223992ab1f..de9084d098ae43ed18ec2c852ed3784fb9c0823d 100644 (file)
@@ -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/<username>/inbox """
-    pass
-
     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 """
+    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)