Refactors api uploading to media managers
authorxray7224 <xray7224@googlemail.com>
Thu, 10 Oct 2013 19:19:58 +0000 (20:19 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 22 Jul 2014 22:13:15 +0000 (23:13 +0100)
mediagoblin/db/models.py
mediagoblin/federation/views.py
mediagoblin/media_types/image/__init__.py

index 215e7552f242173302224c82d34026caba8699b3..cc5d0afa673fb87304bf4b8273dd222eb4c9f28a 100644 (file)
@@ -467,18 +467,16 @@ class MediaEntry(Base, MediaEntryMixin):
         if show_comments:
             comments = [comment.serialize(request) for comment in self.get_comments()]
             total = len(comments)
-            if total > 0:
-                # we only want to include replies if there are any.
-                context["replies"] = {
-                    "totalItems": total,
-                    "items": comments,
-                    "url": request.urlgen(
-                            "mediagoblin.federation.object.comments",
-                            objectType=self.objectType,
-                            uuid=self.slug,
-                            qualified=True
-                            ),
-                }
+            context["replies"] = {
+                "totalItems": total,
+                "items": comments,
+                "url": request.urlgen(
+                        "mediagoblin.federation.object.comments",
+                        objectType=self.objectType,
+                        uuid=self.slug,
+                        qualified=True
+                        ),
+            }
 
         return context 
 
index 4add17d906c1bf9217344fe26723d0068a83a536..bdc93d9b48856d18667207a79b7d332466cb997f 100644 (file)
@@ -46,7 +46,7 @@ def user(request):
 #@oauth_required
 @csrf_exempt
 def uploads(request):
-    """ This is the endpoint which uploads can be sent ot - /api/user/<username>/uploads """
+    """ This is the endpoint which uploads can be sent to - /api/user/<username>/uploads """
     user = request.matchdict["username"]
     requested_user = User.query.filter_by(username=user)
 
@@ -58,40 +58,22 @@ def uploads(request):
     if request.method == "POST":
         # Wrap the data in the werkzeug file wrapper
         file_data = FileStorage(
-                stream=io.BytesIO(request.data),
-                filename=request.args.get("qqfile", "unknown.jpg"),
-                content_type=request.headers.get("Content-Type", "application/octal-stream")
-                )
-        
-        # Use the same kind of method from mediagoblin/submit/views:submit_start
+            stream=io.BytesIO(request.data),
+            filename=request.args.get("qqfile", "unknown.jpg"),
+            content_type=request.headers.get("Content-Type", "application/octal-stream")
+        )
+
+        # Find media manager
         media_type, media_manager = sniff_media(file_data)
         entry = new_upload_entry(request.user)
-        entry.media_type = unicode(media_type)
-        entry.title = unicode(request.args.get("title", "Hello ^_^"))
-        entry.description = unicode(request.args.get("description", ""))
-        entry.license = None
-
-        entry.generate_slug()
-
-        queue_file = prepare_queue_task(request.app, entry, file_data.filename) 
-        with queue_file:
-            queue_file.write(request.data)
-
-        entry.save()
-
-        # run the processing
-        feed_url = request.urlgen(
-                'mediagoblin.user_pages.atom_feed',
-                qualified=True, user=request.user.username)
-
-        run_process_media(entry, feed_url)
-        add_comment_subscription(request.user, entry)
-
-        return json_response(entry.serialize(request))
+        if hasattr(media_manager, "api_upload_request"):
+            return media_manager.api_upload_request(request, file_data, entry)
+        else:
+            return json_response({"error": "Not yet implemented"}, status=400)
 
     return json_response({"error": "Not yet implemented"}, status=400)
 
-@oauth_required
+#@oauth_required
 @csrf_exempt
 def feed(request):
     """ Handles the user's outbox - /api/user/<username>/feed """
@@ -124,6 +106,7 @@ def feed(request):
             comment.save()
             data = {"verb": "post", "object": comment.serialize(request)}
             return json_response(data)
+
         elif obj.get("objectType", None) == "image":
             # Posting an image to the feed
             # NB: This is currently just handing the image back until we have an
@@ -146,6 +129,7 @@ def feed(request):
             error = {"error": "Unknown object type '{0}'.".format(obj.get("objectType", None))}
             return json_response(error, status=400)
 
+
     feed_url = request.urlgen(
             "mediagoblin.federation.feed",
             username=request.user.username,
index 06e0f08f888dd0bfab895a8101cae0bb001b4557..0a77b0cebb350bf96d02e511f9fe2ed83f239209 100644 (file)
@@ -19,7 +19,9 @@ import logging
 from mediagoblin.media_types import MediaManagerBase
 from mediagoblin.media_types.image.processing import sniff_handler, \
         ImageProcessingManager
-
+from mediagoblin.tools.response import json_response
+from mediagoblin.submit.lib import prepare_queue_task, run_process_media
+from mediagoblin.notifications import add_comment_subscription
 
 _log = logging.getLogger(__name__)
 
@@ -56,6 +58,30 @@ class ImageMediaManager(MediaManagerBase):
         except (KeyError, ValueError):
             return None
 
+    @staticmethod
+    def api_upload_request(request, file_data, entry):
+        """ This handles a image upload request """
+        # Use the same kind of method from mediagoblin/submit/views:submit_start
+        entry.media_type = unicode(MEDIA_TYPE)
+        entry.title = unicode(request.args.get("title", file_data.filename))
+        entry.description = unicode(request.args.get("description", ""))
+        entry.license = request.args.get("license", "") # not part of the standard API
+
+        entry.generate_slug()
+
+        queue_file = prepare_queue_task(request.app, entry, file_data.filename)
+        with queue_file:
+            queue_file.write(request.data)
+        
+        entry.save()
+
+        feed_url = request.urlgen(
+            'mediagoblin.user_pages.atom_feed',
+            qualified=True, user=request.user.username)
+
+        run_process_media(entry, feed_url)
+        add_comment_subscription(request.user, entry)
+        return json_response(entry.serialize(request))
 
 def get_media_type_and_manager(ext):
     if ext in ACCEPTED_EXTENSIONS: