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
,
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
35 def edit_media(request
, media
):
36 if not may_edit_media(request
, media
):
37 return exc
.HTTPForbidden()
39 form
= forms
.EditForm(request
.POST
,
40 title
= media
['title'],
42 description
= media
['description'],
43 tags
= media_tags_as_string(media
['tags']))
45 if request
.method
== 'POST' and form
.validate():
46 # Make sure there isn't already a MediaEntry with such a slug
48 existing_user_slug_entries
= request
.db
.MediaEntry
.find(
49 {'slug': request
.POST
['slug'],
50 'uploader': media
['uploader'],
51 '_id': {'$ne': media
['_id']}}).count()
53 if existing_user_slug_entries
:
54 form
.slug
.errors
.append(
55 u
'An entry with that slug already exists for this user.')
57 media
['title'] = request
.POST
['title']
58 media
['description'] = request
.POST
.get('description')
59 media
['tags'] = convert_to_tag_list_of_dicts(
60 request
.POST
.get('tags'))
62 md
= markdown
.Markdown(
64 media
['description_html'] = clean_html(
66 media
['description']))
68 media
['slug'] = request
.POST
['slug']
71 return redirect(request
, "mediagoblin.user_pages.media_home",
72 user
=media
.uploader()['username'], media
=media
['slug'])
74 if request
.user
['is_admin'] \
75 and media
['uploader'] != request
.user
['_id'] \
76 and request
.method
!= 'POST':
78 request
, messages
.WARNING
,
79 "You are editing another user's media. Proceed with caution.")
82 return render_to_response(
84 'mediagoblin/edit/edit.html',
90 def edit_profile(request
):
92 # admins may edit any user profile given a username in the querystring
93 edit_username
= request
.GET
.get('username')
94 if request
.user
['is_admin'] and request
.user
['username'] != edit_username
:
95 user
= request
.db
.User
.find_one({'username': edit_username
})
96 # No need to warn again if admin just submitted an edited profile
97 if request
.method
!= 'POST':
99 request
, messages
.WARNING
,
100 "You are editing a user's profile. Proceed with caution.")
104 form
= forms
.EditProfileForm(request
.POST
,
105 url
= user
.get('url'),
106 bio
= user
.get('bio'))
108 if request
.method
== 'POST' and form
.validate():
109 user
['url'] = request
.POST
['url']
110 user
['bio'] = request
.POST
['bio']
113 messages
.add_message(request
,
116 return redirect(request
,
117 'mediagoblin.user_pages.user_home',
118 username
=edit_username
)
120 return render_to_response(
122 'mediagoblin/edit/edit_profile.html',