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
)
41 def edit_media(request
, media
):
42 if not may_edit_media(request
, media
):
43 return exc
.HTTPForbidden()
48 description
=media
['description'],
49 tags
=media_tags_as_string(media
['tags']))
51 form
= forms
.EditForm(
55 if request
.method
== 'POST' and form
.validate():
56 # Make sure there isn't already a MediaEntry with such a slug
58 existing_user_slug_entries
= request
.db
.MediaEntry
.find(
59 {'slug': request
.POST
['slug'],
60 'uploader': media
['uploader'],
61 '_id': {'$ne': media
._id
}}).count()
63 if existing_user_slug_entries
:
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
['description_html'] = cleaned_markdown_conversion(
75 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',
97 def edit_attachments(request
, media
):
98 if mg_globals
.app_config
['allow_attachments']:
99 form
= forms
.EditAttachmentsForm()
101 # Add any attachements
102 if ('attachment_file' in request
.POST
103 and isinstance(request
.POST
['attachment_file'], FieldStorage
)
104 and request
.POST
['attachment_file'].file):
106 attachment_public_filepath \
107 = mg_globals
.public_store
.get_unique_filepath(
108 ['media_entries', unicode(media
._id
), 'attachment',
109 secure_filename(request
.POST
['attachment_file'].filename
)])
111 attachment_public_file
= mg_globals
.public_store
.get_file(
112 attachment_public_filepath
, 'wb')
115 attachment_public_file
.write(
116 request
.POST
['attachment_file'].file.read())
118 request
.POST
['attachment_file'].file.close()
120 media
['attachment_files'].append(dict(
121 name
=request
.POST
['attachment_name'] \
122 or request
.POST
['attachment_file'].filename
,
123 filepath
=attachment_public_filepath
,
124 created
=datetime
.utcnow(),
129 messages
.add_message(
130 request
, messages
.SUCCESS
,
131 "You added the attachment %s!" \
132 % (request
.POST
['attachment_name']
133 or request
.POST
['attachment_file'].filename
))
135 return exc
.HTTPFound(
136 location
=media
.url_for_self(request
.urlgen
))
137 return render_to_response(
139 'mediagoblin/edit/attachments.html',
143 return exc
.HTTPForbidden()
146 @require_active_login
147 def edit_profile(request
):
148 # admins may edit any user profile given a username in the querystring
149 edit_username
= request
.GET
.get('username')
150 if request
.user
.is_admin
and request
.user
.username
!= edit_username
:
151 user
= request
.db
.User
.find_one({'username': edit_username
})
152 # No need to warn again if admin just submitted an edited profile
153 if request
.method
!= 'POST':
154 messages
.add_message(
155 request
, messages
.WARNING
,
156 _("You are editing a user's profile. Proceed with caution."))
160 form
= forms
.EditProfileForm(request
.POST
,
164 if request
.method
== 'POST' and form
.validate():
165 password_matches
= auth_lib
.bcrypt_check_password(
166 request
.POST
['old_password'],
169 if (request
.POST
['old_password'] or request
.POST
['new_password']) and not \
171 form
.old_password
.errors
.append(_('Wrong password'))
173 return render_to_response(
175 'mediagoblin/edit/edit_profile.html',
179 user
['url'] = unicode(request
.POST
['url'])
180 user
['bio'] = unicode(request
.POST
['bio'])
183 user
['pw_hash'] = auth_lib
.bcrypt_gen_password_hash(
184 request
.POST
['new_password'])
186 user
['bio_html'] = cleaned_markdown_conversion(user
['bio'])
190 messages
.add_message(request
,
192 _("Profile edited!"))
193 return redirect(request
,
194 'mediagoblin.user_pages.user_home',
195 user
=user
['username'])
197 return render_to_response(
199 'mediagoblin/edit/edit_profile.html',