Merge branch 'remotes/gullydwarf-cfdv/f360_tagging' (early part) into mergetags
[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 from string import split
20
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
29
30
31 @get_user_media_entry
32 @require_active_login
33 def edit_media(request, media):
34 if not may_edit_media(request, media):
35 return exc.HTTPForbidden()
36
37 form = forms.EditForm(request.POST,
38 title = media['title'],
39 slug = media['slug'],
40 description = media['description'],
41 tags = media_tags_as_string(media['tags']))
42
43 if request.method == 'POST' and form.validate():
44 # Make sure there isn't already a MediaEntry with such a slug
45 # and userid.
46 existing_user_slug_entries = request.db.MediaEntry.find(
47 {'slug': request.POST['slug'],
48 'uploader': media['uploader'],
49 '_id': {'$ne': media['_id']}}).count()
50
51 if existing_user_slug_entries:
52 form.slug.errors.append(
53 u'An entry with that slug already exists for this user.')
54 else:
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'))
59
60 media['description_html'] = cleaned_markdown_conversion(
61 media['description'])
62
63 media['slug'] = request.POST['slug']
64 media.save()
65
66 return redirect(request, "mediagoblin.user_pages.media_home",
67 user=media.uploader()['username'], media=media['slug'])
68
69 if request.user['is_admin'] \
70 and media['uploader'] != request.user['_id'] \
71 and request.method != 'POST':
72 messages.add_message(
73 request, messages.WARNING,
74 "You are editing another user's media. Proceed with caution.")
75
76
77 return render_to_response(
78 request,
79 'mediagoblin/edit/edit.html',
80 {'media': media,
81 'form': form})
82
83
84 @require_active_login
85 def edit_profile(request):
86
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':
93 messages.add_message(
94 request, messages.WARNING,
95 "You are editing a user's profile. Proceed with caution.")
96 else:
97 user = request.user
98
99 form = forms.EditProfileForm(request.POST,
100 url = user.get('url'),
101 bio = user.get('bio'))
102
103 if request.method == 'POST' and form.validate():
104 user['url'] = request.POST['url']
105 user['bio'] = request.POST['bio']
106
107 user['bio_html'] = cleaned_markdown_conversion(user['bio'])
108
109 user.save()
110
111 messages.add_message(request,
112 messages.SUCCESS,
113 'Profile edited!')
114 return redirect(request,
115 'mediagoblin.user_pages.user_home',
116 user=edit_username)
117
118 return render_to_response(
119 request,
120 'mediagoblin/edit/edit_profile.html',
121 {'user': user,
122 'form': form})