65243ca158f4c8058e4d6dd213382eb535f4dbf4
[mediagoblin.git] / mediagoblin / submit / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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 import mediagoblin.mg_globals as mg_globals
18 import uuid
19 from os.path import splitext
20 from cgi import FieldStorage
21
22 from celery import registry
23 import urllib,urllib2
24
25 from werkzeug.utils import secure_filename
26
27 from mediagoblin.db.util import ObjectId
28 from mediagoblin.tools.text import cleaned_markdown_conversion, convert_to_tag_list_of_dicts
29 from mediagoblin.tools.translate import pass_to_ugettext as _
30 from mediagoblin.tools.response import render_to_response, redirect
31 from mediagoblin.decorators import require_active_login
32 from mediagoblin.submit import forms as submit_forms, security
33 from mediagoblin.processing import mark_entry_failed, ProcessMedia
34 from mediagoblin.messages import add_message, SUCCESS
35 from mediagoblin.media_types import get_media_type_and_manager, \
36 InvalidFileType, FileTypeNotSupported
37
38
39 @require_active_login
40 def submit_start(request):
41 """
42 First view for submitting a file.
43 """
44 submit_form = submit_forms.SubmitStartForm(request.POST)
45
46 if request.method == 'POST' and submit_form.validate():
47 if not ('file' in request.POST
48 and isinstance(request.POST['file'], FieldStorage)
49 and request.POST['file'].file):
50 submit_form.file.errors.append(
51 _(u'You must provide a file.'))
52 else:
53 try:
54 filename = request.POST['file'].filename
55 media_type, media_manager = get_media_type_and_manager(filename)
56
57 # create entry and save in database
58 entry = request.db.MediaEntry()
59 entry['_id'] = ObjectId()
60 entry.media_type = unicode(media_type)
61 entry.title = (
62 unicode(request.POST['title'])
63 or unicode(splitext(filename)[0]))
64
65 entry.description = unicode(request.POST.get('description'))
66 entry.description_html = cleaned_markdown_conversion(
67 entry.description)
68
69 entry.uploader = request.user._id
70
71 # Process the user's folksonomy "tags"
72 entry['tags'] = convert_to_tag_list_of_dicts(
73 request.POST.get('tags'))
74
75 # Generate a slug from the title
76 entry.generate_slug()
77
78
79 # Now store generate the queueing related filename
80 queue_filepath = request.app.queue_store.get_unique_filepath(
81 ['media_entries',
82 unicode(entry._id),
83 secure_filename(filename)])
84
85 # queue appropriately
86 queue_file = request.app.queue_store.get_file(
87 queue_filepath, 'wb')
88
89 with queue_file:
90 queue_file.write(request.POST['file'].file.read())
91
92 # Add queued filename to the entry
93 entry.queued_media_file = queue_filepath
94
95 # We generate this ourselves so we know what the taks id is for
96 # retrieval later.
97
98 # (If we got it off the task's auto-generation, there'd be
99 # a risk of a race condition when we'd save after sending
100 # off the task)
101 task_id = unicode(uuid.uuid4())
102 entry['queued_task_id'] = task_id
103
104 # Save now so we have this data before kicking off processing
105 entry.save(validate=True)
106
107 # Pass off to processing
108 #
109 # (... don't change entry after this point to avoid race
110 # conditions with changes to the document via processing code)
111 process_media = registry.tasks[ProcessMedia.name]
112 try:
113 process_media.apply_async(
114 [unicode(entry._id)], {},
115 task_id=task_id)
116 except BaseException as exc:
117 # The purpose of this section is because when running in "lazy"
118 # or always-eager-with-exceptions-propagated celery mode that
119 # the failure handling won't happen on Celery end. Since we
120 # expect a lot of users to run things in this way we have to
121 # capture stuff here.
122 #
123 # ... not completely the diaper pattern because the
124 # exception is re-raised :)
125 mark_entry_failed(entry._id, exc)
126 # re-raise the exception
127 raise
128
129 if mg_globals.app_config["push_urls"]:
130 feed_url=request.urlgen(
131 'mediagoblin.user_pages.atom_feed',
132 qualified=True,user=request.user.username)
133 hubparameters = {
134 'hub.mode': 'publish',
135 'hub.url': feed_url}
136 hubdata = urllib.urlencode(hubparameters)
137 hubheaders = {
138 "Content-type": "application/x-www-form-urlencoded",
139 "Connection": "close"}
140 for huburl in mg_globals.app_config["push_urls"]:
141 hubrequest = urllib2.Request(huburl, hubdata,hubheaders)
142 hubresponse = urllib2.urlopen(hubrequest)
143
144 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
145
146 return redirect(request, "mediagoblin.user_pages.user_home",
147 user=request.user.username)
148 except Exception as e:
149 '''
150 This section is intended to catch exceptions raised in
151 mediagobling.media_types
152 '''
153
154 if isinstance(e, InvalidFileType) or \
155 isinstance(e, FileTypeNotSupported):
156 submit_form.file.errors.append(
157 e)
158 else:
159 raise
160
161 return render_to_response(
162 request,
163 'mediagoblin/submit/start.html',
164 {'submit_form': submit_form,
165 'app_config': mg_globals.app_config})