Drop pre-rendered html: MediaEntry.description_html
[mediagoblin.git] / mediagoblin / edit / views.py
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 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.auth import lib as auth_lib
30 from mediagoblin.edit import forms
31 from mediagoblin.edit.lib import may_edit_media
32 from mediagoblin.decorators import require_active_login, get_user_media_entry
33 from mediagoblin.tools.response import render_to_response, redirect
34 from mediagoblin.tools.translate import pass_to_ugettext as _
35 from mediagoblin.tools.text import (
36 clean_html, convert_to_tag_list_of_dicts,
37 media_tags_as_string)
38 from mediagoblin.tools.licenses import SUPPORTED_LICENSES
39
40
41 @get_user_media_entry
42 @require_active_login
43 def edit_media(request, media):
44 if not may_edit_media(request, media):
45 return exc.HTTPForbidden()
46
47 defaults = dict(
48 title=media.title,
49 slug=media.slug,
50 description=media.description,
51 tags=media_tags_as_string(media.tags),
52 license=media.license)
53
54 form = forms.EditForm(
55 request.POST,
56 **defaults)
57
58 if request.method == 'POST' and form.validate():
59 # Make sure there isn't already a MediaEntry with such a slug
60 # and userid.
61 existing_user_slug_entries = request.db.MediaEntry.find(
62 {'slug': request.POST['slug'],
63 'uploader': media.uploader,
64 '_id': {'$ne': media._id}}).count()
65
66 if existing_user_slug_entries:
67 form.slug.errors.append(
68 _(u'An entry with that slug already exists for this user.'))
69 else:
70 media.title = unicode(request.POST['title'])
71 media.description = unicode(request.POST.get('description'))
72 media.tags = convert_to_tag_list_of_dicts(
73 request.POST.get('tags'))
74
75 media.license = unicode(request.POST.get('license', '')) or None
76
77 media.slug = unicode(request.POST['slug'])
78
79 media.save()
80
81 return exc.HTTPFound(
82 location=media.url_for_self(request.urlgen))
83
84 if request.user.is_admin \
85 and media.uploader != request.user._id \
86 and request.method != 'POST':
87 messages.add_message(
88 request, messages.WARNING,
89 _("You are editing another user's media. Proceed with caution."))
90
91 return render_to_response(
92 request,
93 'mediagoblin/edit/edit.html',
94 {'media': media,
95 'form': form})
96
97
98 @get_user_media_entry
99 @require_active_login
100 def edit_attachments(request, media):
101 if mg_globals.app_config['allow_attachments']:
102 form = forms.EditAttachmentsForm()
103
104 # Add any attachements
105 if ('attachment_file' in request.POST
106 and isinstance(request.POST['attachment_file'], FieldStorage)
107 and request.POST['attachment_file'].file):
108
109 attachment_public_filepath \
110 = mg_globals.public_store.get_unique_filepath(
111 ['media_entries', unicode(media._id), 'attachment',
112 secure_filename(request.POST['attachment_file'].filename)])
113
114 attachment_public_file = mg_globals.public_store.get_file(
115 attachment_public_filepath, 'wb')
116
117 try:
118 attachment_public_file.write(
119 request.POST['attachment_file'].file.read())
120 finally:
121 request.POST['attachment_file'].file.close()
122
123 media['attachment_files'].append(dict(
124 name=request.POST['attachment_name'] \
125 or request.POST['attachment_file'].filename,
126 filepath=attachment_public_filepath,
127 created=datetime.utcnow(),
128 ))
129
130 media.save()
131
132 messages.add_message(
133 request, messages.SUCCESS,
134 "You added the attachment %s!" \
135 % (request.POST['attachment_name']
136 or request.POST['attachment_file'].filename))
137
138 return exc.HTTPFound(
139 location=media.url_for_self(request.urlgen))
140 return render_to_response(
141 request,
142 'mediagoblin/edit/attachments.html',
143 {'media': media,
144 'form': form})
145 else:
146 return exc.HTTPForbidden()
147
148
149 @require_active_login
150 def edit_profile(request):
151 # admins may edit any user profile given a username in the querystring
152 edit_username = request.GET.get('username')
153 if request.user.is_admin and request.user.username != edit_username:
154 user = request.db.User.find_one({'username': edit_username})
155 # No need to warn again if admin just submitted an edited profile
156 if request.method != 'POST':
157 messages.add_message(
158 request, messages.WARNING,
159 _("You are editing a user's profile. Proceed with caution."))
160 else:
161 user = request.user
162
163 form = forms.EditProfileForm(request.POST,
164 url=user.get('url'),
165 bio=user.get('bio'))
166
167 if request.method == 'POST' and form.validate():
168 user.url = unicode(request.POST['url'])
169 user.bio = unicode(request.POST['bio'])
170
171 user.save()
172
173 messages.add_message(request,
174 messages.SUCCESS,
175 _("Profile changes saved"))
176 return redirect(request,
177 'mediagoblin.user_pages.user_home',
178 user=user['username'])
179
180 return render_to_response(
181 request,
182 'mediagoblin/edit/edit_profile.html',
183 {'user': user,
184 'form': form})
185
186
187 @require_active_login
188 def edit_account(request):
189 edit_username = request.GET.get('username')
190 user = request.user
191
192 form = forms.EditAccountForm(request.POST)
193
194 if request.method == 'POST' and form.validate():
195 password_matches = auth_lib.bcrypt_check_password(
196 request.POST['old_password'],
197 user.pw_hash)
198
199 if (request.POST['old_password'] or request.POST['new_password']) and not \
200 password_matches:
201 form.old_password.errors.append(_('Wrong password'))
202
203 return render_to_response(
204 request,
205 'mediagoblin/edit/edit_account.html',
206 {'user': user,
207 'form': form})
208
209 if password_matches:
210 user.pw_hash = auth_lib.bcrypt_gen_password_hash(
211 request.POST['new_password'])
212
213 user.save()
214
215 messages.add_message(request,
216 messages.SUCCESS,
217 _("Account settings saved"))
218 return redirect(request,
219 'mediagoblin.user_pages.user_home',
220 user=user.username)
221
222 return render_to_response(
223 request,
224 'mediagoblin/edit/edit_account.html',
225 {'user': user,
226 'form': form})