Moving all views over to using util.render_template()!
[mediagoblin.git] / mediagoblin / submit / views.py
CommitLineData
e323a068
CAW
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
bb49e56f 17from os.path import splitext
03afc828
CAW
18from cgi import FieldStorage
19
e323a068 20from webob import Response, exc
f6f524bf 21from werkzeug.utils import secure_filename
e323a068 22
b5d3aec6 23from mediagoblin.util import render_template
e323a068
CAW
24from mediagoblin.decorators import require_active_login
25from mediagoblin.submit import forms as submit_forms
fa7f9c61 26from mediagoblin.process_media import process_media_initial
e323a068
CAW
27
28
29@require_active_login
30def submit_start(request):
31 """
32 First view for submitting a file.
33 """
20439236 34 submit_form = submit_forms.SubmitStartForm(request.POST)
e323a068 35
f6f524bf 36 if request.method == 'POST' and submit_form.validate():
03afc828
CAW
37 if not (request.POST.has_key('file')
38 and isinstance(request.POST['file'], FieldStorage)
39 and request.POST['file'].file):
40 submit_form.file.errors.append(
41 u'You must provide a file.')
42 else:
bb49e56f
AW
43 filename = request.POST['file'].filename
44
03afc828
CAW
45 # create entry and save in database
46 entry = request.db.MediaEntry()
bb49e56f 47 entry['title'] = request.POST['title'] or unicode(splitext(filename)[0])
ec61f094 48 entry['description'] = request.POST.get('description')
03afc828 49 entry['media_type'] = u'image' # heh
16509be1 50 entry['uploader'] = request.user['_id']
03afc828
CAW
51
52 # Save, just so we can get the entry id for the sake of using
53 # it to generate the file path
54 entry.save(validate=False)
55
0546833c
AW
56 # Generate a slug from the title
57 entry.generate_slug()
58
03afc828
CAW
59 # Now store generate the queueing related filename
60 queue_filepath = request.app.queue_store.get_unique_filepath(
61 ['media_entries',
03afc828 62 unicode(entry['_id']),
bb49e56f 63 secure_filename(filename)])
03afc828
CAW
64
65 # queue appropriately
66 queue_file = request.app.queue_store.get_file(
67 queue_filepath, 'wb')
68
69 with queue_file:
70 queue_file.write(request.POST['file'].file.read())
71
72 # Add queued filename to the entry
fa7f9c61 73 entry['queued_media_file'] = queue_filepath
03afc828
CAW
74 entry.save(validate=True)
75
fa7f9c61
CAW
76 # queue it for processing
77 process_media_initial.delay(unicode(entry['_id']))
78
03afc828
CAW
79 # redirect
80 return exc.HTTPFound(
81 location=request.urlgen("mediagoblin.submit.success"))
f6f524bf 82
e323a068 83 # render
e323a068 84 return Response(
b5d3aec6
CAW
85 render_template(
86 request, 'mediagoblin/submit/start.html',
87 {'submit_form': submit_form}))
f6f524bf
CAW
88
89
f6f524bf
CAW
90def submit_success(request):
91 # render
f6f524bf 92 return Response(
b5d3aec6
CAW
93 render_template(
94 request, 'mediagoblin/submit/success.html', {}))