Dot-Notation: Some random places
[mediagoblin.git] / mediagoblin / edit / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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 import uuid
18
19 from webob import exc
20 from string import split
21 from cgi import FieldStorage
22 from datetime import datetime
23
24 from werkzeug.utils import secure_filename
25
26 from mediagoblin import messages
27 from mediagoblin import mg_globals
28
29 from mediagoblin.auth import lib as auth_lib
30 from mediagoblin.edit import forms
31 from mediagoblin.edit.lib import may_edit_media
32 from mediagoblin.decorators import require_active_login, get_user_media_entry
33 from mediagoblin.tools.response import render_to_response, redirect
34 from mediagoblin.tools.translate import pass_to_ugettext as _
35 from mediagoblin.tools.text import (
36 clean_html, convert_to_tag_list_of_dicts,
37 media_tags_as_string, cleaned_markdown_conversion)
38
39 @get_user_media_entry
40 @require_active_login
41 def edit_media(request, media):
42 if not may_edit_media(request, media):
43 return exc.HTTPForbidden()
44
45 defaults = dict(
46 title=media.title,
47 slug=media.slug,
48 description=media.description,
49 tags=media_tags_as_string(media.tags))
50
51 form = forms.EditForm(
52 request.POST,
53 **defaults)
54
55 if request.method == 'POST' and form.validate():
56 # Make sure there isn't already a MediaEntry with such a slug
57 # and userid.
58 existing_user_slug_entries = request.db.MediaEntry.find(
59 {'slug': request.POST['slug'],
60 'uploader': media.uploader,
61 '_id': {'$ne': media._id}}).count()
62
63 if existing_user_slug_entries:
64 form.slug.errors.append(
65 _(u'An entry with that slug already exists for this user.'))
66 else:
67 media.title = unicode(request.POST['title'])
68 media.description = unicode(request.POST.get('description'))
69 media['tags'] = convert_to_tag_list_of_dicts(
70 request.POST.get('tags'))
71
72 media.description_html = cleaned_markdown_conversion(
73 media.description)
74
75 media.slug = unicode(request.POST['slug'])
76 media.save()
77
78 return exc.HTTPFound(
79 location=media.url_for_self(request.urlgen))
80
81 if request.user.is_admin \
82 and media.uploader != request.user._id \
83 and request.method != 'POST':
84 messages.add_message(
85 request, messages.WARNING,
86 _("You are editing another user's media. Proceed with caution."))
87
88 return render_to_response(
89 request,
90 'mediagoblin/edit/edit.html',
91 {'media': media,
92 'form': form})
93
94
95 @get_user_media_entry
96 @require_active_login
97 def edit_attachments(request, media):
98 if mg_globals.app_config['allow_attachments']:
99 form = forms.EditAttachmentsForm()
100
101 # Add any attachements
102 if ('attachment_file' in request.POST
103 and isinstance(request.POST['attachment_file'], FieldStorage)
104 and request.POST['attachment_file'].file):
105
106 attachment_public_filepath \
107 = mg_globals.public_store.get_unique_filepath(
108 ['media_entries', unicode(media._id), 'attachment',
109 secure_filename(request.POST['attachment_file'].filename)])
110
111 attachment_public_file = mg_globals.public_store.get_file(
112 attachment_public_filepath, 'wb')
113
114 try:
115 attachment_public_file.write(
116 request.POST['attachment_file'].file.read())
117 finally:
118 request.POST['attachment_file'].file.close()
119
120 media['attachment_files'].append(dict(
121 name=request.POST['attachment_name'] \
122 or request.POST['attachment_file'].filename,
123 filepath=attachment_public_filepath,
124 created=datetime.utcnow(),
125 ))
126
127 media.save()
128
129 messages.add_message(
130 request, messages.SUCCESS,
131 "You added the attachment %s!" \
132 % (request.POST['attachment_name']
133 or request.POST['attachment_file'].filename))
134
135 return exc.HTTPFound(
136 location=media.url_for_self(request.urlgen))
137 return render_to_response(
138 request,
139 'mediagoblin/edit/attachments.html',
140 {'media': media,
141 'form': form})
142 else:
143 return exc.HTTPForbidden()
144
145
146 @require_active_login
147 def edit_profile(request):
148 # admins may edit any user profile given a username in the querystring
149 edit_username = request.GET.get('username')
150 if request.user.is_admin and request.user.username != edit_username:
151 user = request.db.User.find_one({'username': edit_username})
152 # No need to warn again if admin just submitted an edited profile
153 if request.method != 'POST':
154 messages.add_message(
155 request, messages.WARNING,
156 _("You are editing a user's profile. Proceed with caution."))
157 else:
158 user = request.user
159
160 form = forms.EditProfileForm(request.POST,
161 url=user.get('url'),
162 bio=user.get('bio'))
163
164 if request.method == 'POST' and form.validate():
165 user.url = unicode(request.POST['url'])
166 user.bio = unicode(request.POST['bio'])
167
168 user.bio_html = cleaned_markdown_conversion(user.bio)
169
170 user.save()
171
172 messages.add_message(request,
173 messages.SUCCESS,
174 _("Profile changes saved"))
175 return redirect(request,
176 'mediagoblin.user_pages.user_home',
177 user=user['username'])
178
179 return render_to_response(
180 request,
181 'mediagoblin/edit/edit_profile.html',
182 {'user': user,
183 'form': form})
184
185
186 @require_active_login
187 def edit_account(request):
188 edit_username = request.GET.get('username')
189 user = request.user
190
191 form = forms.EditAccountForm(request.POST)
192
193 if request.method == 'POST' and form.validate():
194 password_matches = auth_lib.bcrypt_check_password(
195 request.POST['old_password'],
196 user.pw_hash)
197
198 if (request.POST['old_password'] or request.POST['new_password']) and not \
199 password_matches:
200 form.old_password.errors.append(_('Wrong password'))
201
202 return render_to_response(
203 request,
204 'mediagoblin/edit/edit_account.html',
205 {'user': user,
206 'form': form})
207
208 if password_matches:
209 user.pw_hash = auth_lib.bcrypt_gen_password_hash(
210 request.POST['new_password'])
211
212 user.save()
213
214 messages.add_message(request,
215 messages.SUCCESS,
216 _("Account settings saved"))
217 return redirect(request,
218 'mediagoblin.user_pages.user_home',
219 user=user.username)
220
221 return render_to_response(
222 request,
223 'mediagoblin/edit/edit_account.html',
224 {'user': user,
225 'form': form})