| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
| 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 | |
| 17 | from mediagoblin import messages |
| 18 | import mediagoblin.mg_globals as mg_globals |
| 19 | |
| 20 | import logging |
| 21 | |
| 22 | _log = logging.getLogger(__name__) |
| 23 | |
| 24 | |
| 25 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
| 26 | from mediagoblin.tools.response import render_to_response, redirect |
| 27 | from mediagoblin.decorators import require_active_login, user_has_privilege |
| 28 | from mediagoblin.submit import forms as submit_forms |
| 29 | from mediagoblin.messages import add_message, SUCCESS |
| 30 | from mediagoblin.media_types import \ |
| 31 | InvalidFileType, FileTypeNotSupported |
| 32 | from mediagoblin.submit.lib import \ |
| 33 | check_file_field, submit_media, get_upload_file_limits, \ |
| 34 | FileUploadLimit, UserUploadLimit, UserPastUploadLimit |
| 35 | |
| 36 | |
| 37 | @require_active_login |
| 38 | @user_has_privilege(u'uploader') |
| 39 | def submit_start(request): |
| 40 | """ |
| 41 | First view for submitting a file. |
| 42 | """ |
| 43 | upload_limit, max_file_size = get_upload_file_limits(request.user) |
| 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, |
| 50 | uploaded=request.user.uploaded) |
| 51 | |
| 52 | if request.method == 'POST' and submit_form.validate(): |
| 53 | if not check_file_field(request, 'file'): |
| 54 | submit_form.file.errors.append( |
| 55 | _(u'You must provide a file.')) |
| 56 | else: |
| 57 | try: |
| 58 | submit_media( |
| 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, |
| 67 | urlgen=request.urlgen) |
| 68 | |
| 69 | add_message(request, SUCCESS, _('Woohoo! Submitted!')) |
| 70 | |
| 71 | return redirect(request, "mediagoblin.user_pages.user_home", |
| 72 | user=request.user.username) |
| 73 | |
| 74 | |
| 75 | # Handle upload limit issues |
| 76 | except FileUploadLimit: |
| 77 | submit_form.file.errors.append( |
| 78 | _(u'Sorry, the file size is too big.')) |
| 79 | except UserUploadLimit: |
| 80 | submit_form.file.errors.append( |
| 81 | _('Sorry, uploading this file will put you over your' |
| 82 | ' upload limit.')) |
| 83 | except UserPastUploadLimit: |
| 84 | messages.add_message( |
| 85 | request, |
| 86 | messages.WARNING, |
| 87 | _('Sorry, you have reached your upload limit.')) |
| 88 | return redirect(request, "mediagoblin.user_pages.user_home", |
| 89 | user=request.user.username) |
| 90 | |
| 91 | except Exception as e: |
| 92 | ''' |
| 93 | This section is intended to catch exceptions raised in |
| 94 | mediagoblin.media_types |
| 95 | ''' |
| 96 | if isinstance(e, InvalidFileType) or \ |
| 97 | isinstance(e, FileTypeNotSupported): |
| 98 | submit_form.file.errors.append( |
| 99 | e) |
| 100 | else: |
| 101 | raise |
| 102 | |
| 103 | return render_to_response( |
| 104 | request, |
| 105 | 'mediagoblin/submit/start.html', |
| 106 | {'submit_form': submit_form, |
| 107 | 'app_config': mg_globals.app_config}) |
| 108 | |
| 109 | |
| 110 | @require_active_login |
| 111 | def add_collection(request, media=None): |
| 112 | """ |
| 113 | View to create a new collection |
| 114 | """ |
| 115 | submit_form = submit_forms.AddCollectionForm(request.form) |
| 116 | |
| 117 | if request.method == 'POST' and submit_form.validate(): |
| 118 | collection = request.db.Collection() |
| 119 | |
| 120 | collection.title = unicode(submit_form.title.data) |
| 121 | collection.description = unicode(submit_form.description.data) |
| 122 | collection.creator = request.user.id |
| 123 | collection.generate_slug() |
| 124 | |
| 125 | # Make sure this user isn't duplicating an existing collection |
| 126 | existing_collection = request.db.Collection.query.filter_by( |
| 127 | creator=request.user.id, |
| 128 | title=collection.title).first() |
| 129 | |
| 130 | if existing_collection: |
| 131 | add_message(request, messages.ERROR, |
| 132 | _('You already have a collection called "%s"!') \ |
| 133 | % collection.title) |
| 134 | else: |
| 135 | collection.save() |
| 136 | |
| 137 | add_message(request, SUCCESS, |
| 138 | _('Collection "%s" added!') % collection.title) |
| 139 | |
| 140 | return redirect(request, "mediagoblin.user_pages.user_home", |
| 141 | user=request.user.username) |
| 142 | |
| 143 | return render_to_response( |
| 144 | request, |
| 145 | 'mediagoblin/submit/collection.html', |
| 146 | {'submit_form': submit_form, |
| 147 | 'app_config': mg_globals.app_config}) |