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 |
bb49e56f | 19 | from os.path import splitext |
03afc828 | 20 | |
77b91efc | 21 | import logging |
8e5f9746 | 22 | |
c03d13cd | 23 | _log = logging.getLogger(__name__) |
8e5f9746 | 24 | |
e323a068 | 25 | |
1e72e075 | 26 | from mediagoblin.tools.text import convert_to_tag_list_of_dicts |
a789b713 | 27 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
152a3bfa | 28 | from mediagoblin.tools.response import render_to_response, redirect |
e323a068 | 29 | from mediagoblin.decorators import require_active_login |
32d8cf45 | 30 | from mediagoblin.submit import forms as submit_forms |
4dc74441 | 31 | from mediagoblin.messages import add_message, SUCCESS |
ec4261a4 | 32 | from mediagoblin.media_types import sniff_media, \ |
4601c30c | 33 | InvalidFileType, FileTypeNotSupported |
2ef2f46e | 34 | from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \ |
6c1467d5 | 35 | run_process_media, new_upload_entry |
e323a068 CAW |
36 | |
37 | ||
38 | @require_active_login | |
39 | def submit_start(request): | |
40 | """ | |
41 | First view for submitting a file. | |
42 | """ | |
dc4dfbde | 43 | submit_form = submit_forms.SubmitStartForm(request.form, |
066d49b2 | 44 | license=request.user.license_preference) |
e323a068 | 45 | |
f6f524bf | 46 | if request.method == 'POST' and submit_form.validate(): |
2ef2f46e | 47 | if not check_file_field(request, 'file'): |
03afc828 | 48 | submit_form.file.errors.append( |
4b1adc13 | 49 | _(u'You must provide a file.')) |
03afc828 | 50 | else: |
6788b412 | 51 | try: |
f1d06e1d | 52 | filename = request.files['file'].filename |
ec4261a4 JW |
53 | |
54 | # Sniff the submitted media to determine which | |
55 | # media plugin should handle processing | |
56 | media_type, media_manager = sniff_media( | |
f1d06e1d | 57 | request.files['file']) |
bb49e56f | 58 | |
0bce749b | 59 | # create entry and save in database |
6c1467d5 | 60 | entry = new_upload_entry(request.user) |
f4ee8399 | 61 | entry.media_type = unicode(media_type) |
ec82fbd8 | 62 | entry.title = ( |
c5673a13 | 63 | unicode(submit_form.title.data) |
0bce749b | 64 | or unicode(splitext(filename)[0])) |
93bdab9d | 65 | |
c5673a13 | 66 | entry.description = unicode(submit_form.description.data) |
4bf8e888 | 67 | |
c5673a13 | 68 | entry.license = unicode(submit_form.license.data) or None |
99a270e9 | 69 | |
0bce749b | 70 | # Process the user's folksonomy "tags" |
de917303 | 71 | entry.tags = convert_to_tag_list_of_dicts( |
c5673a13 | 72 | submit_form.tags.data) |
03afc828 | 73 | |
0bce749b JW |
74 | # Generate a slug from the title |
75 | entry.generate_slug() | |
4a477e24 | 76 | |
b228d897 | 77 | queue_file = prepare_queue_task(request.app, entry, filename) |
03afc828 | 78 | |
0bce749b | 79 | with queue_file: |
f1d06e1d | 80 | queue_file.write(request.files['file'].stream.read()) |
03afc828 | 81 | |
0bce749b | 82 | # Save now so we have this data before kicking off processing |
b39d1f23 | 83 | entry.save() |
fa7f9c61 | 84 | |
0bce749b | 85 | # Pass off to processing |
6788b412 | 86 | # |
0bce749b JW |
87 | # (... don't change entry after this point to avoid race |
88 | # conditions with changes to the document via processing code) | |
c7b3d070 SS |
89 | feed_url = request.urlgen( |
90 | 'mediagoblin.user_pages.atom_feed', | |
91 | qualified=True, user=request.user.username) | |
92 | run_process_media(entry, feed_url) | |
0bce749b JW |
93 | add_message(request, SUCCESS, _('Woohoo! Submitted!')) |
94 | ||
95 | return redirect(request, "mediagoblin.user_pages.user_home", | |
5a4e3ff1 | 96 | user=request.user.username) |
a246ccca JW |
97 | except Exception as e: |
98 | ''' | |
deea3f66 | 99 | This section is intended to catch exceptions raised in |
7a258b14 | 100 | mediagoblin.media_types |
a246ccca | 101 | ''' |
4601c30c JW |
102 | if isinstance(e, InvalidFileType) or \ |
103 | isinstance(e, FileTypeNotSupported): | |
104 | submit_form.file.errors.append( | |
105 | e) | |
106 | else: | |
107 | raise | |
f6f524bf | 108 | |
9038c9f9 CAW |
109 | return render_to_response( |
110 | request, | |
c9c24934 | 111 | 'mediagoblin/submit/start.html', |
2c437493 JW |
112 | {'submit_form': submit_form, |
113 | 'app_config': mg_globals.app_config}) | |
be5be115 | 114 | |
2041ceae | 115 | |
be5be115 AW |
116 | @require_active_login |
117 | def add_collection(request, media=None): | |
118 | """ | |
119 | View to create a new collection | |
120 | """ | |
111a609d | 121 | submit_form = submit_forms.AddCollectionForm(request.form) |
be5be115 AW |
122 | |
123 | if request.method == 'POST' and submit_form.validate(): | |
adf53036 E |
124 | collection = request.db.Collection() |
125 | ||
126 | collection.title = unicode(submit_form.title.data) | |
127 | collection.description = unicode(submit_form.description.data) | |
128 | collection.creator = request.user.id | |
129 | collection.generate_slug() | |
130 | ||
131 | # Make sure this user isn't duplicating an existing collection | |
132 | existing_collection = request.db.Collection.find_one({ | |
133 | 'creator': request.user.id, | |
134 | 'title':collection.title}) | |
135 | ||
136 | if existing_collection: | |
137 | add_message(request, messages.ERROR, | |
138 | _('You already have a collection called "%s"!') \ | |
139 | % collection.title) | |
140 | else: | |
141 | collection.save() | |
142 | ||
143 | add_message(request, SUCCESS, | |
144 | _('Collection "%s" added!') % collection.title) | |
145 | ||
146 | return redirect(request, "mediagoblin.user_pages.user_home", | |
147 | user=request.user.username) | |
be5be115 | 148 | |
be5be115 AW |
149 | return render_to_response( |
150 | request, | |
151 | 'mediagoblin/submit/collection.html', | |
152 | {'submit_form': submit_form, | |
153 | 'app_config': mg_globals.app_config}) |