1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 from mediagoblin
import messages
18 import mediagoblin
.mg_globals
as mg_globals
20 from os
.path
import splitext
24 _log
= logging
.getLogger(__name__
)
26 from werkzeug
.utils
import secure_filename
27 from werkzeug
.datastructures
import FileStorage
29 from mediagoblin
.tools
.text
import convert_to_tag_list_of_dicts
30 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
31 from mediagoblin
.tools
.response
import render_to_response
, redirect
32 from mediagoblin
.decorators
import require_active_login
33 from mediagoblin
.submit
import forms
as submit_forms
34 from mediagoblin
.messages
import add_message
, SUCCESS
35 from mediagoblin
.media_types
import sniff_media
, \
36 InvalidFileType
, FileTypeNotSupported
37 from mediagoblin
.submit
.lib
import handle_push_urls
, run_process_media
41 def submit_start(request
):
43 First view for submitting a file.
45 submit_form
= submit_forms
.SubmitStartForm(request
.form
)
47 if request
.method
== 'POST' and submit_form
.validate():
48 if not ('file' in request
.files
49 and isinstance(request
.files
['file'], FileStorage
)
50 and request
.files
['file'].stream
):
51 submit_form
.file.errors
.append(
52 _(u
'You must provide a file.'))
55 filename
= request
.files
['file'].filename
57 # Sniff the submitted media to determine which
58 # media plugin should handle processing
59 media_type
, media_manager
= sniff_media(
60 request
.files
['file'])
62 # create entry and save in database
63 entry
= request
.db
.MediaEntry()
64 entry
.media_type
= unicode(media_type
)
66 unicode(request
.form
['title'])
67 or unicode(splitext(filename
)[0]))
69 entry
.description
= unicode(request
.form
.get('description'))
71 entry
.license
= unicode(request
.form
.get('license', "")) or None
73 entry
.uploader
= request
.user
.id
75 # Process the user's folksonomy "tags"
76 entry
.tags
= convert_to_tag_list_of_dicts(
77 request
.form
.get('tags'))
79 # Generate a slug from the title
82 # We generate this ourselves so we know what the taks id is for
85 # (If we got it off the task's auto-generation, there'd be
86 # a risk of a race condition when we'd save after sending
88 task_id
= unicode(uuid
.uuid4())
90 # Now store generate the queueing related filename
91 queue_filepath
= request
.app
.queue_store
.get_unique_filepath(
94 secure_filename(filename
)])
97 queue_file
= request
.app
.queue_store
.get_file(
101 queue_file
.write(request
.files
['file'].stream
.read())
103 # Add queued filename to the entry
104 entry
.queued_media_file
= queue_filepath
106 entry
.queued_task_id
= task_id
108 # Save now so we have this data before kicking off processing
111 # Pass off to processing
113 # (... don't change entry after this point to avoid race
114 # conditions with changes to the document via processing code)
115 run_process_media(entry
)
117 handle_push_urls(request
)
119 add_message(request
, SUCCESS
, _('Woohoo! Submitted!'))
121 return redirect(request
, "mediagoblin.user_pages.user_home",
122 user
=request
.user
.username
)
123 except Exception as e
:
125 This section is intended to catch exceptions raised in
126 mediagoblin.media_types
128 if isinstance(e
, InvalidFileType
) or \
129 isinstance(e
, FileTypeNotSupported
):
130 submit_form
.file.errors
.append(
135 return render_to_response(
137 'mediagoblin/submit/start.html',
138 {'submit_form': submit_form
,
139 'app_config': mg_globals
.app_config
})
141 @require_active_login
142 def add_collection(request
, media
=None):
144 View to create a new collection
146 submit_form
= submit_forms
.AddCollectionForm(request
.form
)
148 if request
.method
== 'POST' and submit_form
.validate():
150 collection
= request
.db
.Collection()
152 collection
.title
= unicode(request
.form
['title'])
153 collection
.description
= unicode(request
.form
.get('description'))
154 collection
.creator
= request
.user
.id
155 collection
.generate_slug()
157 # Make sure this user isn't duplicating an existing collection
158 existing_collection
= request
.db
.Collection
.find_one({
159 'creator': request
.user
.id,
160 'title':collection
.title
})
162 if existing_collection
:
163 messages
.add_message(
164 request
, messages
.ERROR
, _('You already have a collection called "%s"!' % collection
.title
))
168 add_message(request
, SUCCESS
, _('Collection "%s" added!' % collection
.title
))
170 return redirect(request
, "mediagoblin.user_pages.user_home",
171 user
=request
.user
.username
)
173 except Exception as e
:
176 return render_to_response(
178 'mediagoblin/submit/collection.html',
179 {'submit_form': submit_form
,
180 'app_config': mg_globals
.app_config
})