1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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/>.
19 from string
import split
21 from mediagoblin
import messages
22 from mediagoblin
import mg_globals
23 from mediagoblin
.util
import (
24 render_to_response
, redirect
, clean_html
, convert_to_tag_list_of_dicts
,
25 media_tags_as_string
, cleaned_markdown_conversion
)
26 from mediagoblin
.edit
import forms
27 from mediagoblin
.edit
.lib
import may_edit_media
28 from mediagoblin
.decorators
import require_active_login
, get_user_media_entry
33 def edit_media(request
, media
):
34 if not may_edit_media(request
, media
):
35 return exc
.HTTPForbidden()
37 form
= forms
.EditForm(request
.POST
,
38 title
= media
['title'],
40 description
= media
['description'],
41 tags
= media_tags_as_string(media
['tags']))
43 if request
.method
== 'POST' and form
.validate():
44 # Make sure there isn't already a MediaEntry with such a slug
46 existing_user_slug_entries
= request
.db
.MediaEntry
.find(
47 {'slug': request
.POST
['slug'],
48 'uploader': media
['uploader'],
49 '_id': {'$ne': media
['_id']}}).count()
51 if existing_user_slug_entries
:
52 form
.slug
.errors
.append(
53 u
'An entry with that slug already exists for this user.')
55 media
['title'] = request
.POST
['title']
56 media
['description'] = request
.POST
.get('description')
57 media
['tags'] = convert_to_tag_list_of_dicts(
58 request
.POST
.get('tags'))
60 media
['description_html'] = cleaned_markdown_conversion(
63 media
['slug'] = request
.POST
['slug']
66 return redirect(request
, "mediagoblin.user_pages.media_home",
67 user
=media
.uploader()['username'], media
=media
['slug'])
69 if request
.user
['is_admin'] \
70 and media
['uploader'] != request
.user
['_id'] \
71 and request
.method
!= 'POST':
73 request
, messages
.WARNING
,
74 "You are editing another user's media. Proceed with caution.")
77 return render_to_response(
79 'mediagoblin/edit/edit.html',
85 def edit_profile(request
):
87 # admins may edit any user profile given a username in the querystring
88 edit_username
= request
.GET
.get('username')
89 if request
.user
['is_admin'] and request
.user
['username'] != edit_username
:
90 user
= request
.db
.User
.find_one({'username': edit_username
})
91 # No need to warn again if admin just submitted an edited profile
92 if request
.method
!= 'POST':
94 request
, messages
.WARNING
,
95 "You are editing a user's profile. Proceed with caution.")
99 form
= forms
.EditProfileForm(request
.POST
,
100 url
= user
.get('url'),
101 bio
= user
.get('bio'))
103 if request
.method
== 'POST' and form
.validate():
104 user
['url'] = request
.POST
['url']
105 user
['bio'] = request
.POST
['bio']
107 user
['bio_html'] = cleaned_markdown_conversion(user
['bio'])
111 messages
.add_message(request
,
114 return redirect(request
,
115 'mediagoblin.user_pages.user_home',
118 return render_to_response(
120 'mediagoblin/edit/edit_profile.html',