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