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