upload refactor: push url handling
[mediagoblin.git] / mediagoblin / submit / views.py
CommitLineData
e323a068 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 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
be5be115 17from mediagoblin import messages
2c437493 18import mediagoblin.mg_globals as mg_globals
4a477e24 19import uuid
bb49e56f 20from os.path import splitext
03afc828 21
8e5f9746 22from celery import registry
77b91efc 23import logging
8e5f9746 24
c03d13cd 25_log = logging.getLogger(__name__)
8e5f9746 26
f6f524bf 27from werkzeug.utils import secure_filename
f1d06e1d 28from werkzeug.datastructures import FileStorage
e323a068 29
1e72e075 30from mediagoblin.tools.text import convert_to_tag_list_of_dicts
ae3bc7fa 31from mediagoblin.tools.translate import pass_to_ugettext as _
152a3bfa 32from mediagoblin.tools.response import render_to_response, redirect
e323a068 33from mediagoblin.decorators import require_active_login
32d8cf45 34from mediagoblin.submit import forms as submit_forms
eace050a
E
35from mediagoblin.processing import mark_entry_failed
36from mediagoblin.processing.task import ProcessMedia
4dc74441 37from mediagoblin.messages import add_message, SUCCESS
ec4261a4 38from mediagoblin.media_types import sniff_media, \
4601c30c 39 InvalidFileType, FileTypeNotSupported
be1f0f7d 40from mediagoblin.submit.lib import handle_push_urls
e323a068
CAW
41
42
43@require_active_login
44def submit_start(request):
45 """
46 First view for submitting a file.
47 """
111a609d 48 submit_form = submit_forms.SubmitStartForm(request.form)
e323a068 49
f6f524bf 50 if request.method == 'POST' and submit_form.validate():
f1d06e1d
JW
51 if not ('file' in request.files
52 and isinstance(request.files['file'], FileStorage)
53 and request.files['file'].stream):
03afc828 54 submit_form.file.errors.append(
4b1adc13 55 _(u'You must provide a file.'))
03afc828 56 else:
6788b412 57 try:
f1d06e1d 58 filename = request.files['file'].filename
ec4261a4
JW
59
60 # Sniff the submitted media to determine which
61 # media plugin should handle processing
62 media_type, media_manager = sniff_media(
f1d06e1d 63 request.files['file'])
bb49e56f 64
0bce749b
JW
65 # create entry and save in database
66 entry = request.db.MediaEntry()
f4ee8399 67 entry.media_type = unicode(media_type)
ec82fbd8 68 entry.title = (
111a609d 69 unicode(request.form['title'])
0bce749b 70 or unicode(splitext(filename)[0]))
93bdab9d 71
111a609d 72 entry.description = unicode(request.form.get('description'))
4bf8e888 73
111a609d 74 entry.license = unicode(request.form.get('license', "")) or None
99a270e9 75
5c2b8486 76 entry.uploader = request.user.id
03afc828 77
0bce749b 78 # Process the user's folksonomy "tags"
de917303 79 entry.tags = convert_to_tag_list_of_dicts(
111a609d 80 request.form.get('tags'))
03afc828 81
0bce749b
JW
82 # Generate a slug from the title
83 entry.generate_slug()
4a477e24 84
572d4f01
E
85 # We generate this ourselves so we know what the taks id is for
86 # retrieval later.
87
88 # (If we got it off the task's auto-generation, there'd be
89 # a risk of a race condition when we'd save after sending
90 # off the task)
91 task_id = unicode(uuid.uuid4())
4dc74441 92
0bce749b
JW
93 # Now store generate the queueing related filename
94 queue_filepath = request.app.queue_store.get_unique_filepath(
95 ['media_entries',
572d4f01 96 task_id,
0bce749b 97 secure_filename(filename)])
03afc828 98
0bce749b
JW
99 # queue appropriately
100 queue_file = request.app.queue_store.get_file(
101 queue_filepath, 'wb')
03afc828 102
0bce749b 103 with queue_file:
f1d06e1d 104 queue_file.write(request.files['file'].stream.read())
03afc828 105
0bce749b 106 # Add queued filename to the entry
8545cfc9 107 entry.queued_media_file = queue_filepath
03afc828 108
572d4f01 109 entry.queued_task_id = task_id
07934b44 110
0bce749b 111 # Save now so we have this data before kicking off processing
b39d1f23 112 entry.save()
fa7f9c61 113
0bce749b 114 # Pass off to processing
6788b412 115 #
0bce749b
JW
116 # (... don't change entry after this point to avoid race
117 # conditions with changes to the document via processing code)
118 process_media = registry.tasks[ProcessMedia.name]
119 try:
120 process_media.apply_async(
5c2b8486 121 [unicode(entry.id)], {},
0bce749b
JW
122 task_id=task_id)
123 except BaseException as exc:
124 # The purpose of this section is because when running in "lazy"
125 # or always-eager-with-exceptions-propagated celery mode that
126 # the failure handling won't happen on Celery end. Since we
127 # expect a lot of users to run things in this way we have to
128 # capture stuff here.
129 #
130 # ... not completely the diaper pattern because the
131 # exception is re-raised :)
5c2b8486 132 mark_entry_failed(entry.id, exc)
0bce749b
JW
133 # re-raise the exception
134 raise
135
be1f0f7d 136 handle_push_urls(request)
5b1a7bae 137
0bce749b
JW
138 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
139
140 return redirect(request, "mediagoblin.user_pages.user_home",
5a4e3ff1 141 user=request.user.username)
a246ccca
JW
142 except Exception as e:
143 '''
deea3f66 144 This section is intended to catch exceptions raised in
7a258b14 145 mediagoblin.media_types
a246ccca 146 '''
4601c30c
JW
147 if isinstance(e, InvalidFileType) or \
148 isinstance(e, FileTypeNotSupported):
149 submit_form.file.errors.append(
150 e)
151 else:
152 raise
f6f524bf 153
9038c9f9
CAW
154 return render_to_response(
155 request,
c9c24934 156 'mediagoblin/submit/start.html',
2c437493
JW
157 {'submit_form': submit_form,
158 'app_config': mg_globals.app_config})
be5be115
AW
159
160@require_active_login
161def add_collection(request, media=None):
162 """
163 View to create a new collection
164 """
111a609d 165 submit_form = submit_forms.AddCollectionForm(request.form)
be5be115
AW
166
167 if request.method == 'POST' and submit_form.validate():
168 try:
169 collection = request.db.Collection()
be5be115 170
111a609d 171 collection.title = unicode(request.form['title'])
111a609d 172 collection.description = unicode(request.form.get('description'))
5c2b8486 173 collection.creator = request.user.id
be5be115
AW
174 collection.generate_slug()
175
176 # Make sure this user isn't duplicating an existing collection
177 existing_collection = request.db.Collection.find_one({
5c2b8486 178 'creator': request.user.id,
be5be115 179 'title':collection.title})
f1d06e1d 180
be5be115
AW
181 if existing_collection:
182 messages.add_message(
183 request, messages.ERROR, _('You already have a collection called "%s"!' % collection.title))
184 else:
b39d1f23 185 collection.save()
f1d06e1d 186
be5be115
AW
187 add_message(request, SUCCESS, _('Collection "%s" added!' % collection.title))
188
189 return redirect(request, "mediagoblin.user_pages.user_home",
190 user=request.user.username)
191
192 except Exception as e:
193 raise
194
195 return render_to_response(
196 request,
197 'mediagoblin/submit/collection.html',
198 {'submit_form': submit_form,
199 'app_config': mg_globals.app_config})