437a5a517bfb756b333f0f974e72953a1f0ca313
[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, clean_html
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 import markdown
28
29
30 @require_active_login
31 def submit_start(request):
32 """
33 First view for submitting a file.
34 """
35 submit_form = submit_forms.SubmitStartForm(request.POST)
36
37 if request.method == 'POST' and submit_form.validate():
38 if not (request.POST.has_key('file')
39 and isinstance(request.POST['file'], FieldStorage)
40 and request.POST['file'].file):
41 submit_form.file.errors.append(
42 u'You must provide a file.')
43 elif not security.check_filetype(request.POST['file']):
44 submit_form.file.errors.append(
45 u'The file doesn\'t seem to be an image!')
46 else:
47 filename = request.POST['file'].filename
48
49 # create entry and save in database
50 entry = request.db.MediaEntry()
51 entry['title'] = request.POST['title'] or unicode(splitext(filename)[0])
52 entry['description'] = request.POST.get('description')
53
54 md = markdown.Markdown(
55 safe_mode = 'escape')
56 entry['description_html'] = clean_html(
57 md.convert(
58 entry['description']))
59
60 entry['media_type'] = u'image' # heh
61 entry['uploader'] = request.user['_id']
62
63 # Save, just so we can get the entry id for the sake of using
64 # it to generate the file path
65 entry.save(validate=False)
66
67 # Generate a slug from the title
68 entry.generate_slug()
69
70 # Now store generate the queueing related filename
71 queue_filepath = request.app.queue_store.get_unique_filepath(
72 ['media_entries',
73 unicode(entry['_id']),
74 secure_filename(filename)])
75
76 # queue appropriately
77 queue_file = request.app.queue_store.get_file(
78 queue_filepath, 'wb')
79
80 with queue_file:
81 queue_file.write(request.POST['file'].file.read())
82
83 # Add queued filename to the entry
84 entry['queued_media_file'] = queue_filepath
85 entry.save(validate=True)
86
87 # queue it for processing
88 process_media_initial.delay(unicode(entry['_id']))
89
90 return redirect(request, "mediagoblin.submit.success")
91
92 return render_to_response(
93 request,
94 'mediagoblin/submit/start.html',
95 {'submit_form': submit_form})
96
97
98 def submit_success(request):
99 return render_to_response(
100 request, 'mediagoblin/submit/success.html', {})