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 | |
5f60a455 | 26 | from mediagoblin.db.models import Collection |
27 | from mediagoblin.tools.federation import create_activity | |
a789b713 | 28 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
152a3bfa | 29 | from mediagoblin.tools.response import render_to_response, redirect |
3fb96fc9 | 30 | from mediagoblin.decorators import require_active_login, user_has_privilege |
32d8cf45 | 31 | from mediagoblin.submit import forms as submit_forms |
54b4b28f | 32 | from mediagoblin.media_types import FileTypeNotSupported |
5d754da7 CAW |
33 | from mediagoblin.submit.lib import \ |
34 | check_file_field, submit_media, get_upload_file_limits, \ | |
9e15c674 | 35 | FileUploadLimit, UserUploadLimit, UserPastUploadLimit |
5f60a455 | 36 | from mediagoblin.user_pages.lib import add_media_to_collection |
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) |
5f60a455 | 53 | users_collections = Collection.query.filter_by( |
54 | actor=request.user.id, | |
55 | type=Collection.USER_DEFINED_TYPE | |
56 | ).order_by(Collection.title) | |
57 | ||
9bc8c870 CAW |
58 | # Only show the Collections dropdown if the user has some |
59 | # collections set up | |
5f60a455 | 60 | if users_collections.count() > 0: |
61 | submit_form.collection.query = users_collections | |
62 | else: | |
63 | del submit_form.collection | |
e323a068 | 64 | |
f6f524bf | 65 | if request.method == 'POST' and submit_form.validate(): |
2ef2f46e | 66 | if not check_file_field(request, 'file'): |
03afc828 | 67 | submit_form.file.errors.append( |
4b1adc13 | 68 | _(u'You must provide a file.')) |
03afc828 | 69 | else: |
6788b412 | 70 | try: |
5f60a455 | 71 | media = submit_media( |
70b2f1ec CAW |
72 | mg_app=request.app, user=request.user, |
73 | submitted_file=request.files['file'], | |
74 | filename=request.files['file'].filename, | |
e49b7e02 BP |
75 | title=six.text_type(submit_form.title.data), |
76 | description=six.text_type(submit_form.description.data), | |
77 | license=six.text_type(submit_form.license.data) or None, | |
70b2f1ec | 78 | tags_string=submit_form.tags.data, |
c802c41a | 79 | urlgen=request.urlgen) |
9e15c674 | 80 | |
5f60a455 | 81 | if submit_form.collection and submit_form.collection.data: |
82 | add_media_to_collection( | |
83 | submit_form.collection.data, media) | |
84 | create_activity( | |
85 | "add", media, request.user, | |
86 | target=submit_form.collection.data) | |
87 | ||
5c7b2a63 AB |
88 | messages.add_message( |
89 | request, | |
90 | messages.SUCCESS, | |
91 | _('Woohoo! Submitted!')) | |
9e15c674 CAW |
92 | |
93 | return redirect(request, "mediagoblin.user_pages.user_home", | |
5d754da7 | 94 | user=request.user.username) |
9e15c674 CAW |
95 | |
96 | ||
97 | # Handle upload limit issues | |
98 | except FileUploadLimit: | |
99 | submit_form.file.errors.append( | |
100 | _(u'Sorry, the file size is too big.')) | |
101 | except UserUploadLimit: | |
102 | submit_form.file.errors.append( | |
103 | _('Sorry, uploading this file will put you over your' | |
104 | ' upload limit.')) | |
105 | except UserPastUploadLimit: | |
106 | messages.add_message( | |
107 | request, | |
108 | messages.WARNING, | |
109 | _('Sorry, you have reached your upload limit.')) | |
110 | return redirect(request, "mediagoblin.user_pages.user_home", | |
111 | user=request.user.username) | |
54b4b28f BB |
112 | except FileTypeNotSupported as e: |
113 | submit_form.file.errors.append(e) | |
a246ccca | 114 | except Exception as e: |
54b4b28f | 115 | raise |
f6f524bf | 116 | |
9038c9f9 CAW |
117 | return render_to_response( |
118 | request, | |
c9c24934 | 119 | 'mediagoblin/submit/start.html', |
2c437493 JW |
120 | {'submit_form': submit_form, |
121 | 'app_config': mg_globals.app_config}) | |
be5be115 | 122 | |
2041ceae | 123 | |
be5be115 AW |
124 | @require_active_login |
125 | def add_collection(request, media=None): | |
126 | """ | |
127 | View to create a new collection | |
128 | """ | |
111a609d | 129 | submit_form = submit_forms.AddCollectionForm(request.form) |
be5be115 AW |
130 | |
131 | if request.method == 'POST' and submit_form.validate(): | |
adf53036 E |
132 | collection = request.db.Collection() |
133 | ||
e49b7e02 BP |
134 | collection.title = six.text_type(submit_form.title.data) |
135 | collection.description = six.text_type(submit_form.description.data) | |
17eb7786 DK |
136 | collection.actor = request.user.id |
137 | collection.type = request.db.Collection.USER_DEFINED_TYPE | |
adf53036 E |
138 | collection.generate_slug() |
139 | ||
140 | # Make sure this user isn't duplicating an existing collection | |
44082b12 | 141 | existing_collection = request.db.Collection.query.filter_by( |
17eb7786 DK |
142 | actor=request.user.id, |
143 | type=request.db.Collection.USER_DEFINED_TYPE, | |
44082b12 | 144 | title=collection.title).first() |
adf53036 E |
145 | |
146 | if existing_collection: | |
5c7b2a63 AB |
147 | messages.add_message( |
148 | request, | |
149 | messages.ERROR, | |
150 | _('You already have a collection called "%s"!') % | |
151 | collection.title) | |
adf53036 E |
152 | else: |
153 | collection.save() | |
154 | ||
5c7b2a63 AB |
155 | messages.add_message( |
156 | request, | |
157 | messages.SUCCESS, | |
adf53036 E |
158 | _('Collection "%s" added!') % collection.title) |
159 | ||
160 | return redirect(request, "mediagoblin.user_pages.user_home", | |
161 | user=request.user.username) | |
be5be115 | 162 | |
be5be115 AW |
163 | return render_to_response( |
164 | request, | |
165 | 'mediagoblin/submit/collection.html', | |
166 | {'submit_form': submit_form, | |
167 | 'app_config': mg_globals.app_config}) |