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__
)
26 from mediagoblin
.db
.models
import Collection
27 from mediagoblin
.tools
.federation
import create_activity
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
, user_has_privilege
31 from mediagoblin
.submit
import forms
as submit_forms
32 from mediagoblin
.messages
import add_message
, SUCCESS
33 from mediagoblin
.media_types
import FileTypeNotSupported
34 from mediagoblin
.submit
.lib
import \
35 check_file_field
, submit_media
, get_upload_file_limits
, \
36 FileUploadLimit
, UserUploadLimit
, UserPastUploadLimit
37 from mediagoblin
.user_pages
.lib
import add_media_to_collection
41 @user_has_privilege(u
'uploader')
42 def submit_start(request
):
44 First view for submitting a file.
46 upload_limit
, max_file_size
= get_upload_file_limits(request
.user
)
48 submit_form
= submit_forms
.get_submit_start_form(
50 license
=request
.user
.license_preference
,
51 max_file_size
=max_file_size
,
52 upload_limit
=upload_limit
,
53 uploaded
=request
.user
.uploaded
)
54 users_collections
= Collection
.query
.filter_by(
55 actor
=request
.user
.id,
56 type=Collection
.USER_DEFINED_TYPE
57 ).order_by(Collection
.title
)
59 # Only show the Collections dropdown if the user has some
61 if users_collections
.count() > 0:
62 submit_form
.collection
.query
= users_collections
64 del submit_form
.collection
66 if request
.method
== 'POST' and submit_form
.validate():
67 if not check_file_field(request
, 'file'):
68 submit_form
.file.errors
.append(
69 _(u
'You must provide a file.'))
73 mg_app
=request
.app
, user
=request
.user
,
74 submitted_file
=request
.files
['file'],
75 filename
=request
.files
['file'].filename
,
76 title
=six
.text_type(submit_form
.title
.data
),
77 description
=six
.text_type(submit_form
.description
.data
),
78 license
=six
.text_type(submit_form
.license
.data
) or None,
79 tags_string
=submit_form
.tags
.data
,
80 upload_limit
=upload_limit
, max_file_size
=max_file_size
,
81 urlgen
=request
.urlgen
)
83 if submit_form
.collection
and submit_form
.collection
.data
:
84 add_media_to_collection(
85 submit_form
.collection
.data
, media
)
87 "add", media
, request
.user
,
88 target
=submit_form
.collection
.data
)
90 add_message(request
, SUCCESS
, _('Woohoo! Submitted!'))
92 return redirect(request
, "mediagoblin.user_pages.user_home",
93 user
=request
.user
.username
)
96 # Handle upload limit issues
97 except FileUploadLimit
:
98 submit_form
.file.errors
.append(
99 _(u
'Sorry, the file size is too big.'))
100 except UserUploadLimit
:
101 submit_form
.file.errors
.append(
102 _('Sorry, uploading this file will put you over your'
104 except UserPastUploadLimit
:
105 messages
.add_message(
108 _('Sorry, you have reached your upload limit.'))
109 return redirect(request
, "mediagoblin.user_pages.user_home",
110 user
=request
.user
.username
)
111 except FileTypeNotSupported
as e
:
112 submit_form
.file.errors
.append(e
)
113 except Exception as e
:
116 return render_to_response(
118 'mediagoblin/submit/start.html',
119 {'submit_form': submit_form
,
120 'app_config': mg_globals
.app_config
})
123 @require_active_login
124 def add_collection(request
, media
=None):
126 View to create a new collection
128 submit_form
= submit_forms
.AddCollectionForm(request
.form
)
130 if request
.method
== 'POST' and submit_form
.validate():
131 collection
= request
.db
.Collection()
133 collection
.title
= six
.text_type(submit_form
.title
.data
)
134 collection
.description
= six
.text_type(submit_form
.description
.data
)
135 collection
.actor
= request
.user
.id
136 collection
.type = request
.db
.Collection
.USER_DEFINED_TYPE
137 collection
.generate_slug()
139 # Make sure this user isn't duplicating an existing collection
140 existing_collection
= request
.db
.Collection
.query
.filter_by(
141 actor
=request
.user
.id,
142 type=request
.db
.Collection
.USER_DEFINED_TYPE
,
143 title
=collection
.title
).first()
145 if existing_collection
:
146 add_message(request
, messages
.ERROR
,
147 _('You already have a collection called "%s"!') \
152 add_message(request
, SUCCESS
,
153 _('Collection "%s" added!') % collection
.title
)
155 return redirect(request
, "mediagoblin.user_pages.user_home",
156 user
=request
.user
.username
)
158 return render_to_response(
160 'mediagoblin/submit/collection.html',
161 {'submit_form': submit_form
,
162 'app_config': mg_globals
.app_config
})