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
43 def edit_media(request
, media
):
44 if not may_edit_media(request
, media
):
45 return exc
.HTTPForbidden()
50 description
=media
.description
,
51 tags
=media_tags_as_string(media
.tags
),
52 license
=media
.license
)
54 form
= forms
.EditForm(
58 if request
.method
== 'POST' and form
.validate():
59 # Make sure there isn't already a MediaEntry with such a slug
61 existing_user_slug_entries
= request
.db
.MediaEntry
.find(
62 {'slug': request
.POST
['slug'],
63 'uploader': media
.uploader
,
64 '_id': {'$ne': media
._id
}}).count()
66 if existing_user_slug_entries
:
67 form
.slug
.errors
.append(
68 _(u
'An entry with that slug already exists for this user.'))
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'))
75 media
.license
= unicode(request
.POST
.get('license', '')) or None
77 media
.slug
= unicode(request
.POST
['slug'])
82 location
=media
.url_for_self(request
.urlgen
))
84 if request
.user
.is_admin \
85 and media
.uploader
!= request
.user
._id \
86 and request
.method
!= 'POST':
88 request
, messages
.WARNING
,
89 _("You are editing another user's media. Proceed with caution."))
91 return render_to_response(
93 'mediagoblin/edit/edit.html',
100 def edit_attachments(request
, media
):
101 if mg_globals
.app_config
['allow_attachments']:
102 form
= forms
.EditAttachmentsForm()
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):
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
)])
114 attachment_public_file
= mg_globals
.public_store
.get_file(
115 attachment_public_filepath
, 'wb')
118 attachment_public_file
.write(
119 request
.POST
['attachment_file'].file.read())
121 request
.POST
['attachment_file'].file.close()
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(),
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
))
138 return exc
.HTTPFound(
139 location
=media
.url_for_self(request
.urlgen
))
140 return render_to_response(
142 'mediagoblin/edit/attachments.html',
146 return exc
.HTTPForbidden()
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."))
163 form
= forms
.EditProfileForm(request
.POST
,
167 if request
.method
== 'POST' and form
.validate():
168 user
.url
= unicode(request
.POST
['url'])
169 user
.bio
= unicode(request
.POST
['bio'])
173 messages
.add_message(request
,
175 _("Profile changes saved"))
176 return redirect(request
,
177 'mediagoblin.user_pages.user_home',
178 user
=user
['username'])
180 return render_to_response(
182 'mediagoblin/edit/edit_profile.html',
187 @require_active_login
188 def edit_account(request
):
189 edit_username
= request
.GET
.get('username')
192 form
= forms
.EditAccountForm(request
.POST
)
194 if request
.method
== 'POST' and form
.validate():
195 password_matches
= auth_lib
.bcrypt_check_password(
196 request
.POST
['old_password'],
199 if (request
.POST
['old_password'] or request
.POST
['new_password']) and not \
201 form
.old_password
.errors
.append(_('Wrong password'))
203 return render_to_response(
205 'mediagoblin/edit/edit_account.html',
210 user
.pw_hash
= auth_lib
.bcrypt_gen_password_hash(
211 request
.POST
['new_password'])
215 messages
.add_message(request
,
217 _("Account settings saved"))
218 return redirect(request
,
219 'mediagoblin.user_pages.user_home',
222 return render_to_response(
224 'mediagoblin/edit/edit_account.html',