5cfb22977691ccb1a92e782349dc8a12a3f1333b
[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 Response, exc
19
20 from mediagoblin.edit import forms
21 from mediagoblin.edit.lib import may_edit_media
22 from mediagoblin.decorators import require_active_login, get_user_media_entry
23
24
25 @get_user_media_entry
26 @require_active_login
27 def edit_media(request, media):
28 if not may_edit_media(request, media):
29 return exc.HTTPForbidden()
30
31 form = forms.EditForm(request.POST,
32 title = media['title'],
33 slug = media['slug'],
34 description = media['description'])
35
36 if request.method == 'POST' and form.validate():
37 # Make sure there isn't already a MediaEntry with such a slug
38 # and userid.
39 existing_user_slug_entries = request.db.MediaEntry.find(
40 {'slug': request.POST['slug'],
41 'uploader': media['uploader'],
42 '_id': {'$ne': media['_id']}}).count()
43
44 if existing_user_slug_entries:
45 form.slug.errors.append(
46 u'An entry with that slug already exists for this user.')
47 else:
48 media['title'] = request.POST['title']
49 media['description'] = request.POST['description']
50 media['slug'] = request.POST['slug']
51
52 # redirect
53 return exc.HTTPFound(
54 location=request.urlgen("mediagoblin.user_pages.media_home",
55 user=media.uploader()['username'], media=media['_id']))
56
57 # render
58 template = request.template_env.get_template(
59 'mediagoblin/edit/edit.html')
60 return Response(
61 template.render(
62 {'request': request,
63 'media': media,
64 'form': form}))