Merge remote branch 'remotes/gullydwarf-cfdv/b404_v2_redirect_after_profile_edit'
[mediagoblin.git] / mediagoblin / edit / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
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.
8 #
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.
13 #
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/>.
16
17
18 from webob import exc
19
20 from mediagoblin import messages
21 from mediagoblin.util import render_to_response, redirect, clean_html
22 from mediagoblin.edit import forms
23 from mediagoblin.edit.lib import may_edit_media
24 from mediagoblin.decorators import require_active_login, get_user_media_entry
25
26 import markdown
27
28
29 @get_user_media_entry
30 @require_active_login
31 def edit_media(request, media):
32 if not may_edit_media(request, media):
33 return exc.HTTPForbidden()
34
35 form = forms.EditForm(request.POST,
36 title = media['title'],
37 slug = media['slug'],
38 description = media['description'])
39
40 if request.method == 'POST' and form.validate():
41 # Make sure there isn't already a MediaEntry with such a slug
42 # and userid.
43 existing_user_slug_entries = request.db.MediaEntry.find(
44 {'slug': request.POST['slug'],
45 'uploader': media['uploader'],
46 '_id': {'$ne': media['_id']}}).count()
47
48 if existing_user_slug_entries:
49 form.slug.errors.append(
50 u'An entry with that slug already exists for this user.')
51 else:
52 media['title'] = request.POST['title']
53 media['description'] = request.POST.get('description')
54
55 md = markdown.Markdown(
56 safe_mode = 'escape')
57 media['description_html'] = clean_html(
58 md.convert(
59 media['description']))
60
61 media['slug'] = request.POST['slug']
62 media.save()
63
64 return redirect(request, "mediagoblin.user_pages.media_home",
65 user=media.uploader()['username'], media=media['slug'])
66
67 if request.user['is_admin'] \
68 and media['uploader'] != request.user['_id'] \
69 and request.method != 'POST':
70 messages.add_message(
71 request, messages.WARNING,
72 "You are editing another user's media. Proceed with caution.")
73
74
75 return render_to_response(
76 request,
77 'mediagoblin/edit/edit.html',
78 {'media': media,
79 'form': form})
80
81
82 @require_active_login
83 def edit_profile(request):
84
85 # admins may edit any user profile given a username in the querystring
86 edit_username = request.GET.get('username')
87 if request.user['is_admin'] and request.user['username'] != edit_username:
88 user = request.db.User.find_one({'username': edit_username})
89 # No need to warn again if admin just submitted an edited profile
90 if request.method != 'POST':
91 messages.add_message(
92 request, messages.WARNING,
93 "You are editing a user's profile. Proceed with caution.")
94 else:
95 user = request.user
96
97 form = forms.EditProfileForm(request.POST,
98 url = user.get('url'),
99 bio = user.get('bio'))
100
101 if request.method == 'POST' and form.validate():
102 user['url'] = request.POST['url']
103 user['bio'] = request.POST['bio']
104 user.save()
105
106 messages.add_message(request,
107 messages.SUCCESS,
108 'Profile edited!')
109 return redirect(request,
110 'mediagoblin.user_pages.user_home',
111 username=edit_username)
112
113 return render_to_response(
114 request,
115 'mediagoblin/edit/edit_profile.html',
116 {'user': user,
117 'form': form})