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
.edit
import forms
30 from mediagoblin
.edit
.lib
import may_edit_media
31 from mediagoblin
.decorators
import require_active_login
, get_user_media_entry
32 from mediagoblin
.tools
.response
import render_to_response
, redirect
33 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
34 from mediagoblin
.tools
.text
import (
35 clean_html
, convert_to_tag_list_of_dicts
,
36 media_tags_as_string
, cleaned_markdown_conversion
)
40 def edit_media(request
, media
):
41 if not may_edit_media(request
, media
):
42 return exc
.HTTPForbidden()
47 description
=media
['description'],
48 tags
=media_tags_as_string(media
['tags']))
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 existing_user_slug_entries
= request
.db
.MediaEntry
.find(
58 {'slug': request
.POST
['slug'],
59 'uploader': media
['uploader'],
60 '_id': {'$ne': media
['_id']}}).count()
62 if existing_user_slug_entries
:
63 form
.slug
.errors
.append(
64 _(u
'An entry with that slug already exists for this user.'))
66 media
['title'] = unicode(request
.POST
['title'])
67 media
['description'] = unicode(request
.POST
.get('description'))
68 media
['tags'] = convert_to_tag_list_of_dicts(
69 request
.POST
.get('tags'))
71 media
['description_html'] = cleaned_markdown_conversion(
74 media
['slug'] = unicode(request
.POST
['slug'])
78 location
=media
.url_for_self(request
.urlgen
))
80 if request
.user
['is_admin'] \
81 and media
['uploader'] != request
.user
['_id'] \
82 and request
.method
!= 'POST':
84 request
, messages
.WARNING
,
85 _("You are editing another user's media. Proceed with caution."))
87 return render_to_response(
89 'mediagoblin/edit/edit.html',
96 def edit_attachments(request
, media
):
97 if mg_globals
.app_config
['allow_attachments']:
98 form
= forms
.EditAttachmentsForm()
100 # Add any attachements
101 if ('attachment_file' in request
.POST
102 and isinstance(request
.POST
['attachment_file'], FieldStorage
)
103 and request
.POST
['attachment_file'].file):
105 attachment_public_filepath \
106 = mg_globals
.public_store
.get_unique_filepath(
107 ['media_entries', unicode(media
['_id']), 'attachment',
108 secure_filename(request
.POST
['attachment_file'].filename
)])
110 attachment_public_file
= mg_globals
.public_store
.get_file(
111 attachment_public_filepath
, 'wb')
114 attachment_public_file
.write(
115 request
.POST
['attachment_file'].file.read())
117 request
.POST
['attachment_file'].file.close()
119 media
['attachment_files'].append(dict(
120 name
=request
.POST
['attachment_name'] \
121 or request
.POST
['attachment_file'].filename
,
122 filepath
=attachment_public_filepath
,
123 created
=datetime
.utcnow()
128 messages
.add_message(
129 request
, messages
.SUCCESS
,
130 "You added the attachment %s!" \
131 % (request
.POST
['attachment_name']
132 or request
.POST
['attachment_file'].filename
))
134 return exc
.HTTPFound(
135 location
=media
.url_for_self(request
.urlgen
))
136 return render_to_response(
138 'mediagoblin/edit/attachments.html',
142 return exc
.HTTPForbidden()
145 @require_active_login
146 def edit_profile(request
):
147 # admins may edit any user profile given a username in the querystring
148 edit_username
= request
.GET
.get('username')
149 if request
.user
['is_admin'] and request
.user
['username'] != edit_username
:
150 user
= request
.db
.User
.find_one({'username': edit_username
})
151 # No need to warn again if admin just submitted an edited profile
152 if request
.method
!= 'POST':
153 messages
.add_message(
154 request
, messages
.WARNING
,
155 _("You are editing a user's profile. Proceed with caution."))
159 form
= forms
.EditProfileForm(request
.POST
,
163 if request
.method
== 'POST' and form
.validate():
164 user
['url'] = unicode(request
.POST
['url'])
165 user
['bio'] = unicode(request
.POST
['bio'])
167 user
['bio_html'] = cleaned_markdown_conversion(user
['bio'])
171 messages
.add_message(request
,
173 _("Profile edited!"))
174 return redirect(request
,
175 'mediagoblin.user_pages.user_home',
178 return render_to_response(
180 'mediagoblin/edit/edit_profile.html',