Merge branch 'master' into joar-skip_transcoding
[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 from os.path import splitext
20
21 import logging
22
23 _log = logging.getLogger(__name__)
24
25 from werkzeug.datastructures import FileStorage
26
27 from mediagoblin.tools.text import convert_to_tag_list_of_dicts
28 from mediagoblin.tools.translate import pass_to_ugettext as _
29 from mediagoblin.tools.response import render_to_response, redirect
30 from mediagoblin.decorators import require_active_login
31 from mediagoblin.submit import forms as submit_forms
32 from mediagoblin.messages import add_message, SUCCESS
33 from mediagoblin.media_types import sniff_media, \
34 InvalidFileType, FileTypeNotSupported
35 from mediagoblin.submit.lib import run_process_media, prepare_queue_task
36
37
38 @require_active_login
39 def submit_start(request):
40 """
41 First view for submitting a file.
42 """
43 submit_form = submit_forms.SubmitStartForm(request.form,
44 license=request.user.license_preference)
45
46 if request.method == 'POST' and submit_form.validate():
47 if not ('file' in request.files
48 and isinstance(request.files['file'], FileStorage)
49 and request.files['file'].stream):
50 submit_form.file.errors.append(
51 _(u'You must provide a file.'))
52 else:
53 try:
54 filename = request.files['file'].filename
55
56 # Sniff the submitted media to determine which
57 # media plugin should handle processing
58 media_type, media_manager = sniff_media(
59 request.files['file'])
60
61 # create entry and save in database
62 entry = request.db.MediaEntry()
63 entry.media_type = unicode(media_type)
64 entry.title = (
65 unicode(request.form['title'])
66 or unicode(splitext(filename)[0]))
67
68 entry.description = unicode(request.form.get('description'))
69
70 entry.license = unicode(request.form.get('license', "")) or None
71
72 entry.uploader = request.user.id
73
74 # Process the user's folksonomy "tags"
75 entry.tags = convert_to_tag_list_of_dicts(
76 request.form.get('tags'))
77
78 # Generate a slug from the title
79 entry.generate_slug()
80
81 queue_file = prepare_queue_task(request.app, entry, filename)
82
83 with queue_file:
84 queue_file.write(request.files['file'].stream.read())
85
86 # Save now so we have this data before kicking off processing
87 entry.save()
88
89 # Pass off to processing
90 #
91 # (... don't change entry after this point to avoid race
92 # conditions with changes to the document via processing code)
93 feed_url = request.urlgen(
94 'mediagoblin.user_pages.atom_feed',
95 qualified=True, user=request.user.username)
96 run_process_media(entry, feed_url)
97 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
98
99 return redirect(request, "mediagoblin.user_pages.user_home",
100 user=request.user.username)
101 except Exception as e:
102 '''
103 This section is intended to catch exceptions raised in
104 mediagoblin.media_types
105 '''
106 if isinstance(e, InvalidFileType) or \
107 isinstance(e, FileTypeNotSupported):
108 submit_form.file.errors.append(
109 e)
110 else:
111 raise
112
113 return render_to_response(
114 request,
115 'mediagoblin/submit/start.html',
116 {'submit_form': submit_form,
117 'app_config': mg_globals.app_config})
118
119 @require_active_login
120 def add_collection(request, media=None):
121 """
122 View to create a new collection
123 """
124 submit_form = submit_forms.AddCollectionForm(request.form)
125
126 if request.method == 'POST' and submit_form.validate():
127 try:
128 collection = request.db.Collection()
129
130 collection.title = unicode(request.form['title'])
131 collection.description = unicode(request.form.get('description'))
132 collection.creator = request.user.id
133 collection.generate_slug()
134
135 # Make sure this user isn't duplicating an existing collection
136 existing_collection = request.db.Collection.find_one({
137 'creator': request.user.id,
138 'title':collection.title})
139
140 if existing_collection:
141 messages.add_message(
142 request, messages.ERROR, _('You already have a collection called "%s"!' % collection.title))
143 else:
144 collection.save()
145
146 add_message(request, SUCCESS, _('Collection "%s" added!' % collection.title))
147
148 return redirect(request, "mediagoblin.user_pages.user_home",
149 user=request.user.username)
150
151 except Exception as e:
152 raise
153
154 return render_to_response(
155 request,
156 'mediagoblin/submit/collection.html',
157 {'submit_form': submit_form,
158 'app_config': mg_globals.app_config})