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