Commit | Line | Data |
---|---|---|
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 | ||
e49b7e02 BP |
17 | import six |
18 | ||
be5be115 | 19 | from mediagoblin import messages |
2c437493 | 20 | import mediagoblin.mg_globals as mg_globals |
03afc828 | 21 | |
77b91efc | 22 | import logging |
8e5f9746 | 23 | |
c03d13cd | 24 | _log = logging.getLogger(__name__) |
8e5f9746 | 25 | |
e323a068 | 26 | |
a789b713 | 27 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
152a3bfa | 28 | from mediagoblin.tools.response import render_to_response, redirect |
3fb96fc9 | 29 | from mediagoblin.decorators import require_active_login, user_has_privilege |
32d8cf45 | 30 | from mediagoblin.submit import forms as submit_forms |
4dc74441 | 31 | from mediagoblin.messages import add_message, SUCCESS |
9e15c674 | 32 | from mediagoblin.media_types import \ |
4601c30c | 33 | InvalidFileType, FileTypeNotSupported |
5d754da7 CAW |
34 | from mediagoblin.submit.lib import \ |
35 | check_file_field, submit_media, get_upload_file_limits, \ | |
9e15c674 | 36 | FileUploadLimit, UserUploadLimit, UserPastUploadLimit |
2d7b6bde | 37 | |
e323a068 CAW |
38 | |
39 | @require_active_login | |
3fb96fc9 | 40 | @user_has_privilege(u'uploader') |
e323a068 CAW |
41 | def submit_start(request): |
42 | """ | |
43 | First view for submitting a file. | |
44 | """ | |
5d754da7 | 45 | upload_limit, max_file_size = get_upload_file_limits(request.user) |
2188925b RE |
46 | |
47 | submit_form = submit_forms.get_submit_start_form( | |
48 | request.form, | |
49 | license=request.user.license_preference, | |
50 | max_file_size=max_file_size, | |
51 | upload_limit=upload_limit, | |
5d754da7 | 52 | uploaded=request.user.uploaded) |
e323a068 | 53 | |
f6f524bf | 54 | if request.method == 'POST' and submit_form.validate(): |
2ef2f46e | 55 | if not check_file_field(request, 'file'): |
03afc828 | 56 | submit_form.file.errors.append( |
4b1adc13 | 57 | _(u'You must provide a file.')) |
03afc828 | 58 | else: |
6788b412 | 59 | try: |
9e15c674 | 60 | submit_media( |
70b2f1ec CAW |
61 | mg_app=request.app, user=request.user, |
62 | submitted_file=request.files['file'], | |
63 | filename=request.files['file'].filename, | |
e49b7e02 BP |
64 | title=six.text_type(submit_form.title.data), |
65 | description=six.text_type(submit_form.description.data), | |
66 | license=six.text_type(submit_form.license.data) or None, | |
70b2f1ec | 67 | tags_string=submit_form.tags.data, |
c802c41a CAW |
68 | upload_limit=upload_limit, max_file_size=max_file_size, |
69 | urlgen=request.urlgen) | |
9e15c674 CAW |
70 | |
71 | add_message(request, SUCCESS, _('Woohoo! Submitted!')) | |
72 | ||
73 | return redirect(request, "mediagoblin.user_pages.user_home", | |
5d754da7 | 74 | user=request.user.username) |
9e15c674 CAW |
75 | |
76 | ||
77 | # Handle upload limit issues | |
78 | except FileUploadLimit: | |
79 | submit_form.file.errors.append( | |
80 | _(u'Sorry, the file size is too big.')) | |
81 | except UserUploadLimit: | |
82 | submit_form.file.errors.append( | |
83 | _('Sorry, uploading this file will put you over your' | |
84 | ' upload limit.')) | |
85 | except UserPastUploadLimit: | |
86 | messages.add_message( | |
87 | request, | |
88 | messages.WARNING, | |
89 | _('Sorry, you have reached your upload limit.')) | |
90 | return redirect(request, "mediagoblin.user_pages.user_home", | |
91 | user=request.user.username) | |
2d7b6bde | 92 | |
a246ccca JW |
93 | except Exception as e: |
94 | ''' | |
deea3f66 | 95 | This section is intended to catch exceptions raised in |
7a258b14 | 96 | mediagoblin.media_types |
a246ccca | 97 | ''' |
4601c30c JW |
98 | if isinstance(e, InvalidFileType) or \ |
99 | isinstance(e, FileTypeNotSupported): | |
100 | submit_form.file.errors.append( | |
101 | e) | |
102 | else: | |
103 | raise | |
f6f524bf | 104 | |
9038c9f9 CAW |
105 | return render_to_response( |
106 | request, | |
c9c24934 | 107 | 'mediagoblin/submit/start.html', |
2c437493 JW |
108 | {'submit_form': submit_form, |
109 | 'app_config': mg_globals.app_config}) | |
be5be115 | 110 | |
2041ceae | 111 | |
be5be115 AW |
112 | @require_active_login |
113 | def add_collection(request, media=None): | |
114 | """ | |
115 | View to create a new collection | |
116 | """ | |
111a609d | 117 | submit_form = submit_forms.AddCollectionForm(request.form) |
be5be115 AW |
118 | |
119 | if request.method == 'POST' and submit_form.validate(): | |
adf53036 E |
120 | collection = request.db.Collection() |
121 | ||
e49b7e02 BP |
122 | collection.title = six.text_type(submit_form.title.data) |
123 | collection.description = six.text_type(submit_form.description.data) | |
adf53036 E |
124 | collection.creator = request.user.id |
125 | collection.generate_slug() | |
126 | ||
127 | # Make sure this user isn't duplicating an existing collection | |
44082b12 RE |
128 | existing_collection = request.db.Collection.query.filter_by( |
129 | creator=request.user.id, | |
130 | title=collection.title).first() | |
adf53036 E |
131 | |
132 | if existing_collection: | |
133 | add_message(request, messages.ERROR, | |
134 | _('You already have a collection called "%s"!') \ | |
135 | % collection.title) | |
136 | else: | |
137 | collection.save() | |
138 | ||
139 | add_message(request, SUCCESS, | |
140 | _('Collection "%s" added!') % collection.title) | |
141 | ||
142 | return redirect(request, "mediagoblin.user_pages.user_home", | |
143 | user=request.user.username) | |
be5be115 | 144 | |
be5be115 AW |
145 | return render_to_response( |
146 | request, | |
147 | 'mediagoblin/submit/collection.html', | |
148 | {'submit_form': submit_form, | |
149 | 'app_config': mg_globals.app_config}) |