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