1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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
,
37 media_tags_as_string
, cleaned_markdown_conversion
)
38 from mediagoblin
.tools
.licenses
import SUPPORTED_LICENSES
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 existing_user_slug_entries
= request
.db
.MediaEntry
.find(
61 {'slug': request
.POST
['slug'],
62 'uploader': media
.uploader
,
63 '_id': {'$ne': media
._id
}}).count()
65 if existing_user_slug_entries
:
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
.description_html
= cleaned_markdown_conversion(
78 unicode(request
.POST
.get('license'))
81 media
.slug
= unicode(request
.POST
['slug'])
86 location
=media
.url_for_self(request
.urlgen
))
88 if request
.user
.is_admin \
89 and media
.uploader
!= request
.user
._id \
90 and request
.method
!= 'POST':
92 request
, messages
.WARNING
,
93 _("You are editing another user's media. Proceed with caution."))
95 return render_to_response(
97 'mediagoblin/edit/edit.html',
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 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
)])
118 attachment_public_file
= mg_globals
.public_store
.get_file(
119 attachment_public_filepath
, 'wb')
122 attachment_public_file
.write(
123 request
.POST
['attachment_file'].file.read())
125 request
.POST
['attachment_file'].file.close()
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(),
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
))
142 return exc
.HTTPFound(
143 location
=media
.url_for_self(request
.urlgen
))
144 return render_to_response(
146 'mediagoblin/edit/attachments.html',
150 return exc
.HTTPForbidden()
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."))
167 form
= forms
.EditProfileForm(request
.POST
,
171 if request
.method
== 'POST' and form
.validate():
172 user
.url
= unicode(request
.POST
['url'])
173 user
.bio
= unicode(request
.POST
['bio'])
175 user
.bio_html
= cleaned_markdown_conversion(user
.bio
)
179 messages
.add_message(request
,
181 _("Profile changes saved"))
182 return redirect(request
,
183 'mediagoblin.user_pages.user_home',
184 user
=user
['username'])
186 return render_to_response(
188 'mediagoblin/edit/edit_profile.html',
193 @require_active_login
194 def edit_account(request
):
195 edit_username
= request
.GET
.get('username')
198 form
= forms
.EditAccountForm(request
.POST
)
200 if request
.method
== 'POST' and form
.validate():
201 password_matches
= auth_lib
.bcrypt_check_password(
202 request
.POST
['old_password'],
205 if (request
.POST
['old_password'] or request
.POST
['new_password']) and not \
207 form
.old_password
.errors
.append(_('Wrong password'))
209 return render_to_response(
211 'mediagoblin/edit/edit_account.html',
216 user
.pw_hash
= auth_lib
.bcrypt_gen_password_hash(
217 request
.POST
['new_password'])
221 messages
.add_message(request
,
223 _("Account settings saved"))
224 return redirect(request
,
225 'mediagoblin.user_pages.user_home',
228 return render_to_response(
230 'mediagoblin/edit/edit_account.html',