Switch test_app generation over to use py.test fixtures.
[mediagoblin.git] / mediagoblin / submit / lib.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import logging
18 import uuid
19 from werkzeug.utils import secure_filename
20 from werkzeug.datastructures import FileStorage
21
22 from mediagoblin.processing import mark_entry_failed
23 from mediagoblin.processing.task import process_media
24
25
26 _log = logging.getLogger(__name__)
27
28
29 def check_file_field(request, field_name):
30 """Check if a file field meets minimal criteria"""
31 retval = (field_name in request.files
32 and isinstance(request.files[field_name], FileStorage)
33 and request.files[field_name].stream)
34 if not retval:
35 _log.debug("Form did not contain proper file field %s", field_name)
36 return retval
37
38
39 def prepare_queue_task(app, entry, filename):
40 """
41 Prepare a MediaEntry for the processing queue and get a queue file
42 """
43 # We generate this ourselves so we know what the taks id is for
44 # retrieval later.
45
46 # (If we got it off the task's auto-generation, there'd be
47 # a risk of a race condition when we'd save after sending
48 # off the task)
49 task_id = unicode(uuid.uuid4())
50 entry.queued_task_id = task_id
51
52 # Now store generate the queueing related filename
53 queue_filepath = app.queue_store.get_unique_filepath(
54 ['media_entries',
55 task_id,
56 secure_filename(filename)])
57
58 # queue appropriately
59 queue_file = app.queue_store.get_file(
60 queue_filepath, 'wb')
61
62 # Add queued filename to the entry
63 entry.queued_media_file = queue_filepath
64
65 return queue_file
66
67
68 def run_process_media(entry, feed_url=None):
69 """Process the media asynchronously
70
71 :param entry: MediaEntry() instance to be processed.
72 :param feed_url: A string indicating the feed_url that the PuSH servers
73 should be notified of. This will be sth like: `request.urlgen(
74 'mediagoblin.user_pages.atom_feed',qualified=True,
75 user=request.user.username)`"""
76 try:
77 process_media.apply_async(
78 [entry.id, feed_url], {},
79 task_id=entry.queued_task_id)
80 except BaseException as exc:
81 # The purpose of this section is because when running in "lazy"
82 # or always-eager-with-exceptions-propagated celery mode that
83 # the failure handling won't happen on Celery end. Since we
84 # expect a lot of users to run things in this way we have to
85 # capture stuff here.
86 #
87 # ... not completely the diaper pattern because the
88 # exception is re-raised :)
89 mark_entry_failed(entry.id, exc)
90 # re-raise the exception
91 raise