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/>.
20 from string
import split
21 from cgi
import FieldStorage
22 from datetime
import datetime
24 from werkzeug
.utils
import secure_filename
26 from mediagoblin
import messages
27 from mediagoblin
import mg_globals
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
,
38 from mediagoblin
.tools
.licenses
import SUPPORTED_LICENSES
39 from mediagoblin
.db
.util
import check_media_slug_used
44 def edit_media(request
, media
):
45 if not may_edit_media(request
, media
):
46 return exc
.HTTPForbidden()
51 description
=media
.description
,
52 tags
=media_tags_as_string(media
.tags
),
53 license
=media
.license
)
55 form
= forms
.EditForm(
59 if request
.method
== 'POST' and form
.validate():
60 # Make sure there isn't already a MediaEntry with such a slug
62 slug_used
= check_media_slug_used(request
.db
, media
.uploader
,
63 request
.POST
['slug'], media
.id)
66 form
.slug
.errors
.append(
67 _(u
'An entry with that slug already exists for this user.'))
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'))
74 media
.license
= unicode(request
.POST
.get('license', '')) or None
76 media
.slug
= unicode(request
.POST
['slug'])
81 location
=media
.url_for_self(request
.urlgen
))
83 if request
.user
.is_admin \
84 and media
.uploader
!= request
.user
._id \
85 and request
.method
!= 'POST':
87 request
, messages
.WARNING
,
88 _("You are editing another user's media. Proceed with caution."))
90 return render_to_response(
92 'mediagoblin/edit/edit.html',
99 def edit_attachments(request
, media
):
100 if mg_globals
.app_config
['allow_attachments']:
101 form
= forms
.EditAttachmentsForm()
103 # Add any attachements
104 if ('attachment_file' in request
.POST
105 and isinstance(request
.POST
['attachment_file'], FieldStorage
)
106 and request
.POST
['attachment_file'].file):
108 attachment_public_filepath \
109 = mg_globals
.public_store
.get_unique_filepath(
110 ['media_entries', unicode(media
._id
), 'attachment',
111 secure_filename(request
.POST
['attachment_file'].filename
)])
113 attachment_public_file
= mg_globals
.public_store
.get_file(
114 attachment_public_filepath
, 'wb')
117 attachment_public_file
.write(
118 request
.POST
['attachment_file'].file.read())
120 request
.POST
['attachment_file'].file.close()
122 media
.attachment_files
.append(dict(
123 name
=request
.POST
['attachment_name'] \
124 or request
.POST
['attachment_file'].filename
,
125 filepath
=attachment_public_filepath
,
126 created
=datetime
.utcnow(),
131 messages
.add_message(
132 request
, messages
.SUCCESS
,
133 "You added the attachment %s!" \
134 % (request
.POST
['attachment_name']
135 or request
.POST
['attachment_file'].filename
))
137 return exc
.HTTPFound(
138 location
=media
.url_for_self(request
.urlgen
))
139 return render_to_response(
141 'mediagoblin/edit/attachments.html',
145 return exc
.HTTPForbidden()
148 @require_active_login
149 def edit_profile(request
):
150 # admins may edit any user profile given a username in the querystring
151 edit_username
= request
.GET
.get('username')
152 if request
.user
.is_admin
and request
.user
.username
!= edit_username
:
153 user
= request
.db
.User
.find_one({'username': edit_username
})
154 # No need to warn again if admin just submitted an edited profile
155 if request
.method
!= 'POST':
156 messages
.add_message(
157 request
, messages
.WARNING
,
158 _("You are editing a user's profile. Proceed with caution."))
162 form
= forms
.EditProfileForm(request
.POST
,
166 if request
.method
== 'POST' and form
.validate():
167 user
.url
= unicode(request
.POST
['url'])
168 user
.bio
= unicode(request
.POST
['bio'])
172 messages
.add_message(request
,
174 _("Profile changes saved"))
175 return redirect(request
,
176 'mediagoblin.user_pages.user_home',
177 user
=user
['username'])
179 return render_to_response(
181 'mediagoblin/edit/edit_profile.html',
186 @require_active_login
187 def edit_account(request
):
188 edit_username
= request
.GET
.get('username')
191 form
= forms
.EditAccountForm(request
.POST
)
193 if request
.method
== 'POST' and form
.validate():
194 password_matches
= auth_lib
.bcrypt_check_password(
195 request
.POST
['old_password'],
198 if (request
.POST
['old_password'] or request
.POST
['new_password']) and not \
200 form
.old_password
.errors
.append(_('Wrong password'))
202 return render_to_response(
204 'mediagoblin/edit/edit_account.html',
209 user
.pw_hash
= auth_lib
.bcrypt_gen_password_hash(
210 request
.POST
['new_password'])
214 messages
.add_message(request
,
216 _("Account settings saved"))
217 return redirect(request
,
218 'mediagoblin.user_pages.user_home',
221 return render_to_response(
223 'mediagoblin/edit/edit_account.html',