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