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 from mediagoblin
.tools
.response
import render_to_response
, redirect
31 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
32 from mediagoblin
.tools
.text
import (
33 convert_to_tag_list_of_dicts
, media_tags_as_string
)
34 from mediagoblin
.db
.util
import check_media_slug_used
39 def edit_media(request
, media
):
40 if not may_edit_media(request
, media
):
41 return exc
.HTTPForbidden()
46 description
=media
.description
,
47 tags
=media_tags_as_string(media
.tags
),
48 license
=media
.license
)
50 form
= forms
.EditForm(
54 if request
.method
== 'POST' and form
.validate():
55 # Make sure there isn't already a MediaEntry with such a slug
57 slug_used
= check_media_slug_used(request
.db
, media
.uploader
,
58 request
.POST
['slug'], media
.id)
61 form
.slug
.errors
.append(
62 _(u
'An entry with that slug already exists for this user.'))
64 media
.title
= unicode(request
.POST
['title'])
65 media
.description
= unicode(request
.POST
.get('description'))
66 media
.tags
= convert_to_tag_list_of_dicts(
67 request
.POST
.get('tags'))
69 media
.license
= unicode(request
.POST
.get('license', '')) or None
71 media
.slug
= unicode(request
.POST
['slug'])
76 location
=media
.url_for_self(request
.urlgen
))
78 if request
.user
.is_admin \
79 and media
.uploader
!= request
.user
._id \
80 and request
.method
!= 'POST':
82 request
, messages
.WARNING
,
83 _("You are editing another user's media. Proceed with caution."))
85 return render_to_response(
87 'mediagoblin/edit/edit.html',
94 def edit_attachments(request
, media
):
95 if mg_globals
.app_config
['allow_attachments']:
96 form
= forms
.EditAttachmentsForm()
98 # Add any attachements
99 if ('attachment_file' in request
.POST
100 and isinstance(request
.POST
['attachment_file'], FieldStorage
)
101 and request
.POST
['attachment_file'].file):
103 attachment_public_filepath \
104 = mg_globals
.public_store
.get_unique_filepath(
105 ['media_entries', unicode(media
._id
), 'attachment',
106 secure_filename(request
.POST
['attachment_file'].filename
)])
108 attachment_public_file
= mg_globals
.public_store
.get_file(
109 attachment_public_filepath
, 'wb')
112 attachment_public_file
.write(
113 request
.POST
['attachment_file'].file.read())
115 request
.POST
['attachment_file'].file.close()
117 media
.attachment_files
.append(dict(
118 name
=request
.POST
['attachment_name'] \
119 or request
.POST
['attachment_file'].filename
,
120 filepath
=attachment_public_filepath
,
121 created
=datetime
.utcnow(),
126 messages
.add_message(
127 request
, messages
.SUCCESS
,
128 "You added the attachment %s!" \
129 % (request
.POST
['attachment_name']
130 or request
.POST
['attachment_file'].filename
))
132 return exc
.HTTPFound(
133 location
=media
.url_for_self(request
.urlgen
))
134 return render_to_response(
136 'mediagoblin/edit/attachments.html',
140 return exc
.HTTPForbidden()
143 @require_active_login
144 def edit_profile(request
):
145 # admins may edit any user profile given a username in the querystring
146 edit_username
= request
.GET
.get('username')
147 if request
.user
.is_admin
and request
.user
.username
!= edit_username
:
148 user
= request
.db
.User
.find_one({'username': edit_username
})
149 # No need to warn again if admin just submitted an edited profile
150 if request
.method
!= 'POST':
151 messages
.add_message(
152 request
, messages
.WARNING
,
153 _("You are editing a user's profile. Proceed with caution."))
157 form
= forms
.EditProfileForm(request
.POST
,
161 if request
.method
== 'POST' and form
.validate():
162 user
.url
= unicode(request
.POST
['url'])
163 user
.bio
= unicode(request
.POST
['bio'])
167 messages
.add_message(request
,
169 _("Profile changes saved"))
170 return redirect(request
,
171 'mediagoblin.user_pages.user_home',
174 return render_to_response(
176 'mediagoblin/edit/edit_profile.html',
181 @require_active_login
182 def edit_account(request
):
185 form
= forms
.EditAccountForm(request
.POST
)
187 if request
.method
== 'POST' and form
.validate():
188 password_matches
= auth_lib
.bcrypt_check_password(
189 request
.POST
['old_password'],
192 if (request
.POST
['old_password'] or request
.POST
['new_password']) and not \
194 form
.old_password
.errors
.append(_('Wrong password'))
196 return render_to_response(
198 'mediagoblin/edit/edit_account.html',
203 user
.pw_hash
= auth_lib
.bcrypt_gen_password_hash(
204 request
.POST
['new_password'])
208 messages
.add_message(request
,
210 _("Account settings saved"))
211 return redirect(request
,
212 'mediagoblin.user_pages.user_home',
215 return render_to_response(
217 'mediagoblin/edit/edit_account.html',