Fix #927 - Clean up federation code after Elrond's review
- Add json_error and use inplace of json_response where appropriate. - Add garbage_collection to config spec file. - Fix bugs in both garbage collection task and test - Handle /api/whoami when no user logged in and a test for such a case. - Validate ID is correct and user has comment privilege to comment.
This commit is contained in:
@@ -22,6 +22,7 @@ from werkzeug.utils import secure_filename
|
||||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
from mediagoblin import mg_globals
|
||||
from mediagoblin.tools.response import json_response
|
||||
from mediagoblin.tools.text import convert_to_tag_list_of_dicts
|
||||
from mediagoblin.db.models import MediaEntry, ProcessingMetaData
|
||||
from mediagoblin.processing import mark_entry_failed
|
||||
@@ -259,3 +260,33 @@ def run_process_media(entry, feed_url=None,
|
||||
mark_entry_failed(entry.id, exc)
|
||||
# re-raise the exception
|
||||
raise
|
||||
|
||||
|
||||
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.title = file_data.filename
|
||||
entry.generate_slug()
|
||||
|
||||
queue_file = prepare_queue_task(request.app, entry, file_data.filename)
|
||||
with queue_file:
|
||||
queue_file.write(request.data)
|
||||
|
||||
entry.save()
|
||||
return json_response(entry.serialize(request))
|
||||
|
||||
def api_add_to_feed(request, entry):
|
||||
""" Add media to Feed """
|
||||
if entry.title:
|
||||
# Shame we have to do this here but we didn't have the data in
|
||||
# api_upload_request as no filename is usually specified.
|
||||
entry.slug = None
|
||||
entry.generate_slug()
|
||||
|
||||
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))
|
||||
38
mediagoblin/submit/task.py
Executable file
38
mediagoblin/submit/task.py
Executable file
@@ -0,0 +1,38 @@
|
||||
# GNU MediaGoblin -- federated, autonomous media hosting
|
||||
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import celery
|
||||
import datetime
|
||||
import pytz
|
||||
|
||||
from mediagoblin.db.models import MediaEntry
|
||||
|
||||
@celery.task()
|
||||
def collect_garbage():
|
||||
"""
|
||||
Garbage collection to clean up media
|
||||
|
||||
This will look for all critera on models to clean
|
||||
up. This is primerally written to clean up media that's
|
||||
entered a erroneous state.
|
||||
"""
|
||||
cuttoff = datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=1)
|
||||
|
||||
garbage = MediaEntry.query.filter(MediaEntry.created < cuttoff)
|
||||
garbage = garbage.filter(MediaEntry.state == "unprocessed")
|
||||
|
||||
for entry in garbage.all():
|
||||
entry.delete()
|
||||
Reference in New Issue
Block a user