Excitedly send a user a message that their comment was posted.
[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 return render_to_response(
52 request,
53 'mediagoblin/user_pages/user.html',
54 {'user': user,
55 'media_entries': media_entries,
56 'pagination': pagination})
57
58 @uses_pagination
59 def 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
78 return render_to_response(
79 request,
80 'mediagoblin/user_pages/gallery.html',
81 {'user': user,
82 'media_entries': media_entries,
83 'pagination': pagination})
84
85
86 @get_user_media_entry
87 @uses_pagination
88 def media_home(request, media, **kwargs):
89 """
90 'Homepage' of a MediaEntry()
91 """
92
93 comment_form = user_forms.MediaCommentForm(request.POST)
94
95 (comments, pagination) = media.get_comments(kwargs.get('page'))
96
97 return render_to_response(
98 request,
99 'mediagoblin/user_pages/media.html',
100 {'media': media,
101 'comments': comments,
102 'pagination': pagination,
103 'comment_form': comment_form})
104
105
106 @require_active_login
107 def media_post_comment(request):
108 """
109 recieves POST from a MediaEntry() comment form, saves the comment.
110 """
111 comment = request.db.MediaComment()
112 comment['media_entry'] = ObjectId(request.matchdict['media'])
113 comment['author'] = request.user['_id']
114 comment['content'] = request.POST['comment']
115
116 comment['content_html'] = cleaned_markdown_conversion(comment['content'])
117
118 comment.save()
119
120 messages.add_message(
121 request, messages.SUCCESS,
122 'Comment posted!')
123
124 return redirect(request, 'mediagoblin.user_pages.media_home',
125 media = request.matchdict['media'],
126 user = request.matchdict['user'])
127
128
129 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 5
130
131 def atom_feed(request):
132 """
133 generates the atom feed with the newest images
134 """
135
136 user = request.db.User.find_one({
137 'username': request.matchdict['user'],
138 'status': 'active'})
139 if not user:
140 return exc.HTTPNotFound()
141
142 cursor = request.db.MediaEntry.find({
143 'uploader': user['_id'],
144 'state': 'processed'}) \
145 .sort('created', DESCENDING) \
146 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
147
148 feed = AtomFeed(request.matchdict['user'],
149 feed_url=request.url,
150 url=request.host_url)
151
152 for entry in cursor:
153 feed.add(entry.get('title'),
154 entry.get('description_html'),
155 content_type='html',
156 author=request.matchdict['user'],
157 updated=entry.get('created'),
158 url=entry.url_for_self(request.urlgen))
159
160 return feed.get_response()