9cec74dcefbe5a2087506f0521737180ad9dc1bc
[mediagoblin.git] / mediagoblin / user_pages / views.py
1 # 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 from webob import exc
18
19 from mediagoblin import messages, mg_globals
20 from mediagoblin.db.util import DESCENDING, ObjectId
21 from mediagoblin.tools.text import cleaned_markdown_conversion
22 from mediagoblin.tools.response import render_to_response, render_404, redirect
23 from mediagoblin.tools.translate import pass_to_ugettext as _
24 from mediagoblin.tools.pagination import Pagination
25 from mediagoblin.tools.files import delete_media_files
26 from mediagoblin.user_pages import forms as user_forms
27
28 from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
29 require_active_login, user_may_delete_media)
30
31 from werkzeug.contrib.atom import AtomFeed
32
33
34 @uses_pagination
35 def user_home(request, page):
36 """'Homepage' of a User()"""
37 user = request.db.User.find_one({
38 'username': request.matchdict['user']})
39 if not user:
40 return render_404(request)
41 elif user['status'] != u'active':
42 return render_to_response(
43 request,
44 'mediagoblin/user_pages/user.html',
45 {'user': user})
46
47 cursor = request.db.MediaEntry.find(
48 {'uploader': user['_id'],
49 'state': 'processed'}).sort('created', DESCENDING)
50
51 pagination = Pagination(page, cursor)
52 media_entries = pagination()
53
54 #if no data is available, return NotFound
55 if media_entries == None:
56 return render_404(request)
57
58 user_gallery_url = request.urlgen(
59 'mediagoblin.user_pages.user_gallery',
60 user=user['username'])
61
62 return render_to_response(
63 request,
64 'mediagoblin/user_pages/user.html',
65 {'user': user,
66 'user_gallery_url': user_gallery_url,
67 'media_entries': media_entries,
68 'pagination': pagination})
69
70 @uses_pagination
71 def user_gallery(request, page):
72 """'Gallery' of a User()"""
73 user = request.db.User.find_one({
74 'username': request.matchdict['user'],
75 'status': 'active'})
76 if not user:
77 return render_404(request)
78
79 cursor = request.db.MediaEntry.find(
80 {'uploader': user['_id'],
81 'state': 'processed'}).sort('created', DESCENDING)
82
83 pagination = Pagination(page, cursor)
84 media_entries = pagination()
85
86 #if no data is available, return NotFound
87 if media_entries == None:
88 return render_404(request)
89
90 return render_to_response(
91 request,
92 'mediagoblin/user_pages/gallery.html',
93 {'user': user,
94 'media_entries': media_entries,
95 'pagination': pagination})
96
97 MEDIA_COMMENTS_PER_PAGE = 50
98
99 @get_user_media_entry
100 @uses_pagination
101 def media_home(request, media, page, **kwargs):
102 """
103 'Homepage' of a MediaEntry()
104 """
105 if ObjectId(request.matchdict.get('comment')):
106 pagination = Pagination(
107 page, media.get_comments(), MEDIA_COMMENTS_PER_PAGE,
108 ObjectId(request.matchdict.get('comment')))
109 else:
110 pagination = Pagination(
111 page, media.get_comments(), MEDIA_COMMENTS_PER_PAGE)
112
113 comments = pagination()
114
115 comment_form = user_forms.MediaCommentForm(request.POST)
116
117 return render_to_response(
118 request,
119 'mediagoblin/user_pages/media.html',
120 {'media': media,
121 'comments': comments,
122 'pagination': pagination,
123 'comment_form': comment_form,
124 'app_config': mg_globals.app_config})
125
126
127 @require_active_login
128 def media_post_comment(request):
129 """
130 recieves POST from a MediaEntry() comment form, saves the comment.
131 """
132 comment = request.db.MediaComment()
133 comment['media_entry'] = ObjectId(request.matchdict['media'])
134 comment['author'] = request.user['_id']
135 comment['content'] = unicode(request.POST['comment_content'])
136
137 comment['content_html'] = cleaned_markdown_conversion(comment['content'])
138
139 comment.save()
140
141 messages.add_message(
142 request, messages.SUCCESS,
143 'Comment posted!')
144
145 return redirect(request, 'mediagoblin.user_pages.media_home',
146 media = request.matchdict['media'],
147 user = request.matchdict['user'])
148
149
150 @get_user_media_entry
151 @require_active_login
152 @user_may_delete_media
153 def media_confirm_delete(request, media):
154
155 form = user_forms.ConfirmDeleteForm(request.POST)
156
157 if request.method == 'POST' and form.validate():
158 if form.confirm.data is True:
159 username = media.uploader()['username']
160
161 # Delete all files on the public storage
162 delete_media_files(media)
163
164 media.delete()
165
166 return redirect(request, "mediagoblin.user_pages.user_home",
167 user=username)
168 else:
169 return exc.HTTPFound(
170 location=media.url_for_self(request.urlgen))
171
172 if ((request.user[u'is_admin'] and
173 request.user[u'_id'] != media.uploader()[u'_id'])):
174 messages.add_message(
175 request, messages.WARNING,
176 _("You are about to delete another user's media. "
177 "Proceed with caution."))
178
179 return render_to_response(
180 request,
181 'mediagoblin/user_pages/media_confirm_delete.html',
182 {'media': media,
183 'form': form})
184
185
186 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
187
188 def atom_feed(request):
189 """
190 generates the atom feed with the newest images
191 """
192
193 user = request.db.User.find_one({
194 'username': request.matchdict['user'],
195 'status': 'active'})
196 if not user:
197 return render_404(request)
198
199 cursor = request.db.MediaEntry.find({
200 'uploader': user['_id'],
201 'state': 'processed'}) \
202 .sort('created', DESCENDING) \
203 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
204
205 feed = AtomFeed(request.matchdict['user'],
206 feed_url=request.url,
207 url=request.host_url)
208
209 for entry in cursor:
210 feed.add(entry.get('title'),
211 entry.get('description_html'),
212 content_type='html',
213 author=request.matchdict['user'],
214 updated=entry.get('created'),
215 url=entry.url_for_self(request.urlgen))
216
217 return feed.get_response()
218
219
220 @require_active_login
221 def processing_panel(request):
222 """
223 Show to the user what media is still in conversion/processing...
224 and what failed, and why!
225 """
226 # Get the user
227 user = request.db.User.find_one(
228 {'username': request.matchdict['user'],
229 'status': 'active'})
230
231 # Make sure the user exists and is active
232 if not user:
233 return render_404(request)
234 elif user['status'] != u'active':
235 return render_to_response(
236 request,
237 'mediagoblin/user_pages/user.html',
238 {'user': user})
239
240 # XXX: Should this be a decorator?
241 #
242 # Make sure we have permission to access this user's panel. Only
243 # admins and this user herself should be able to do so.
244 if not (user[u'_id'] == request.user[u'_id']
245 or request.user.is_admin):
246 # No? Let's simply redirect to this user's homepage then.
247 return redirect(
248 request, 'mediagoblin.user_pages.user_home',
249 user=request.matchdict['user'])
250
251 # Get media entries which are in-processing
252 processing_entries = request.db.MediaEntry.find(
253 {'uploader': user['_id'],
254 'state': 'processing'}).sort('created', DESCENDING)
255
256 # Get media entries which have failed to process
257 failed_entries = request.db.MediaEntry.find(
258 {'uploader': user['_id'],
259 'state': 'failed'}).sort('created', DESCENDING)
260
261 # Render to response
262 return render_to_response(
263 request,
264 'mediagoblin/user_pages/processing_panel.html',
265 {'user': user,
266 'processing_entries': processing_entries,
267 'failed_entries': failed_entries})