min=0 makes more sense than min=-1
[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
CAW
22
23from mediagoblin.decorators import require_active_login
24from mediagoblin.submit import forms as submit_forms
fa7f9c61 25from mediagoblin.process_media import process_media_initial
e323a068
CAW
26
27
28@require_active_login
29def submit_start(request):
30 """
31 First view for submitting a file.
32 """
20439236 33 submit_form = submit_forms.SubmitStartForm(request.POST)
e323a068 34
f6f524bf 35 if request.method == 'POST' and submit_form.validate():
03afc828
CAW
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 else:
bb49e56f
AW
42 filename = request.POST['file'].filename
43
03afc828
CAW
44 # create entry and save in database
45 entry = request.db.MediaEntry()
bb49e56f 46 entry['title'] = request.POST['title'] or unicode(splitext(filename)[0])
ec61f094 47 entry['description'] = request.POST.get('description')
03afc828
CAW
48 entry['media_type'] = u'image' # heh
49 entry['uploader'] = request.user
50
51 # Save, just so we can get the entry id for the sake of using
52 # it to generate the file path
53 entry.save(validate=False)
54
55 # Now store generate the queueing related filename
56 queue_filepath = request.app.queue_store.get_unique_filepath(
57 ['media_entries',
03afc828 58 unicode(entry['_id']),
bb49e56f 59 secure_filename(filename)])
03afc828
CAW
60
61 # queue appropriately
62 queue_file = request.app.queue_store.get_file(
63 queue_filepath, 'wb')
64
65 with queue_file:
66 queue_file.write(request.POST['file'].file.read())
67
68 # Add queued filename to the entry
fa7f9c61 69 entry['queued_media_file'] = queue_filepath
03afc828
CAW
70 entry.save(validate=True)
71
fa7f9c61
CAW
72 # queue it for processing
73 process_media_initial.delay(unicode(entry['_id']))
74
03afc828
CAW
75 # redirect
76 return exc.HTTPFound(
77 location=request.urlgen("mediagoblin.submit.success"))
f6f524bf 78
e323a068
CAW
79 # render
80 template = request.template_env.get_template(
81 'mediagoblin/submit/start.html')
82 return Response(
83 template.render(
84 {'request': request,
85 'submit_form': submit_form}))
f6f524bf
CAW
86
87
f6f524bf
CAW
88def submit_success(request):
89 # render
90 template = request.template_env.get_template(
91 'mediagoblin/submit/success.html')
92 return Response(
93 template.render(
94 {'request': request}))