upload refactor: Use prepare_entry in api.
[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
E
35from mediagoblin.submit.lib import handle_push_urls, run_process_media, \
36 prepare_entry
e323a068
CAW
37
38
39@require_active_login
40def submit_start(request):
41 """
42 First view for submitting a file.
43 """
111a609d 44 submit_form = submit_forms.SubmitStartForm(request.form)
e323a068 45
f6f524bf 46 if request.method == 'POST' and submit_form.validate():
f1d06e1d
JW
47 if not ('file' in request.files
48 and isinstance(request.files['file'], FileStorage)
49 and request.files['file'].stream):
03afc828 50 submit_form.file.errors.append(
4b1adc13 51 _(u'You must provide a file.'))
03afc828 52 else:
6788b412 53 try:
f1d06e1d 54 filename = request.files['file'].filename
ec4261a4
JW
55
56 # Sniff the submitted media to determine which
57 # media plugin should handle processing
58 media_type, media_manager = sniff_media(
f1d06e1d 59 request.files['file'])
bb49e56f 60
0bce749b
JW
61 # create entry and save in database
62 entry = request.db.MediaEntry()
f4ee8399 63 entry.media_type = unicode(media_type)
ec82fbd8 64 entry.title = (
111a609d 65 unicode(request.form['title'])
0bce749b 66 or unicode(splitext(filename)[0]))
93bdab9d 67
111a609d 68 entry.description = unicode(request.form.get('description'))
4bf8e888 69
111a609d 70 entry.license = unicode(request.form.get('license', "")) or None
99a270e9 71
5c2b8486 72 entry.uploader = request.user.id
03afc828 73
0bce749b 74 # Process the user's folksonomy "tags"
de917303 75 entry.tags = convert_to_tag_list_of_dicts(
111a609d 76 request.form.get('tags'))
03afc828 77
0bce749b
JW
78 # Generate a slug from the title
79 entry.generate_slug()
4a477e24 80
8eb47d02 81 queue_file = prepare_entry(request, entry, filename)
03afc828 82
0bce749b 83 with queue_file:
f1d06e1d 84 queue_file.write(request.files['file'].stream.read())
03afc828 85
0bce749b 86 # Save now so we have this data before kicking off processing
b39d1f23 87 entry.save()
fa7f9c61 88
0bce749b 89 # Pass off to processing
6788b412 90 #
0bce749b
JW
91 # (... don't change entry after this point to avoid race
92 # conditions with changes to the document via processing code)
86bb44ef 93 run_process_media(entry)
0bce749b 94
be1f0f7d 95 handle_push_urls(request)
5b1a7bae 96
0bce749b
JW
97 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
98
99 return redirect(request, "mediagoblin.user_pages.user_home",
5a4e3ff1 100 user=request.user.username)
a246ccca
JW
101 except Exception as e:
102 '''
deea3f66 103 This section is intended to catch exceptions raised in
7a258b14 104 mediagoblin.media_types
a246ccca 105 '''
4601c30c
JW
106 if isinstance(e, InvalidFileType) or \
107 isinstance(e, FileTypeNotSupported):
108 submit_form.file.errors.append(
109 e)
110 else:
111 raise
f6f524bf 112
9038c9f9
CAW
113 return render_to_response(
114 request,
c9c24934 115 'mediagoblin/submit/start.html',
2c437493
JW
116 {'submit_form': submit_form,
117 'app_config': mg_globals.app_config})
be5be115
AW
118
119@require_active_login
120def add_collection(request, media=None):
121 """
122 View to create a new collection
123 """
111a609d 124 submit_form = submit_forms.AddCollectionForm(request.form)
be5be115
AW
125
126 if request.method == 'POST' and submit_form.validate():
127 try:
128 collection = request.db.Collection()
be5be115 129
111a609d 130 collection.title = unicode(request.form['title'])
111a609d 131 collection.description = unicode(request.form.get('description'))
5c2b8486 132 collection.creator = request.user.id
be5be115
AW
133 collection.generate_slug()
134
135 # Make sure this user isn't duplicating an existing collection
136 existing_collection = request.db.Collection.find_one({
5c2b8486 137 'creator': request.user.id,
be5be115 138 'title':collection.title})
f1d06e1d 139
be5be115
AW
140 if existing_collection:
141 messages.add_message(
142 request, messages.ERROR, _('You already have a collection called "%s"!' % collection.title))
143 else:
b39d1f23 144 collection.save()
f1d06e1d 145
be5be115
AW
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})