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/>.
19 from mediagoblin
import messages
20 import mediagoblin
.mg_globals
as mg_globals
24 _log
= logging
.getLogger(__name__
)
27 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
28 from mediagoblin
.tools
.response
import render_to_response
, redirect
29 from mediagoblin
.decorators
import require_active_login
, user_has_privilege
30 from mediagoblin
.submit
import forms
as submit_forms
31 from mediagoblin
.messages
import add_message
, SUCCESS
32 from mediagoblin
.media_types
import FileTypeNotSupported
33 from mediagoblin
.submit
.lib
import \
34 check_file_field
, submit_media
, get_upload_file_limits
, \
35 FileUploadLimit
, UserUploadLimit
, UserPastUploadLimit
39 @user_has_privilege(u
'uploader')
40 def submit_start(request
):
42 First view for submitting a file.
44 upload_limit
, max_file_size
= get_upload_file_limits(request
.user
)
46 submit_form
= submit_forms
.get_submit_start_form(
48 license
=request
.user
.license_preference
,
49 max_file_size
=max_file_size
,
50 upload_limit
=upload_limit
,
51 uploaded
=request
.user
.uploaded
)
53 if request
.method
== 'POST' and submit_form
.validate():
54 if not check_file_field(request
, 'file'):
55 submit_form
.file.errors
.append(
56 _(u
'You must provide a file.'))
60 mg_app
=request
.app
, user
=request
.user
,
61 submitted_file
=request
.files
['file'],
62 filename
=request
.files
['file'].filename
,
63 title
=six
.text_type(submit_form
.title
.data
),
64 description
=six
.text_type(submit_form
.description
.data
),
65 license
=six
.text_type(submit_form
.license
.data
) or None,
66 tags_string
=submit_form
.tags
.data
,
67 upload_limit
=upload_limit
, max_file_size
=max_file_size
,
68 urlgen
=request
.urlgen
)
70 add_message(request
, SUCCESS
, _('Woohoo! Submitted!'))
72 return redirect(request
, "mediagoblin.user_pages.user_home",
73 user
=request
.user
.username
)
76 # Handle upload limit issues
77 except FileUploadLimit
:
78 submit_form
.file.errors
.append(
79 _(u
'Sorry, the file size is too big.'))
80 except UserUploadLimit
:
81 submit_form
.file.errors
.append(
82 _('Sorry, uploading this file will put you over your'
84 except UserPastUploadLimit
:
88 _('Sorry, you have reached your upload limit.'))
89 return redirect(request
, "mediagoblin.user_pages.user_home",
90 user
=request
.user
.username
)
91 except FileTypeNotSupported
as e
:
92 submit_form
.file.errors
.append(e
)
93 except Exception as e
:
96 return render_to_response(
98 'mediagoblin/submit/start.html',
99 {'submit_form': submit_form
,
100 'app_config': mg_globals
.app_config
})
103 @require_active_login
104 def add_collection(request
, media
=None):
106 View to create a new collection
108 submit_form
= submit_forms
.AddCollectionForm(request
.form
)
110 if request
.method
== 'POST' and submit_form
.validate():
111 collection
= request
.db
.Collection()
113 collection
.title
= six
.text_type(submit_form
.title
.data
)
114 collection
.description
= six
.text_type(submit_form
.description
.data
)
115 collection
.creator
= request
.user
.id
116 collection
.generate_slug()
118 # Make sure this user isn't duplicating an existing collection
119 existing_collection
= request
.db
.Collection
.query
.filter_by(
120 creator
=request
.user
.id,
121 title
=collection
.title
).first()
123 if existing_collection
:
124 add_message(request
, messages
.ERROR
,
125 _('You already have a collection called "%s"!') \
130 add_message(request
, SUCCESS
,
131 _('Collection "%s" added!') % collection
.title
)
133 return redirect(request
, "mediagoblin.user_pages.user_home",
134 user
=request
.user
.username
)
136 return render_to_response(
138 'mediagoblin/submit/collection.html',
139 {'submit_form': submit_form
,
140 'app_config': mg_globals
.app_config
})