Refactors api uploading to media managers

This commit is contained in:
xray7224 2013-10-10 20:19:58 +01:00 committed by Jessica Tallon
parent 3c3fa5e7bf
commit 7810817caf
3 changed files with 51 additions and 43 deletions

View File

@ -467,8 +467,6 @@ class MediaEntry(Base, MediaEntryMixin):
if show_comments: if show_comments:
comments = [comment.serialize(request) for comment in self.get_comments()] comments = [comment.serialize(request) for comment in self.get_comments()]
total = len(comments) total = len(comments)
if total > 0:
# we only want to include replies if there are any.
context["replies"] = { context["replies"] = {
"totalItems": total, "totalItems": total,
"items": comments, "items": comments,

View File

@ -46,7 +46,7 @@ def user(request):
#@oauth_required #@oauth_required
@csrf_exempt @csrf_exempt
def uploads(request): 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"] user = request.matchdict["username"]
requested_user = User.query.filter_by(username=user) requested_user = User.query.filter_by(username=user)
@ -63,35 +63,17 @@ def uploads(request):
content_type=request.headers.get("Content-Type", "application/octal-stream") content_type=request.headers.get("Content-Type", "application/octal-stream")
) )
# Use the same kind of method from mediagoblin/submit/views:submit_start # Find media manager
media_type, media_manager = sniff_media(file_data) media_type, media_manager = sniff_media(file_data)
entry = new_upload_entry(request.user) entry = new_upload_entry(request.user)
entry.media_type = unicode(media_type) if hasattr(media_manager, "api_upload_request"):
entry.title = unicode(request.args.get("title", "Hello ^_^")) return media_manager.api_upload_request(request, file_data, entry)
entry.description = unicode(request.args.get("description", "")) else:
entry.license = None return json_response({"error": "Not yet implemented"}, status=400)
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))
return json_response({"error": "Not yet implemented"}, status=400) return json_response({"error": "Not yet implemented"}, status=400)
@oauth_required #@oauth_required
@csrf_exempt @csrf_exempt
def feed(request): def feed(request):
""" Handles the user's outbox - /api/user/<username>/feed """ """ Handles the user's outbox - /api/user/<username>/feed """
@ -124,6 +106,7 @@ def feed(request):
comment.save() comment.save()
data = {"verb": "post", "object": comment.serialize(request)} data = {"verb": "post", "object": comment.serialize(request)}
return json_response(data) return json_response(data)
elif obj.get("objectType", None) == "image": elif obj.get("objectType", None) == "image":
# Posting an image to the feed # Posting an image to the feed
# NB: This is currently just handing the image back until we have an # 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))} error = {"error": "Unknown object type '{0}'.".format(obj.get("objectType", None))}
return json_response(error, status=400) return json_response(error, status=400)
feed_url = request.urlgen( feed_url = request.urlgen(
"mediagoblin.federation.feed", "mediagoblin.federation.feed",
username=request.user.username, username=request.user.username,

View File

@ -19,7 +19,9 @@ import logging
from mediagoblin.media_types import MediaManagerBase from mediagoblin.media_types import MediaManagerBase
from mediagoblin.media_types.image.processing import sniff_handler, \ from mediagoblin.media_types.image.processing import sniff_handler, \
ImageProcessingManager 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__) _log = logging.getLogger(__name__)
@ -56,6 +58,30 @@ class ImageMediaManager(MediaManagerBase):
except (KeyError, ValueError): except (KeyError, ValueError):
return None 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): def get_media_type_and_manager(ext):
if ext in ACCEPTED_EXTENSIONS: if ext in ACCEPTED_EXTENSIONS: