Adds license header
[mediagoblin.git] / mediagoblin / user_pages / views.py
CommitLineData
9a16e16f
SS
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
1c63ad5d 17from webob import exc
52359e91
CAW
18
19from mediagoblin import messages
9074ee7c 20from mediagoblin.db.util import DESCENDING, ObjectId
95e6da02
CAW
21from mediagoblin.util import (
22 Pagination, render_to_response, redirect, cleaned_markdown_conversion)
9074ee7c 23from mediagoblin.user_pages import forms as user_forms
f6249408 24
9074ee7c
JW
25from mediagoblin.decorators import uses_pagination, get_user_media_entry, \
26 require_active_login
9a16e16f 27
00c39256 28from werkzeug.contrib.atom import AtomFeed
1301a8ad 29
9074ee7c 30
3eb6fc4f 31@uses_pagination
1301a8ad 32def user_home(request, page):
9a16e16f 33 """'Homepage' of a User()"""
7acdbfd3 34 user = request.db.User.find_one({
990d3b69 35 'username': request.matchdict['user']})
7acdbfd3
SS
36 if not user:
37 return exc.HTTPNotFound()
990d3b69
CAW
38 elif user['status'] != u'active':
39 return render_to_response(
40 request,
41 'mediagoblin/user_pages/user.html',
42 {'user': user})
9a16e16f 43
434b3221 44 cursor = request.db.MediaEntry.find(
16509be1 45 {'uploader': user['_id'],
434b3221 46 'state': 'processed'}).sort('created', DESCENDING)
9a16e16f 47
1301a8ad 48 pagination = Pagination(page, cursor)
ca3ca51c 49 media_entries = pagination()
44e3e917 50
ae85ed0f
BK
51 #if no data is available, return NotFound
52 if media_entries == None:
53 return exc.HTTPNotFound()
54
5949be9a
CAW
55 user_gallery_url = request.urlgen(
56 'mediagoblin.user_pages.user_gallery',
57 user=user['username'])
58
9038c9f9
CAW
59 return render_to_response(
60 request,
c9c24934
E
61 'mediagoblin/user_pages/user.html',
62 {'user': user,
5949be9a 63 'user_gallery_url': user_gallery_url,
c9c24934
E
64 'media_entries': media_entries,
65 'pagination': pagination})
f6249408 66
184f2240 67@uses_pagination
68def user_gallery(request, page):
69 """'Gallery' of a User()"""
70 user = request.db.User.find_one({
71 'username': request.matchdict['user'],
72 'status': 'active'})
73 if not user:
74 return exc.HTTPNotFound()
75
76 cursor = request.db.MediaEntry.find(
77 {'uploader': user['_id'],
78 'state': 'processed'}).sort('created', DESCENDING)
79
80 pagination = Pagination(page, cursor)
81 media_entries = pagination()
82
83 #if no data is available, return NotFound
84 if media_entries == None:
85 return exc.HTTPNotFound()
86
4b5f5a08 87 return render_to_response(
88 request,
89 'mediagoblin/user_pages/gallery.html',
90 {'user': user,
91 'media_entries': media_entries,
92 'pagination': pagination})
184f2240 93
6f59a3a3 94MEDIA_COMMENTS_PER_PAGE = 50
434b3221 95
01674e10 96@get_user_media_entry
9074ee7c 97@uses_pagination
6f59a3a3 98def media_home(request, media, page, **kwargs):
9074ee7c
JW
99 """
100 'Homepage' of a MediaEntry()
101 """
af2fcba5
JW
102 if ObjectId(request.matchdict.get('comment')):
103 pagination = Pagination(
104 page, media.get_comments(), MEDIA_COMMENTS_PER_PAGE,
105 ObjectId(request.matchdict.get('comment')))
106 else:
107 pagination = Pagination(
108 page, media.get_comments(), MEDIA_COMMENTS_PER_PAGE)
9074ee7c 109
6f59a3a3 110 comments = pagination()
9074ee7c 111
6f59a3a3 112 comment_form = user_forms.MediaCommentForm(request.POST)
9074ee7c 113
9038c9f9
CAW
114 return render_to_response(
115 request,
c9c24934 116 'mediagoblin/user_pages/media.html',
9074ee7c
JW
117 {'media': media,
118 'comments': comments,
119 'pagination': pagination,
120 'comment_form': comment_form})
121
95e6da02 122
9074ee7c
JW
123@require_active_login
124def media_post_comment(request):
125 """
126 recieves POST from a MediaEntry() comment form, saves the comment.
127 """
128 comment = request.db.MediaComment()
129 comment['media_entry'] = ObjectId(request.matchdict['media'])
130 comment['author'] = request.user['_id']
f646f5d3 131 comment['content'] = request.POST['comment_content']
9074ee7c 132
95e6da02 133 comment['content_html'] = cleaned_markdown_conversion(comment['content'])
9074ee7c
JW
134
135 comment.save()
b5d3aec6 136
52359e91
CAW
137 messages.add_message(
138 request, messages.SUCCESS,
139 'Comment posted!')
140
9074ee7c
JW
141 return redirect(request, 'mediagoblin.user_pages.media_home',
142 media = request.matchdict['media'],
143 user = request.matchdict['user'])
00c39256 144
95e6da02 145
00c39256
BK
146ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 5
147
148def atom_feed(request):
149 """
150 generates the atom feed with the newest images
151 """
152
153 user = request.db.User.find_one({
154 'username': request.matchdict['user'],
155 'status': 'active'})
156 if not user:
157 return exc.HTTPNotFound()
158
159 cursor = request.db.MediaEntry.find({
160 'uploader': user['_id'],
161 'state': 'processed'}) \
162 .sort('created', DESCENDING) \
163 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
164
165 feed = AtomFeed(request.matchdict['user'],
166 feed_url=request.url,
167 url=request.host_url)
44e2da2f 168
00c39256
BK
169 for entry in cursor:
170 feed.add(entry.get('title'),
44e2da2f 171 entry.get('description_html'),
00c39256
BK
172 content_type='html',
173 author=request.matchdict['user'],
174 updated=entry.get('created'),
175 url=entry.url_for_self(request.urlgen))
176
9074ee7c 177 return feed.get_response()