This attribute in quotes, too. :)
[mediagoblin.git] / mediagoblin / submit / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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 from os.path import splitext
18 from cgi import FieldStorage
19
20 from werkzeug.utils import secure_filename
21
22 from mediagoblin.util import render_to_response, redirect
23 from mediagoblin.decorators import require_active_login
24 from mediagoblin.submit import forms as submit_forms, security
25 from mediagoblin.process_media import process_media_initial
26
27
28 @require_active_login
29 def submit_start(request):
30 """
31 First view for submitting a file.
32 """
33 submit_form = submit_forms.SubmitStartForm(request.POST)
34
35 if request.method == 'POST' and submit_form.validate():
36 if not (request.POST.has_key('file')
37 and isinstance(request.POST['file'], FieldStorage)
38 and request.POST['file'].file):
39 submit_form.file.errors.append(
40 u'You must provide a file.')
41 elif not security.check_filetype(request.POST['file']):
42 submit_form.file.errors.append(
43 u'The file doesn\'t seem to be an image!')
44 else:
45 filename = request.POST['file'].filename
46
47 # create entry and save in database
48 entry = request.db.MediaEntry()
49 entry['title'] = request.POST['title'] or unicode(splitext(filename)[0])
50 entry['description'] = request.POST.get('description')
51 entry['media_type'] = u'image' # heh
52 entry['uploader'] = request.user['_id']
53
54 # Save, just so we can get the entry id for the sake of using
55 # it to generate the file path
56 entry.save(validate=False)
57
58 # Generate a slug from the title
59 entry.generate_slug()
60
61 # Now store generate the queueing related filename
62 queue_filepath = request.app.queue_store.get_unique_filepath(
63 ['media_entries',
64 unicode(entry['_id']),
65 secure_filename(filename)])
66
67 # queue appropriately
68 queue_file = request.app.queue_store.get_file(
69 queue_filepath, 'wb')
70
71 with queue_file:
72 queue_file.write(request.POST['file'].file.read())
73
74 # Add queued filename to the entry
75 entry['queued_media_file'] = queue_filepath
76 entry.save(validate=True)
77
78 # queue it for processing
79 process_media_initial.delay(unicode(entry['_id']))
80
81 return redirect(request, "mediagoblin.submit.success")
82
83 return render_to_response(
84 request,
85 'mediagoblin/submit/start.html',
86 {'submit_form': submit_form})
87
88
89 def submit_success(request):
90 return render_to_response(
91 request, 'mediagoblin/submit/success.html', {})