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
19 from os
.path
import splitext
23 _log
= logging
.getLogger(__name__
)
25 from werkzeug
.datastructures
import FileStorage
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 handle_push_urls
, run_process_media
, \
40 def submit_start(request
):
42 First view for submitting a file.
44 submit_form
= submit_forms
.SubmitStartForm(request
.form
)
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.'))
54 filename
= request
.files
['file'].filename
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'])
61 # create entry and save in database
62 entry
= request
.db
.MediaEntry()
63 entry
.media_type
= unicode(media_type
)
65 unicode(request
.form
['title'])
66 or unicode(splitext(filename
)[0]))
68 entry
.description
= unicode(request
.form
.get('description'))
70 entry
.license
= unicode(request
.form
.get('license', "")) or None
72 entry
.uploader
= request
.user
.id
74 # Process the user's folksonomy "tags"
75 entry
.tags
= convert_to_tag_list_of_dicts(
76 request
.form
.get('tags'))
78 # Generate a slug from the title
81 queue_file
= prepare_queue_task(request
.app
, entry
, filename
)
84 queue_file
.write(request
.files
['file'].stream
.read())
86 # Save now so we have this data before kicking off processing
89 # Pass off to processing
91 # (... don't change entry after this point to avoid race
92 # conditions with changes to the document via processing code)
93 run_process_media(entry
)
95 handle_push_urls(request
)
97 add_message(request
, SUCCESS
, _('Woohoo! Submitted!'))
99 return redirect(request
, "mediagoblin.user_pages.user_home",
100 user
=request
.user
.username
)
101 except Exception as e
:
103 This section is intended to catch exceptions raised in
104 mediagoblin.media_types
106 if isinstance(e
, InvalidFileType
) or \
107 isinstance(e
, FileTypeNotSupported
):
108 submit_form
.file.errors
.append(
113 return render_to_response(
115 'mediagoblin/submit/start.html',
116 {'submit_form': submit_form
,
117 'app_config': mg_globals
.app_config
})
119 @require_active_login
120 def add_collection(request
, media
=None):
122 View to create a new collection
124 submit_form
= submit_forms
.AddCollectionForm(request
.form
)
126 if request
.method
== 'POST' and submit_form
.validate():
128 collection
= request
.db
.Collection()
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()
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
})
140 if existing_collection
:
141 messages
.add_message(
142 request
, messages
.ERROR
, _('You already have a collection called "%s"!' % collection
.title
))
146 add_message(request
, SUCCESS
, _('Collection "%s" added!' % collection
.title
))
148 return redirect(request
, "mediagoblin.user_pages.user_home",
149 user
=request
.user
.username
)
151 except Exception as e
:
154 return render_to_response(
156 'mediagoblin/submit/collection.html',
157 {'submit_form': submit_form
,
158 'app_config': mg_globals
.app_config
})