Commit | Line | Data |
---|---|---|
9bfe1d8e | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
12a100e4 | 2 | # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. |
9bfe1d8e E |
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/>. | |
aba81c9f | 16 | |
08750772 | 17 | import uuid |
aba81c9f | 18 | |
1c63ad5d | 19 | from webob import exc |
04a95150 | 20 | from string import split |
3a8c3a38 JW |
21 | from cgi import FieldStorage |
22 | from datetime import datetime | |
23 | ||
24 | from werkzeug.utils import secure_filename | |
aba81c9f | 25 | |
d9ed098e | 26 | from mediagoblin import messages |
10d7496d | 27 | from mediagoblin import mg_globals |
152a3bfa | 28 | |
aba81c9f | 29 | from mediagoblin.edit import forms |
0732236e | 30 | from mediagoblin.edit.lib import may_edit_media |
8cd5d4f8 | 31 | from mediagoblin.decorators import require_active_login, get_user_media_entry |
152a3bfa AW |
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) | |
c849e690 | 37 | |
8cd5d4f8 | 38 | @get_user_media_entry |
aba81c9f E |
39 | @require_active_login |
40 | def edit_media(request, media): | |
c849e690 E |
41 | if not may_edit_media(request, media): |
42 | return exc.HTTPForbidden() | |
43 | ||
2c437493 | 44 | defaults = dict( |
3a8c3a38 JW |
45 | title=media['title'], |
46 | slug=media['slug'], | |
47 | description=media['description'], | |
48 | tags=media_tags_as_string(media['tags'])) | |
aba81c9f | 49 | |
2c437493 JW |
50 | form = forms.EditForm( |
51 | request.POST, | |
52 | **defaults) | |
53 | ||
98857207 | 54 | if request.method == 'POST' and form.validate(): |
d5e90fe4 CAW |
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() | |
3a8c3a38 | 61 | |
d5e90fe4 CAW |
62 | if existing_user_slug_entries: |
63 | form.slug.errors.append( | |
4b1adc13 | 64 | _(u'An entry with that slug already exists for this user.')) |
d5e90fe4 | 65 | else: |
08750772 MH |
66 | media['title'] = unicode(request.POST['title']) |
67 | media['description'] = unicode(request.POST.get('description')) | |
0712a06d CFD |
68 | media['tags'] = convert_to_tag_list_of_dicts( |
69 | request.POST.get('tags')) | |
3a8c3a38 | 70 | |
0d91af09 CAW |
71 | media['description_html'] = cleaned_markdown_conversion( |
72 | media['description']) | |
44e2da2f | 73 | |
08750772 | 74 | media['slug'] = unicode(request.POST['slug']) |
747623cc | 75 | media.save() |
d5e90fe4 | 76 | |
8d7b549b E |
77 | return exc.HTTPFound( |
78 | location=media.url_for_self(request.urlgen)) | |
98857207 | 79 | |
96a2c366 CAW |
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, | |
4b1adc13 | 85 | _("You are editing another user's media. Proceed with caution.")) |
96a2c366 | 86 | |
9038c9f9 CAW |
87 | return render_to_response( |
88 | request, | |
c9c24934 E |
89 | 'mediagoblin/edit/edit.html', |
90 | {'media': media, | |
91 | 'form': form}) | |
46fd661e | 92 | |
3a8c3a38 JW |
93 | |
94 | @get_user_media_entry | |
630b57a3 | 95 | @require_active_login |
3a8c3a38 JW |
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 | )) | |
630b57a3 | 125 | |
3a8c3a38 JW |
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 | ||
8d7b549b E |
134 | return exc.HTTPFound( |
135 | location=media.url_for_self(request.urlgen)) | |
3a8c3a38 JW |
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): | |
a0cf14fe CFD |
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, | |
4b1adc13 | 155 | _("You are editing a user's profile. Proceed with caution.")) |
a0cf14fe CFD |
156 | else: |
157 | user = request.user | |
158 | ||
630b57a3 | 159 | form = forms.EditProfileForm(request.POST, |
3a8c3a38 JW |
160 | url=user.get('url'), |
161 | bio=user.get('bio')) | |
630b57a3 | 162 | |
163 | if request.method == 'POST' and form.validate(): | |
08750772 MH |
164 | user['url'] = unicode(request.POST['url']) |
165 | user['bio'] = unicode(request.POST['bio']) | |
4c465852 | 166 | |
0d91af09 | 167 | user['bio_html'] = cleaned_markdown_conversion(user['bio']) |
4c465852 | 168 | |
630b57a3 | 169 | user.save() |
170 | ||
3a8c3a38 JW |
171 | messages.add_message(request, |
172 | messages.SUCCESS, | |
efd0a42c | 173 | _("Profile edited!")) |
02542d54 CFD |
174 | return redirect(request, |
175 | 'mediagoblin.user_pages.user_home', | |
3a8c3a38 | 176 | user=edit_username) |
630b57a3 | 177 | |
178 | return render_to_response( | |
179 | request, | |
180 | 'mediagoblin/edit/edit_profile.html', | |
181 | {'user': user, | |
182 | 'form': form}) |