1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
18 from cgi
import FieldStorage
19 from datetime
import datetime
21 from werkzeug
.utils
import secure_filename
23 from mediagoblin
import messages
24 from mediagoblin
import mg_globals
26 from mediagoblin
.auth
import lib
as auth_lib
27 from mediagoblin
.edit
import forms
28 from mediagoblin
.edit
.lib
import may_edit_media
29 from mediagoblin
.decorators
import require_active_login
, get_user_media_entry
, \
30 user_may_alter_collection
, get_user_collection
31 from mediagoblin
.tools
.response
import render_to_response
, redirect
32 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
33 from mediagoblin
.tools
.text
import (
34 convert_to_tag_list_of_dicts
, media_tags_as_string
)
35 from mediagoblin
.db
.util
import check_media_slug_used
, check_collection_slug_used
42 def edit_media(request
, media
):
43 if not may_edit_media(request
, media
):
44 return exc
.HTTPForbidden()
49 description
=media
.description
,
50 tags
=media_tags_as_string(media
.tags
),
51 license
=media
.license
)
53 form
= forms
.EditForm(
57 if request
.method
== 'POST' and form
.validate():
58 # Make sure there isn't already a MediaEntry with such a slug
60 slug_used
= check_media_slug_used(request
.db
, media
.uploader
,
61 request
.POST
['slug'], media
.id)
64 form
.slug
.errors
.append(
65 _(u
'An entry with that slug already exists for this user.'))
67 media
.title
= unicode(request
.POST
['title'])
68 media
.description
= unicode(request
.POST
.get('description'))
69 media
.tags
= convert_to_tag_list_of_dicts(
70 request
.POST
.get('tags'))
72 media
.license
= unicode(request
.POST
.get('license', '')) or None
74 media
.slug
= unicode(request
.POST
['slug'])
79 location
=media
.url_for_self(request
.urlgen
))
81 if request
.user
.is_admin \
82 and media
.uploader
!= request
.user
._id \
83 and request
.method
!= 'POST':
85 request
, messages
.WARNING
,
86 _("You are editing another user's media. Proceed with caution."))
88 return render_to_response(
90 'mediagoblin/edit/edit.html',
95 # Mimetypes that browsers parse scripts in.
96 # Content-sniffing isn't taken into consideration.
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()
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):
113 # Security measure to prevent attachments from being served as
114 # text/html, which will be parsed by web clients and pose an XSS
118 # This method isn't flawless as some browsers may perform
120 # This method isn't flawless as we do the mimetype lookup on the
121 # machine parsing the upload form, and not necessarily the machine
122 # serving the attachments.
123 if mimetypes
.guess_type(
124 request
.POST
['attachment_file'].filename
)[0] in \
126 public_filename
= secure_filename('{0}.notsafe'.format(
127 request
.POST
['attachment_file'].filename
))
129 public_filename
= secure_filename(
130 request
.POST
['attachment_file'].filename
)
132 attachment_public_filepath \
133 = mg_globals
.public_store
.get_unique_filepath(
134 ['media_entries', unicode(media
._id
), 'attachment',
137 attachment_public_file
= mg_globals
.public_store
.get_file(
138 attachment_public_filepath
, 'wb')
141 attachment_public_file
.write(
142 request
.POST
['attachment_file'].file.read())
144 request
.POST
['attachment_file'].file.close()
146 media
.attachment_files
.append(dict(
147 name
=request
.POST
['attachment_name'] \
148 or request
.POST
['attachment_file'].filename
,
149 filepath
=attachment_public_filepath
,
150 created
=datetime
.utcnow(),
155 messages
.add_message(
156 request
, messages
.SUCCESS
,
157 "You added the attachment %s!" \
158 % (request
.POST
['attachment_name']
159 or request
.POST
['attachment_file'].filename
))
161 return exc
.HTTPFound(
162 location
=media
.url_for_self(request
.urlgen
))
163 return render_to_response(
165 'mediagoblin/edit/attachments.html',
169 return exc
.HTTPForbidden()
172 @require_active_login
173 def edit_profile(request
):
174 # admins may edit any user profile given a username in the querystring
175 edit_username
= request
.GET
.get('username')
176 if request
.user
.is_admin
and request
.user
.username
!= edit_username
:
177 user
= request
.db
.User
.find_one({'username': edit_username
})
178 # No need to warn again if admin just submitted an edited profile
179 if request
.method
!= 'POST':
180 messages
.add_message(
181 request
, messages
.WARNING
,
182 _("You are editing a user's profile. Proceed with caution."))
186 form
= forms
.EditProfileForm(request
.POST
,
190 if request
.method
== 'POST' and form
.validate():
191 user
.url
= unicode(request
.POST
['url'])
192 user
.bio
= unicode(request
.POST
['bio'])
196 messages
.add_message(request
,
198 _("Profile changes saved"))
199 return redirect(request
,
200 'mediagoblin.user_pages.user_home',
203 return render_to_response(
205 'mediagoblin/edit/edit_profile.html',
210 @require_active_login
211 def edit_account(request
):
213 form
= forms
.EditAccountForm(request
.POST
,
214 wants_comment_notification
=user
.get('wants_comment_notification'))
216 if request
.method
== 'POST':
217 form_validated
= form
.validate()
219 #if the user has not filled in the new or old password fields
220 if not form
.new_password
.data
and not form
.old_password
.data
:
221 if form
.wants_comment_notification
.validate(form
):
222 user
.wants_comment_notification
= \
223 form
.wants_comment_notification
.data
225 messages
.add_message(request
,
227 _("Account settings saved"))
228 return redirect(request
,
229 'mediagoblin.user_pages.user_home',
232 #so the user has filled in one or both of the password fields
235 password_matches
= auth_lib
.bcrypt_check_password(
236 form
.old_password
.data
,
239 #the entire form validates and the password matches
240 user
.pw_hash
= auth_lib
.bcrypt_gen_password_hash(
241 form
.new_password
.data
)
242 user
.wants_comment_notification
= \
243 form
.wants_comment_notification
.data
245 messages
.add_message(request
,
247 _("Account settings saved"))
248 return redirect(request
,
249 'mediagoblin.user_pages.user_home',
252 form
.old_password
.errors
.append(_('Wrong password'))
254 return render_to_response(
256 'mediagoblin/edit/edit_account.html',
261 @require_active_login
262 @user_may_alter_collection
264 def edit_collection(request
, collection
):
266 title
=collection
.title
,
267 slug
=collection
.slug
,
268 description
=collection
.description
)
270 form
= forms
.EditCollectionForm(
274 if request
.method
== 'POST' and form
.validate():
275 # Make sure there isn't already a Collection with such a slug
277 slug_used
= check_collection_slug_used(request
.db
, collection
.creator
,
278 request
.POST
['slug'], collection
.id)
280 # Make sure there isn't already a Collection with this title
281 existing_collection
= request
.db
.Collection
.find_one({
282 'creator': request
.user
._id
,
283 'title':request
.POST
['title']})
285 if existing_collection
and existing_collection
.id != collection
.id:
286 messages
.add_message(
287 request
, messages
.ERROR
,
288 _('You already have a collection called "%s"!') % \
289 request
.POST
['title'])
291 form
.slug
.errors
.append(
292 _(u
'A collection with that slug already exists for this user.'))
294 collection
.title
= unicode(request
.POST
['title'])
295 collection
.description
= unicode(request
.POST
.get('description'))
296 collection
.slug
= unicode(request
.POST
['slug'])
300 return redirect(request
, "mediagoblin.user_pages.user_collection",
301 user
=collection
.get_creator
.username
,
302 collection
=collection
.slug
)
304 if request
.user
.is_admin \
305 and collection
.creator
!= request
.user
._id \
306 and request
.method
!= 'POST':
307 messages
.add_message(
308 request
, messages
.WARNING
,
309 _("You are editing another user's collection. Proceed with caution."))
311 return render_to_response(
313 'mediagoblin/edit/edit_collection.html',
314 {'collection': collection
,