Add a license preference field
[mediagoblin.git] / mediagoblin / submit / views.py
CommitLineData
e323a068 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
e323a068
CAW
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
be5be115 17from mediagoblin import messages
2c437493 18import mediagoblin.mg_globals as mg_globals
bb49e56f 19from os.path import splitext
03afc828 20
77b91efc 21import logging
8e5f9746 22
c03d13cd 23_log = logging.getLogger(__name__)
8e5f9746 24
f1d06e1d 25from werkzeug.datastructures import FileStorage
e323a068 26
1e72e075 27from mediagoblin.tools.text import convert_to_tag_list_of_dicts
ae3bc7fa 28from mediagoblin.tools.translate import pass_to_ugettext as _
152a3bfa 29from mediagoblin.tools.response import render_to_response, redirect
e323a068 30from mediagoblin.decorators import require_active_login
32d8cf45 31from mediagoblin.submit import forms as submit_forms
4dc74441 32from mediagoblin.messages import add_message, SUCCESS
ec4261a4 33from mediagoblin.media_types import sniff_media, \
4601c30c 34 InvalidFileType, FileTypeNotSupported
8eb47d02 35from mediagoblin.submit.lib import handle_push_urls, run_process_media, \
b228d897 36 prepare_queue_task
e323a068
CAW
37
38
39@require_active_login
40def submit_start(request):
41 """
42 First view for submitting a file.
43 """
dc4dfbde
MH
44 submit_form = submit_forms.SubmitStartForm(request.form,
45 license=request.user.get('license_preference'))
e323a068 46
f6f524bf 47 if request.method == 'POST' and submit_form.validate():
f1d06e1d
JW
48 if not ('file' in request.files
49 and isinstance(request.files['file'], FileStorage)
50 and request.files['file'].stream):
03afc828 51 submit_form.file.errors.append(
4b1adc13 52 _(u'You must provide a file.'))
03afc828 53 else:
6788b412 54 try:
f1d06e1d 55 filename = request.files['file'].filename
ec4261a4
JW
56
57 # Sniff the submitted media to determine which
58 # media plugin should handle processing
59 media_type, media_manager = sniff_media(
f1d06e1d 60 request.files['file'])
bb49e56f 61
0bce749b
JW
62 # create entry and save in database
63 entry = request.db.MediaEntry()
f4ee8399 64 entry.media_type = unicode(media_type)
ec82fbd8 65 entry.title = (
111a609d 66 unicode(request.form['title'])
0bce749b 67 or unicode(splitext(filename)[0]))
93bdab9d 68
111a609d 69 entry.description = unicode(request.form.get('description'))
4bf8e888 70
111a609d 71 entry.license = unicode(request.form.get('license', "")) or None
99a270e9 72
5c2b8486 73 entry.uploader = request.user.id
03afc828 74
0bce749b 75 # Process the user's folksonomy "tags"
de917303 76 entry.tags = convert_to_tag_list_of_dicts(
111a609d 77 request.form.get('tags'))
03afc828 78
0bce749b
JW
79 # Generate a slug from the title
80 entry.generate_slug()
4a477e24 81
b228d897 82 queue_file = prepare_queue_task(request.app, entry, filename)
03afc828 83
0bce749b 84 with queue_file:
f1d06e1d 85 queue_file.write(request.files['file'].stream.read())
03afc828 86
0bce749b 87 # Save now so we have this data before kicking off processing
b39d1f23 88 entry.save()
fa7f9c61 89
0bce749b 90 # Pass off to processing
6788b412 91 #
0bce749b
JW
92 # (... don't change entry after this point to avoid race
93 # conditions with changes to the document via processing code)
86bb44ef 94 run_process_media(entry)
0bce749b 95
be1f0f7d 96 handle_push_urls(request)
5b1a7bae 97
0bce749b
JW
98 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
99
100 return redirect(request, "mediagoblin.user_pages.user_home",
5a4e3ff1 101 user=request.user.username)
a246ccca
JW
102 except Exception as e:
103 '''
deea3f66 104 This section is intended to catch exceptions raised in
7a258b14 105 mediagoblin.media_types
a246ccca 106 '''
4601c30c
JW
107 if isinstance(e, InvalidFileType) or \
108 isinstance(e, FileTypeNotSupported):
109 submit_form.file.errors.append(
110 e)
111 else:
112 raise
f6f524bf 113
9038c9f9
CAW
114 return render_to_response(
115 request,
c9c24934 116 'mediagoblin/submit/start.html',
2c437493
JW
117 {'submit_form': submit_form,
118 'app_config': mg_globals.app_config})
be5be115
AW
119
120@require_active_login
121def add_collection(request, media=None):
122 """
123 View to create a new collection
124 """
111a609d 125 submit_form = submit_forms.AddCollectionForm(request.form)
be5be115
AW
126
127 if request.method == 'POST' and submit_form.validate():
128 try:
129 collection = request.db.Collection()
be5be115 130
111a609d 131 collection.title = unicode(request.form['title'])
111a609d 132 collection.description = unicode(request.form.get('description'))
5c2b8486 133 collection.creator = request.user.id
be5be115
AW
134 collection.generate_slug()
135
136 # Make sure this user isn't duplicating an existing collection
137 existing_collection = request.db.Collection.find_one({
5c2b8486 138 'creator': request.user.id,
be5be115 139 'title':collection.title})
f1d06e1d 140
be5be115
AW
141 if existing_collection:
142 messages.add_message(
143 request, messages.ERROR, _('You already have a collection called "%s"!' % collection.title))
144 else:
b39d1f23 145 collection.save()
f1d06e1d 146
be5be115
AW
147 add_message(request, SUCCESS, _('Collection "%s" added!' % collection.title))
148
149 return redirect(request, "mediagoblin.user_pages.user_home",
150 user=request.user.username)
151
152 except Exception as e:
153 raise
154
155 return render_to_response(
156 request,
157 'mediagoblin/submit/collection.html',
158 {'submit_form': submit_form,
159 'app_config': mg_globals.app_config})