| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011 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 | import uuid |
| 18 | |
| 19 | from webob import exc |
| 20 | from string import split |
| 21 | from cgi import FieldStorage |
| 22 | from datetime import datetime |
| 23 | |
| 24 | from werkzeug.utils import secure_filename |
| 25 | |
| 26 | from mediagoblin import messages |
| 27 | from mediagoblin import mg_globals |
| 28 | |
| 29 | from mediagoblin.edit import forms |
| 30 | from mediagoblin.edit.lib import may_edit_media |
| 31 | from mediagoblin.decorators import require_active_login, get_user_media_entry |
| 32 | from mediagoblin.tools.response import render_to_response, redirect |
| 33 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
| 34 | from mediagoblin.tools.text import ( |
| 35 | clean_html, convert_to_tag_list_of_dicts, |
| 36 | media_tags_as_string, cleaned_markdown_conversion) |
| 37 | |
| 38 | @get_user_media_entry |
| 39 | @require_active_login |
| 40 | def edit_media(request, media): |
| 41 | if not may_edit_media(request, media): |
| 42 | return exc.HTTPForbidden() |
| 43 | |
| 44 | defaults = dict( |
| 45 | title=media['title'], |
| 46 | slug=media['slug'], |
| 47 | description=media['description'], |
| 48 | tags=media_tags_as_string(media['tags'])) |
| 49 | |
| 50 | form = forms.EditForm( |
| 51 | request.POST, |
| 52 | **defaults) |
| 53 | |
| 54 | if request.method == 'POST' and form.validate(): |
| 55 | # Make sure there isn't already a MediaEntry with such a slug |
| 56 | # and userid. |
| 57 | existing_user_slug_entries = request.db.MediaEntry.find( |
| 58 | {'slug': request.POST['slug'], |
| 59 | 'uploader': media['uploader'], |
| 60 | '_id': {'$ne': media['_id']}}).count() |
| 61 | |
| 62 | if existing_user_slug_entries: |
| 63 | form.slug.errors.append( |
| 64 | _(u'An entry with that slug already exists for this user.')) |
| 65 | else: |
| 66 | media['title'] = unicode(request.POST['title']) |
| 67 | media['description'] = unicode(request.POST.get('description')) |
| 68 | media['tags'] = convert_to_tag_list_of_dicts( |
| 69 | request.POST.get('tags')) |
| 70 | |
| 71 | media['description_html'] = cleaned_markdown_conversion( |
| 72 | media['description']) |
| 73 | |
| 74 | media['slug'] = unicode(request.POST['slug']) |
| 75 | media.save() |
| 76 | |
| 77 | return exc.HTTPFound( |
| 78 | location=media.url_for_self(request.urlgen)) |
| 79 | |
| 80 | if request.user['is_admin'] \ |
| 81 | and media['uploader'] != request.user['_id'] \ |
| 82 | and request.method != 'POST': |
| 83 | messages.add_message( |
| 84 | request, messages.WARNING, |
| 85 | _("You are editing another user's media. Proceed with caution.")) |
| 86 | |
| 87 | return render_to_response( |
| 88 | request, |
| 89 | 'mediagoblin/edit/edit.html', |
| 90 | {'media': media, |
| 91 | 'form': form}) |
| 92 | |
| 93 | |
| 94 | @get_user_media_entry |
| 95 | @require_active_login |
| 96 | def edit_attachments(request, media): |
| 97 | if mg_globals.app_config['allow_attachments']: |
| 98 | form = forms.EditAttachmentsForm() |
| 99 | |
| 100 | # Add any attachements |
| 101 | if ('attachment_file' in request.POST |
| 102 | and isinstance(request.POST['attachment_file'], FieldStorage) |
| 103 | and request.POST['attachment_file'].file): |
| 104 | |
| 105 | attachment_public_filepath \ |
| 106 | = mg_globals.public_store.get_unique_filepath( |
| 107 | ['media_entries', unicode(media['_id']), 'attachment', |
| 108 | secure_filename(request.POST['attachment_file'].filename)]) |
| 109 | |
| 110 | attachment_public_file = mg_globals.public_store.get_file( |
| 111 | attachment_public_filepath, 'wb') |
| 112 | |
| 113 | try: |
| 114 | attachment_public_file.write( |
| 115 | request.POST['attachment_file'].file.read()) |
| 116 | finally: |
| 117 | request.POST['attachment_file'].file.close() |
| 118 | |
| 119 | media['attachment_files'].append(dict( |
| 120 | name=request.POST['attachment_name'] \ |
| 121 | or request.POST['attachment_file'].filename, |
| 122 | filepath=attachment_public_filepath, |
| 123 | created=datetime.utcnow() |
| 124 | )) |
| 125 | |
| 126 | media.save() |
| 127 | |
| 128 | messages.add_message( |
| 129 | request, messages.SUCCESS, |
| 130 | "You added the attachment %s!" \ |
| 131 | % (request.POST['attachment_name'] |
| 132 | or request.POST['attachment_file'].filename)) |
| 133 | |
| 134 | return exc.HTTPFound( |
| 135 | location=media.url_for_self(request.urlgen)) |
| 136 | return render_to_response( |
| 137 | request, |
| 138 | 'mediagoblin/edit/attachments.html', |
| 139 | {'media': media, |
| 140 | 'form': form}) |
| 141 | else: |
| 142 | return exc.HTTPForbidden() |
| 143 | |
| 144 | |
| 145 | @require_active_login |
| 146 | def edit_profile(request): |
| 147 | # admins may edit any user profile given a username in the querystring |
| 148 | edit_username = request.GET.get('username') |
| 149 | if request.user['is_admin'] and request.user['username'] != edit_username: |
| 150 | user = request.db.User.find_one({'username': edit_username}) |
| 151 | # No need to warn again if admin just submitted an edited profile |
| 152 | if request.method != 'POST': |
| 153 | messages.add_message( |
| 154 | request, messages.WARNING, |
| 155 | _("You are editing a user's profile. Proceed with caution.")) |
| 156 | else: |
| 157 | user = request.user |
| 158 | |
| 159 | form = forms.EditProfileForm(request.POST, |
| 160 | url=user.get('url'), |
| 161 | bio=user.get('bio')) |
| 162 | |
| 163 | if request.method == 'POST' and form.validate(): |
| 164 | user['url'] = unicode(request.POST['url']) |
| 165 | user['bio'] = unicode(request.POST['bio']) |
| 166 | |
| 167 | user['bio_html'] = cleaned_markdown_conversion(user['bio']) |
| 168 | |
| 169 | user.save() |
| 170 | |
| 171 | messages.add_message(request, |
| 172 | messages.SUCCESS, |
| 173 | _("Profile edited!")) |
| 174 | return redirect(request, |
| 175 | 'mediagoblin.user_pages.user_home', |
| 176 | user=edit_username) |
| 177 | |
| 178 | return render_to_response( |
| 179 | request, |
| 180 | 'mediagoblin/edit/edit_profile.html', |
| 181 | {'user': user, |
| 182 | 'form': form}) |