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