Merge remote-tracking branch 'refs/remotes/spaetz/trac_475_email_notification_checkbox'
[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 from celery import registry
23 import urllib
24 import urllib2
25 import logging
26
27 _log = logging.getLogger(__name__)
28
29 from werkzeug.utils import secure_filename
30 from werkzeug.datastructures import FileStorage
31
32 from mediagoblin.db.util import ObjectId
33 from mediagoblin.tools.text import convert_to_tag_list_of_dicts
34 from mediagoblin.tools.translate import pass_to_ugettext as _
35 from mediagoblin.tools.response import render_to_response, redirect
36 from mediagoblin.decorators import require_active_login
37 from mediagoblin.submit import forms as submit_forms
38 from mediagoblin.processing import mark_entry_failed
39 from mediagoblin.processing.task import ProcessMedia
40 from mediagoblin.messages import add_message, SUCCESS
41 from mediagoblin.media_types import sniff_media, \
42 InvalidFileType, FileTypeNotSupported
43
44
45 @require_active_login
46 def submit_start(request):
47 """
48 First view for submitting a file.
49 """
50 submit_form = submit_forms.SubmitStartForm(request.form)
51
52 if request.method == 'POST' and submit_form.validate():
53 if not ('file' in request.files
54 and isinstance(request.files['file'], FileStorage)
55 and request.files['file'].stream):
56 submit_form.file.errors.append(
57 _(u'You must provide a file.'))
58 else:
59 try:
60 filename = request.files['file'].filename
61
62 # Sniff the submitted media to determine which
63 # media plugin should handle processing
64 media_type, media_manager = sniff_media(
65 request.files['file'])
66
67 # create entry and save in database
68 entry = request.db.MediaEntry()
69 entry.id = ObjectId()
70 entry.media_type = unicode(media_type)
71 entry.title = (
72 unicode(request.form['title'])
73 or unicode(splitext(filename)[0]))
74
75 entry.description = unicode(request.form.get('description'))
76
77 entry.license = unicode(request.form.get('license', "")) or None
78
79 entry.uploader = request.user._id
80
81 # Process the user's folksonomy "tags"
82 entry.tags = convert_to_tag_list_of_dicts(
83 request.form.get('tags'))
84
85 # Generate a slug from the title
86 entry.generate_slug()
87
88 # We generate this ourselves so we know what the taks id is for
89 # retrieval later.
90
91 # (If we got it off the task's auto-generation, there'd be
92 # a risk of a race condition when we'd save after sending
93 # off the task)
94 task_id = unicode(uuid.uuid4())
95
96 # Now store generate the queueing related filename
97 queue_filepath = request.app.queue_store.get_unique_filepath(
98 ['media_entries',
99 task_id,
100 secure_filename(filename)])
101
102 # queue appropriately
103 queue_file = request.app.queue_store.get_file(
104 queue_filepath, 'wb')
105
106 with queue_file:
107 queue_file.write(request.files['file'].stream.read())
108
109 # Add queued filename to the entry
110 entry.queued_media_file = queue_filepath
111
112 entry.queued_task_id = task_id
113
114 # Save now so we have this data before kicking off processing
115 entry.save(validate=True)
116
117 # Pass off to processing
118 #
119 # (... don't change entry after this point to avoid race
120 # conditions with changes to the document via processing code)
121 process_media = registry.tasks[ProcessMedia.name]
122 try:
123 process_media.apply_async(
124 [unicode(entry._id)], {},
125 task_id=task_id)
126 except BaseException as exc:
127 # The purpose of this section is because when running in "lazy"
128 # or always-eager-with-exceptions-propagated celery mode that
129 # the failure handling won't happen on Celery end. Since we
130 # expect a lot of users to run things in this way we have to
131 # capture stuff here.
132 #
133 # ... not completely the diaper pattern because the
134 # exception is re-raised :)
135 mark_entry_failed(entry._id, exc)
136 # re-raise the exception
137 raise
138
139 if mg_globals.app_config["push_urls"]:
140 feed_url = request.urlgen(
141 'mediagoblin.user_pages.atom_feed',
142 qualified=True,
143 user=request.user.username)
144 hubparameters = {
145 'hub.mode': 'publish',
146 'hub.url': feed_url}
147 hubdata = urllib.urlencode(hubparameters)
148 hubheaders = {
149 "Content-type": "application/x-www-form-urlencoded",
150 "Connection": "close"}
151 for huburl in mg_globals.app_config["push_urls"]:
152 hubrequest = urllib2.Request(huburl, hubdata, hubheaders)
153 try:
154 hubresponse = urllib2.urlopen(hubrequest)
155 except urllib2.HTTPError as exc:
156 # This is not a big issue, the item will be fetched
157 # by the PuSH server next time we hit it
158 _log.warning(
159 "push url %r gave error %r", huburl, exc.code)
160 except urllib2.URLError as exc:
161 _log.warning(
162 "push url %r is unreachable %r", huburl, exc.reason)
163
164 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
165
166 return redirect(request, "mediagoblin.user_pages.user_home",
167 user=request.user.username)
168 except Exception as e:
169 '''
170 This section is intended to catch exceptions raised in
171 mediagoblin.media_types
172 '''
173 if isinstance(e, InvalidFileType) or \
174 isinstance(e, FileTypeNotSupported):
175 submit_form.file.errors.append(
176 e)
177 else:
178 raise
179
180 return render_to_response(
181 request,
182 'mediagoblin/submit/start.html',
183 {'submit_form': submit_form,
184 'app_config': mg_globals.app_config})
185
186 @require_active_login
187 def add_collection(request, media=None):
188 """
189 View to create a new collection
190 """
191 submit_form = submit_forms.AddCollectionForm(request.form)
192
193 if request.method == 'POST' and submit_form.validate():
194 try:
195 collection = request.db.Collection()
196 collection.id = ObjectId()
197
198 collection.title = unicode(request.form['title'])
199
200 collection.description = unicode(request.form.get('description'))
201 collection.creator = request.user._id
202 collection.generate_slug()
203
204 # Make sure this user isn't duplicating an existing collection
205 existing_collection = request.db.Collection.find_one({
206 'creator': request.user._id,
207 'title':collection.title})
208
209 if existing_collection:
210 messages.add_message(
211 request, messages.ERROR, _('You already have a collection called "%s"!' % collection.title))
212 else:
213 collection.save(validate=True)
214
215 add_message(request, SUCCESS, _('Collection "%s" added!' % collection.title))
216
217 return redirect(request, "mediagoblin.user_pages.user_home",
218 user=request.user.username)
219
220 except Exception as e:
221 raise
222
223 return render_to_response(
224 request,
225 'mediagoblin/submit/collection.html',
226 {'submit_form': submit_form,
227 'app_config': mg_globals.app_config})