modifies duplicate tag check for list of dict tag type change
[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)
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 import markdown
31
32
33 @get_user_media_entry
34 @require_active_login
35 def edit_media(request, media):
36 if not may_edit_media(request, media):
37 return exc.HTTPForbidden()
38
39 form = forms.EditForm(request.POST,
40 title = media['title'],
41 slug = media['slug'],
42 description = media['description'],
43 tags = media_tags_as_string(media['tags']))
44
45 if request.method == 'POST' and form.validate():
46 # Make sure there isn't already a MediaEntry with such a slug
47 # and userid.
48 existing_user_slug_entries = request.db.MediaEntry.find(
49 {'slug': request.POST['slug'],
50 'uploader': media['uploader'],
51 '_id': {'$ne': media['_id']}}).count()
52
53 if existing_user_slug_entries:
54 form.slug.errors.append(
55 u'An entry with that slug already exists for this user.')
56 else:
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'))
61
62 md = markdown.Markdown(
63 safe_mode = 'escape')
64 media['description_html'] = clean_html(
65 md.convert(
66 media['description']))
67
68 media['slug'] = request.POST['slug']
69 media.save()
70
71 return redirect(request, "mediagoblin.user_pages.media_home",
72 user=media.uploader()['username'], media=media['slug'])
73
74 if request.user['is_admin'] \
75 and media['uploader'] != request.user['_id'] \
76 and request.method != 'POST':
77 messages.add_message(
78 request, messages.WARNING,
79 "You are editing another user's media. Proceed with caution.")
80
81
82 return render_to_response(
83 request,
84 'mediagoblin/edit/edit.html',
85 {'media': media,
86 'form': form})
87
88
89 @require_active_login
90 def edit_profile(request):
91
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':
98 messages.add_message(
99 request, messages.WARNING,
100 "You are editing a user's profile. Proceed with caution.")
101 else:
102 user = request.user
103
104 form = forms.EditProfileForm(request.POST,
105 url = user.get('url'),
106 bio = user.get('bio'))
107
108 if request.method == 'POST' and form.validate():
109 user['url'] = request.POST['url']
110 user['bio'] = request.POST['bio']
111 user.save()
112
113 messages.add_message(request,
114 messages.SUCCESS,
115 'Profile edited!')
116 return redirect(request,
117 'mediagoblin.user_pages.user_home',
118 username=edit_username)
119
120 return render_to_response(
121 request,
122 'mediagoblin/edit/edit_profile.html',
123 {'user': user,
124 'form': form})