ad9fedae1263db70b2e27444f649fcc57123d951
[mediagoblin.git] / mediagoblin / submit / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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 mediagoblin import messages
18 import mediagoblin.mg_globals as mg_globals
19 import uuid
20 from os.path import splitext
21
22 import logging
23
24 _log = logging.getLogger(__name__)
25
26 from werkzeug.utils import secure_filename
27 from werkzeug.datastructures import FileStorage
28
29 from mediagoblin.tools.text import convert_to_tag_list_of_dicts
30 from mediagoblin.tools.translate import pass_to_ugettext as _
31 from mediagoblin.tools.response import render_to_response, redirect
32 from mediagoblin.decorators import require_active_login
33 from mediagoblin.submit import forms as submit_forms
34 from mediagoblin.messages import add_message, SUCCESS
35 from mediagoblin.media_types import sniff_media, \
36 InvalidFileType, FileTypeNotSupported
37 from mediagoblin.submit.lib import handle_push_urls, run_process_media
38
39
40 @require_active_login
41 def submit_start(request):
42 """
43 First view for submitting a file.
44 """
45 submit_form = submit_forms.SubmitStartForm(request.form)
46
47 if request.method == 'POST' and submit_form.validate():
48 if not ('file' in request.files
49 and isinstance(request.files['file'], FileStorage)
50 and request.files['file'].stream):
51 submit_form.file.errors.append(
52 _(u'You must provide a file.'))
53 else:
54 try:
55 filename = request.files['file'].filename
56
57 # Sniff the submitted media to determine which
58 # media plugin should handle processing
59 media_type, media_manager = sniff_media(
60 request.files['file'])
61
62 # create entry and save in database
63 entry = request.db.MediaEntry()
64 entry.media_type = unicode(media_type)
65 entry.title = (
66 unicode(request.form['title'])
67 or unicode(splitext(filename)[0]))
68
69 entry.description = unicode(request.form.get('description'))
70
71 entry.license = unicode(request.form.get('license', "")) or None
72
73 entry.uploader = request.user.id
74
75 # Process the user's folksonomy "tags"
76 entry.tags = convert_to_tag_list_of_dicts(
77 request.form.get('tags'))
78
79 # Generate a slug from the title
80 entry.generate_slug()
81
82 # We generate this ourselves so we know what the taks id is for
83 # retrieval later.
84
85 # (If we got it off the task's auto-generation, there'd be
86 # a risk of a race condition when we'd save after sending
87 # off the task)
88 task_id = unicode(uuid.uuid4())
89
90 # Now store generate the queueing related filename
91 queue_filepath = request.app.queue_store.get_unique_filepath(
92 ['media_entries',
93 task_id,
94 secure_filename(filename)])
95
96 # queue appropriately
97 queue_file = request.app.queue_store.get_file(
98 queue_filepath, 'wb')
99
100 with queue_file:
101 queue_file.write(request.files['file'].stream.read())
102
103 # Add queued filename to the entry
104 entry.queued_media_file = queue_filepath
105
106 entry.queued_task_id = task_id
107
108 # Save now so we have this data before kicking off processing
109 entry.save()
110
111 # Pass off to processing
112 #
113 # (... don't change entry after this point to avoid race
114 # conditions with changes to the document via processing code)
115 run_process_media(entry)
116
117 handle_push_urls(request)
118
119 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
120
121 return redirect(request, "mediagoblin.user_pages.user_home",
122 user=request.user.username)
123 except Exception as e:
124 '''
125 This section is intended to catch exceptions raised in
126 mediagoblin.media_types
127 '''
128 if isinstance(e, InvalidFileType) or \
129 isinstance(e, FileTypeNotSupported):
130 submit_form.file.errors.append(
131 e)
132 else:
133 raise
134
135 return render_to_response(
136 request,
137 'mediagoblin/submit/start.html',
138 {'submit_form': submit_form,
139 'app_config': mg_globals.app_config})
140
141 @require_active_login
142 def add_collection(request, media=None):
143 """
144 View to create a new collection
145 """
146 submit_form = submit_forms.AddCollectionForm(request.form)
147
148 if request.method == 'POST' and submit_form.validate():
149 try:
150 collection = request.db.Collection()
151
152 collection.title = unicode(request.form['title'])
153 collection.description = unicode(request.form.get('description'))
154 collection.creator = request.user.id
155 collection.generate_slug()
156
157 # Make sure this user isn't duplicating an existing collection
158 existing_collection = request.db.Collection.find_one({
159 'creator': request.user.id,
160 'title':collection.title})
161
162 if existing_collection:
163 messages.add_message(
164 request, messages.ERROR, _('You already have a collection called "%s"!' % collection.title))
165 else:
166 collection.save()
167
168 add_message(request, SUCCESS, _('Collection "%s" added!' % collection.title))
169
170 return redirect(request, "mediagoblin.user_pages.user_home",
171 user=request.user.username)
172
173 except Exception as e:
174 raise
175
176 return render_to_response(
177 request,
178 'mediagoblin/submit/collection.html',
179 {'submit_form': submit_form,
180 'app_config': mg_globals.app_config})