Merge jQuery branch, resolve conflicts
[mediagoblin.git] / mediagoblin / submit / views.py
CommitLineData
e323a068 1# GNU MediaGoblin -- federated, autonomous media hosting
12a100e4 2# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
e323a068
CAW
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
2c437493 17import mediagoblin.mg_globals as mg_globals
4a477e24 18import uuid
bb49e56f 19from os.path import splitext
03afc828
CAW
20from cgi import FieldStorage
21
8e5f9746
JW
22from celery import registry
23
f6f524bf 24from werkzeug.utils import secure_filename
e323a068 25
f64e5250 26from mediagoblin.db.util import ObjectId
152a3bfa 27from mediagoblin.tools.text import cleaned_markdown_conversion, convert_to_tag_list_of_dicts
ae3bc7fa 28from mediagoblin.tools.translate import pass_to_ugettext as _
152a3bfa 29from mediagoblin.tools.response import render_to_response, redirect
e323a068 30from mediagoblin.decorators import require_active_login
3eeadc92 31from mediagoblin.submit import forms as submit_forms, security
8e5f9746 32from mediagoblin.processing import mark_entry_failed, ProcessMedia
4dc74441 33from mediagoblin.messages import add_message, SUCCESS
0bce749b 34from mediagoblin.media_types import get_media_type_and_manager, InvalidFileType
e323a068
CAW
35
36
37@require_active_login
38def submit_start(request):
39 """
40 First view for submitting a file.
41 """
20439236 42 submit_form = submit_forms.SubmitStartForm(request.POST)
e323a068 43
f6f524bf 44 if request.method == 'POST' and submit_form.validate():
285ffedd 45 if not ('file' in request.POST
03afc828
CAW
46 and isinstance(request.POST['file'], FieldStorage)
47 and request.POST['file'].file):
48 submit_form.file.errors.append(
4b1adc13 49 _(u'You must provide a file.'))
03afc828 50 else:
0bce749b
JW
51 try:
52 filename = request.POST['file'].filename
53 media_type, media_manager = get_media_type_and_manager(filename)
bb49e56f 54
0bce749b
JW
55 # create entry and save in database
56 entry = request.db.MediaEntry()
57 entry['_id'] = ObjectId()
f4ee8399 58 entry.media_type = unicode(media_type)
ec82fbd8 59 entry.title = (
0bce749b
JW
60 unicode(request.POST['title'])
61 or unicode(splitext(filename)[0]))
93bdab9d 62
1d939966
E
63 entry.description = unicode(request.POST.get('description'))
64 entry.description_html = cleaned_markdown_conversion(
65 entry.description)
4bf8e888 66
1ceb4fc8 67 entry.uploader = request.user._id
03afc828 68
0bce749b
JW
69 # Process the user's folksonomy "tags"
70 entry['tags'] = convert_to_tag_list_of_dicts(
71 request.POST.get('tags'))
03afc828 72
0bce749b
JW
73 # Generate a slug from the title
74 entry.generate_slug()
0546833c 75
93bdab9d 76
0bce749b
JW
77 # Now store generate the queueing related filename
78 queue_filepath = request.app.queue_store.get_unique_filepath(
79 ['media_entries',
80 unicode(entry._id),
81 secure_filename(filename)])
03afc828 82
0bce749b
JW
83 # queue appropriately
84 queue_file = request.app.queue_store.get_file(
85 queue_filepath, 'wb')
03afc828 86
0bce749b
JW
87 with queue_file:
88 queue_file.write(request.POST['file'].file.read())
03afc828 89
0bce749b
JW
90 # Add queued filename to the entry
91 entry['queued_media_file'] = queue_filepath
03afc828 92
0bce749b
JW
93 # We generate this ourselves so we know what the taks id is for
94 # retrieval later.
243c3843 95
0bce749b
JW
96 # (If we got it off the task's auto-generation, there'd be
97 # a risk of a race condition when we'd save after sending
98 # off the task)
99 task_id = unicode(uuid.uuid4())
100 entry['queued_task_id'] = task_id
07934b44 101
0bce749b
JW
102 # Save now so we have this data before kicking off processing
103 entry.save(validate=True)
fa7f9c61 104
0bce749b 105 # Pass off to processing
6788b412 106 #
0bce749b
JW
107 # (... don't change entry after this point to avoid race
108 # conditions with changes to the document via processing code)
109 process_media = registry.tasks[ProcessMedia.name]
110 try:
111 process_media.apply_async(
112 [unicode(entry._id)], {},
113 task_id=task_id)
114 except BaseException as exc:
115 # The purpose of this section is because when running in "lazy"
116 # or always-eager-with-exceptions-propagated celery mode that
117 # the failure handling won't happen on Celery end. Since we
118 # expect a lot of users to run things in this way we have to
119 # capture stuff here.
120 #
121 # ... not completely the diaper pattern because the
122 # exception is re-raised :)
123 mark_entry_failed(entry._id, exc)
124 # re-raise the exception
125 raise
126
127 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
128
129 return redirect(request, "mediagoblin.user_pages.user_home",
5a4e3ff1 130 user=request.user.username)
0bce749b
JW
131 except InvalidFileType, exc:
132 submit_form.file.errors.append(
133 _(u'Invalid file type.'))
f6f524bf 134
9038c9f9
CAW
135 return render_to_response(
136 request,
c9c24934 137 'mediagoblin/submit/start.html',
2c437493
JW
138 {'submit_form': submit_form,
139 'app_config': mg_globals.app_config})