Merge remote branch 'remotes/elrond/idea/invalidid'
[mediagoblin.git] / mediagoblin / user_pages / 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 from webob import exc
18 from mediagoblin.db.util import DESCENDING
19 from mediagoblin.util import Pagination, render_to_response
20
21 from mediagoblin.decorators import uses_pagination, get_user_media_entry
22
23 from werkzeug.contrib.atom import AtomFeed
24
25 @uses_pagination
26 def user_home(request, page):
27 """'Homepage' of a User()"""
28 user = request.db.User.find_one({
29 'username': request.matchdict['user'],
30 'status': 'active'})
31 if not user:
32 return exc.HTTPNotFound()
33
34 cursor = request.db.MediaEntry.find(
35 {'uploader': user['_id'],
36 'state': 'processed'}).sort('created', DESCENDING)
37
38 pagination = Pagination(page, cursor)
39 media_entries = pagination()
40
41 #if no data is available, return NotFound
42 if media_entries == None:
43 return exc.HTTPNotFound()
44
45 return render_to_response(
46 request,
47 'mediagoblin/user_pages/user.html',
48 {'user': user,
49 'media_entries': media_entries,
50 'pagination': pagination})
51
52 @uses_pagination
53 def user_gallery(request, page):
54 """'Gallery' of a User()"""
55 user = request.db.User.find_one({
56 'username': request.matchdict['user'],
57 'status': 'active'})
58 if not user:
59 return exc.HTTPNotFound()
60
61 cursor = request.db.MediaEntry.find(
62 {'uploader': user['_id'],
63 'state': 'processed'}).sort('created', DESCENDING)
64
65 pagination = Pagination(page, cursor)
66 media_entries = pagination()
67
68 #if no data is available, return NotFound
69 if media_entries == None:
70 return exc.HTTPNotFound()
71
72 return render_to_response(
73 request,
74 'mediagoblin/user_pages/gallery.html',
75 {'user': user,
76 'media_entries': media_entries,
77 'pagination': pagination})
78
79
80 @get_user_media_entry
81 def media_home(request, media):
82 """'Homepage' of a MediaEntry()"""
83 return render_to_response(
84 request,
85 'mediagoblin/user_pages/media.html',
86 {'media': media})
87
88
89 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 5
90
91 def atom_feed(request):
92 """
93 generates the atom feed with the newest images
94 """
95
96 user = request.db.User.find_one({
97 'username': request.matchdict['user'],
98 'status': 'active'})
99 if not user:
100 return exc.HTTPNotFound()
101
102 cursor = request.db.MediaEntry.find({
103 'uploader': user['_id'],
104 'state': 'processed'}) \
105 .sort('created', DESCENDING) \
106 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
107
108 feed = AtomFeed(request.matchdict['user'],
109 feed_url=request.url,
110 url=request.host_url)
111
112 for entry in cursor:
113 feed.add(entry.get('title'),
114 entry.get('description_html'),
115 content_type='html',
116 author=request.matchdict['user'],
117 updated=entry.get('created'),
118 url=entry.url_for_self(request.urlgen))
119
120 return feed.get_response()