| 1 |
import math |
|---|
| 2 |
|
|---|
| 3 |
from django.shortcuts import get_object_or_404 |
|---|
| 4 |
from django.http import HttpResponseRedirect |
|---|
| 5 |
from django.contrib.auth.models import User |
|---|
| 6 |
from django.contrib.auth.decorators import login_required |
|---|
| 7 |
from django.conf import settings |
|---|
| 8 |
from django.core.urlresolvers import reverse |
|---|
| 9 |
from django.db import connection |
|---|
| 10 |
|
|---|
| 11 |
from pybb.util import render_to, paged, build_form |
|---|
| 12 |
from pybb.models import Category, Forum, Topic, Post, Profile, PrivateMessage |
|---|
| 13 |
from pybb.forms import AddPostForm, EditProfileForm, EditPostForm, UserSearchForm, CreatePMForm |
|---|
| 14 |
|
|---|
| 15 |
def index_ctx(request): |
|---|
| 16 |
quick = {'posts': Post.objects.count(), |
|---|
| 17 |
'topics': Topic.objects.count(), |
|---|
| 18 |
'users': User.objects.count(), |
|---|
| 19 |
'last_topics': Topic.objects.all().select_related()[:settings.PYBB_QUICK_TOPICS_NUMBER], |
|---|
| 20 |
'last_posts': Post.objects.order_by('-created').select_related()[:settings.PYBB_QUICK_POSTS_NUMBER], |
|---|
| 21 |
} |
|---|
| 22 |
|
|---|
| 23 |
cats = {} |
|---|
| 24 |
forums = {} |
|---|
| 25 |
|
|---|
| 26 |
for forum in Forum.objects.all().select_related(): |
|---|
| 27 |
cat = cats.setdefault(forum.category.id, |
|---|
| 28 |
{'cat': forum.category, 'forums': []}) |
|---|
| 29 |
cat['forums'].append(forum) |
|---|
| 30 |
forums[forum.id] = forum |
|---|
| 31 |
|
|---|
| 32 |
cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position) |
|---|
| 33 |
cats = sorted(cats.values(), cmpdef) |
|---|
| 34 |
|
|---|
| 35 |
return {'cats': cats, |
|---|
| 36 |
'quick': quick, |
|---|
| 37 |
} |
|---|
| 38 |
index = render_to('pybb/index.html')(index_ctx) |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
def show_category_ctx(request, category_id): |
|---|
| 42 |
category = Category.objects.get(pk=category_id) |
|---|
| 43 |
quick = {'posts': category.posts.count(), |
|---|
| 44 |
'topics': category.topics.count(), |
|---|
| 45 |
'last_topics': category.topics.select_related()[:settings.PYBB_QUICK_TOPICS_NUMBER], |
|---|
| 46 |
'last_posts': category.posts.order_by('-created').select_related()[:settings.PYBB_QUICK_POSTS_NUMBER], |
|---|
| 47 |
} |
|---|
| 48 |
return {'category': category, |
|---|
| 49 |
'quick': quick, |
|---|
| 50 |
} |
|---|
| 51 |
show_category = render_to('pybb/category.html')(show_category_ctx) |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
@paged('topics', settings.PYBB_FORUM_PAGE_SIZE) |
|---|
| 55 |
def show_forum_ctx(request, forum_id): |
|---|
| 56 |
forum = Forum.objects.get(pk=forum_id) |
|---|
| 57 |
topics = forum.topics.filter(sticky=False).select_related() |
|---|
| 58 |
quick = {'posts': forum.posts.count(), |
|---|
| 59 |
'topics': forum.topics.count(), |
|---|
| 60 |
'last_topics': forum.topics.all().select_related()[:settings.PYBB_QUICK_TOPICS_NUMBER], |
|---|
| 61 |
'last_posts': forum.posts.order_by('-created').select_related()[:settings.PYBB_QUICK_POSTS_NUMBER], |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
return {'forum': forum, |
|---|
| 65 |
'topics': topics, |
|---|
| 66 |
'sticky_topics': forum.topics.filter(sticky=True), |
|---|
| 67 |
'quick': quick, |
|---|
| 68 |
'paged_qs': topics, |
|---|
| 69 |
} |
|---|
| 70 |
show_forum = render_to('pybb/forum.html')(show_forum_ctx) |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
@paged('posts', settings.PYBB_TOPIC_PAGE_SIZE) |
|---|
| 74 |
def show_topic_ctx(request, topic_id): |
|---|
| 75 |
topic = Topic.objects.select_related().get(pk=topic_id) |
|---|
| 76 |
topic.views += 1 |
|---|
| 77 |
topic.save() |
|---|
| 78 |
|
|---|
| 79 |
last_post = topic.posts.order_by('-created')[0] |
|---|
| 80 |
|
|---|
| 81 |
if request.user.is_authenticated(): |
|---|
| 82 |
topic.update_read(request.user) |
|---|
| 83 |
|
|---|
| 84 |
posts = topic.posts.all().select_related() |
|---|
| 85 |
|
|---|
| 86 |
profiles = Profile.objects.filter(user__pk__in=set(x.user.id for x in posts)) |
|---|
| 87 |
profiles = dict((x.user_id, x) for x in profiles) |
|---|
| 88 |
|
|---|
| 89 |
for post in posts: |
|---|
| 90 |
post.user.pybb_profile = profiles[post.user.id] |
|---|
| 91 |
|
|---|
| 92 |
initial = {} |
|---|
| 93 |
if request.user.is_authenticated(): |
|---|
| 94 |
initial = {'markup': request.user.pybb_profile.markup} |
|---|
| 95 |
form = AddPostForm(topic=topic, initial=initial) |
|---|
| 96 |
|
|---|
| 97 |
moderator = request.user.is_superuser or\ |
|---|
| 98 |
request.user in topic.forum.moderators.all() |
|---|
| 99 |
if request.user.is_authenticated() and request.user in topic.subscribers.all(): |
|---|
| 100 |
subscribed = True |
|---|
| 101 |
else: |
|---|
| 102 |
subscribed = False |
|---|
| 103 |
return {'topic': topic, |
|---|
| 104 |
'last_post': last_post, |
|---|
| 105 |
'form': form, |
|---|
| 106 |
'moderator': moderator, |
|---|
| 107 |
'subscribed': subscribed, |
|---|
| 108 |
'paged_qs': posts, |
|---|
| 109 |
} |
|---|
| 110 |
show_topic = render_to('pybb/topic.html')(show_topic_ctx) |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
@login_required |
|---|
| 114 |
def add_post_ctx(request, forum_id, topic_id): |
|---|
| 115 |
forum = None |
|---|
| 116 |
topic = None |
|---|
| 117 |
|
|---|
| 118 |
if forum_id: |
|---|
| 119 |
forum = get_object_or_404(Forum, pk=forum_id) |
|---|
| 120 |
elif topic_id: |
|---|
| 121 |
topic = get_object_or_404(Topic, pk=topic_id) |
|---|
| 122 |
|
|---|
| 123 |
if topic and topic.closed: |
|---|
| 124 |
return HttpResponseRedirect(topic.get_absolute_url()) |
|---|
| 125 |
|
|---|
| 126 |
ip = request.META.get('REMOTE_ADDR', '') |
|---|
| 127 |
form = build_form(AddPostForm, request, topic=topic, forum=forum, |
|---|
| 128 |
user=request.user, ip=ip, |
|---|
| 129 |
initial={'markup': request.user.pybb_profile.markup}) |
|---|
| 130 |
|
|---|
| 131 |
if form.is_valid(): |
|---|
| 132 |
post = form.save(); |
|---|
| 133 |
return HttpResponseRedirect(post.get_absolute_url()) |
|---|
| 134 |
|
|---|
| 135 |
return {'form': form, |
|---|
| 136 |
'topic': topic, |
|---|
| 137 |
'forum': forum, |
|---|
| 138 |
} |
|---|
| 139 |
add_post = render_to('pybb/add_post.html')(add_post_ctx) |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
def user_ctx(request, username): |
|---|
| 143 |
user = get_object_or_404(User, username=username) |
|---|
| 144 |
topic_count = Topic.objects.filter(user=user).count() |
|---|
| 145 |
return {'profile': user, |
|---|
| 146 |
'topic_count': topic_count, |
|---|
| 147 |
} |
|---|
| 148 |
user = render_to('pybb/user.html')(user_ctx) |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
def show_post(request, post_id): |
|---|
| 152 |
post = get_object_or_404(Post, pk=post_id) |
|---|
| 153 |
count = post.topic.posts.filter(created__lt=post.created).count() + 1 |
|---|
| 154 |
page = math.ceil(count / float(settings.PYBB_TOPIC_PAGE_SIZE)) |
|---|
| 155 |
url = '%s?page=%d#post-%d' % (reverse('topic', args=[post.topic.id]), page, post.id) |
|---|
| 156 |
return HttpResponseRedirect(url) |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
@login_required |
|---|
| 160 |
def edit_profile_ctx(request): |
|---|
| 161 |
form = build_form(EditProfileForm, request, instance=request.user.pybb_profile) |
|---|
| 162 |
if form.is_valid(): |
|---|
| 163 |
form.save() |
|---|
| 164 |
return HttpResponseRedirect(reverse('edit_profile')) |
|---|
| 165 |
return {'form': form, |
|---|
| 166 |
'profile': request.user.pybb_profile, |
|---|
| 167 |
} |
|---|
| 168 |
edit_profile = render_to('pybb/edit_profile.html')(edit_profile_ctx) |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
@login_required |
|---|
| 172 |
def edit_post_ctx(request, post_id): |
|---|
| 173 |
from pybb.templatetags.pybb_extras import pybb_editable_by |
|---|
| 174 |
|
|---|
| 175 |
post = get_object_or_404(Post, pk=post_id) |
|---|
| 176 |
if not pybb_editable_by(post, request.user): |
|---|
| 177 |
return HttpResponseRedirect(post.get_absolute_url()) |
|---|
| 178 |
|
|---|
| 179 |
form = build_form(EditPostForm, request, instance=post) |
|---|
| 180 |
|
|---|
| 181 |
if form.is_valid(): |
|---|
| 182 |
post = form.save() |
|---|
| 183 |
return HttpResponseRedirect(post.get_absolute_url()) |
|---|
| 184 |
|
|---|
| 185 |
return {'form': form, |
|---|
| 186 |
'post': post, |
|---|
| 187 |
} |
|---|
| 188 |
edit_post = render_to('pybb/edit_post.html')(edit_post_ctx) |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
@login_required |
|---|
| 192 |
def stick_topic(request, topic_id): |
|---|
| 193 |
from pybb.templatetags.pybb_extras import pybb_moderated_by |
|---|
| 194 |
|
|---|
| 195 |
topic = get_object_or_404(Topic, pk=topic_id) |
|---|
| 196 |
if pybb_moderated_by(topic, request.user): |
|---|
| 197 |
if not topic.sticky: |
|---|
| 198 |
topic.sticky = True |
|---|
| 199 |
topic.save() |
|---|
| 200 |
return HttpResponseRedirect(topic.get_absolute_url()) |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
@login_required |
|---|
| 204 |
def unstick_topic(request, topic_id): |
|---|
| 205 |
from pybb.templatetags.pybb_extras import pybb_moderated_by |
|---|
| 206 |
|
|---|
| 207 |
topic = get_object_or_404(Topic, pk=topic_id) |
|---|
| 208 |
if pybb_moderated_by(topic, request.user): |
|---|
| 209 |
if topic.sticky: |
|---|
| 210 |
topic.sticky = False |
|---|
| 211 |
topic.save() |
|---|
| 212 |
return HttpResponseRedirect(topic.get_absolute_url()) |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
@login_required |
|---|
| 216 |
def delete_post_ctx(request, post_id): |
|---|
| 217 |
post = get_object_or_404(Post, pk=post_id) |
|---|
| 218 |
last_post = post.topic.posts.order_by('-created')[0] |
|---|
| 219 |
topic = post.topic |
|---|
| 220 |
|
|---|
| 221 |
allowed = False |
|---|
| 222 |
if request.user.is_superuser or\ |
|---|
| 223 |
request.user in post.topic.forum.moderators.all() or \ |
|---|
| 224 |
(post.user == request.user and post == last_post): |
|---|
| 225 |
allowed = True |
|---|
| 226 |
|
|---|
| 227 |
if not allowed: |
|---|
| 228 |
return HttpResponseRedirect(post.get_absolute_url()) |
|---|
| 229 |
|
|---|
| 230 |
topic = post.topic |
|---|
| 231 |
forum = post.topic.forum |
|---|
| 232 |
post.delete() |
|---|
| 233 |
|
|---|
| 234 |
try: |
|---|
| 235 |
Topic.objects.get(pk=topic.id) |
|---|
| 236 |
except Topic.DoesNotExist: |
|---|
| 237 |
return HttpResponseRedirect(forum.get_absolute_url()) |
|---|
| 238 |
else: |
|---|
| 239 |
return HttpResponseRedirect(topic.get_absolute_url()) |
|---|
| 240 |
delete_post = render_to('pybb/delete_post.html')(delete_post_ctx) |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
@login_required |
|---|
| 244 |
def close_topic(request, topic_id): |
|---|
| 245 |
from pybb.templatetags.pybb_extras import pybb_moderated_by |
|---|
| 246 |
|
|---|
| 247 |
topic = get_object_or_404(Topic, pk=topic_id) |
|---|
| 248 |
if pybb_moderated_by(topic, request.user): |
|---|
| 249 |
if not topic.closed: |
|---|
| 250 |
topic.closed = True |
|---|
| 251 |
topic.save() |
|---|
| 252 |
return HttpResponseRedirect(topic.get_absolute_url()) |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
@login_required |
|---|
| 256 |
def open_topic(request, topic_id): |
|---|
| 257 |
from pybb.templatetags.pybb_extras import pybb_moderated_by |
|---|
| 258 |
|
|---|
| 259 |
topic = get_object_or_404(Topic, pk=topic_id) |
|---|
| 260 |
if pybb_moderated_by(topic, request.user): |
|---|
| 261 |
if topic.closed: |
|---|
| 262 |
topic.closed = False |
|---|
| 263 |
topic.save() |
|---|
| 264 |
return HttpResponseRedirect(topic.get_absolute_url()) |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
@render_to('pybb/users.html') |
|---|
| 268 |
@paged('users', settings.PYBB_USERS_PAGE_SIZE) |
|---|
| 269 |
def users_ctx(request): |
|---|
| 270 |
users = User.objects.order_by('username') |
|---|
| 271 |
form = UserSearchForm(request.GET) |
|---|
| 272 |
users = form.filter(users) |
|---|
| 273 |
return {'paged_qs': users, |
|---|
| 274 |
'form': form, |
|---|
| 275 |
} |
|---|
| 276 |
users = paged('users', settings.PYBB_USERS_PAGE_SIZE)(users_ctx) |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
@login_required |
|---|
| 280 |
def delete_subscription(request, topic_id): |
|---|
| 281 |
topic = get_object_or_404(Topic, pk=topic_id) |
|---|
| 282 |
topic.subscribers.remove(request.user) |
|---|
| 283 |
if 'from_topic' in request.GET: |
|---|
| 284 |
return HttpResponseRedirect(reverse('topic', args=[topic.id])) |
|---|
| 285 |
else: |
|---|
| 286 |
return HttpResponseRedirect(reverse('edit_profile')) |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
@login_required |
|---|
| 290 |
def add_subscription(request, topic_id): |
|---|
| 291 |
topic = get_object_or_404(Topic, pk=topic_id) |
|---|
| 292 |
topic.subscribers.add(request.user) |
|---|
| 293 |
return HttpResponseRedirect(reverse('topic', args=[topic.id])) |
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
@login_required |
|---|
| 297 |
def create_pm_ctx(request): |
|---|
| 298 |
recipient = request.GET.get('recipient', '') |
|---|
| 299 |
form = build_form(CreatePMForm, request, user=request.user, |
|---|
| 300 |
initial={'markup': request.user.pybb_profile.markup, |
|---|
| 301 |
'recipient': recipient}) |
|---|
| 302 |
|
|---|
| 303 |
if form.is_valid(): |
|---|
| 304 |
post = form.save(); |
|---|
| 305 |
return HttpResponseRedirect(reverse('pybb_pm_outbox')) |
|---|
| 306 |
|
|---|
| 307 |
return {'form': form, |
|---|
| 308 |
} |
|---|
| 309 |
create_pm = render_to('pybb/pm/create_pm.html')(create_pm_ctx) |
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
@login_required |
|---|
| 313 |
def pm_outbox_ctx(request): |
|---|
| 314 |
messages = PrivateMessage.objects.filter(src_user=request.user) |
|---|
| 315 |
return {'messages': messages, |
|---|
| 316 |
} |
|---|
| 317 |
pm_outbox = render_to('pybb/pm/outbox.html')(pm_outbox_ctx) |
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
@login_required |
|---|
| 321 |
def pm_inbox_ctx(request): |
|---|
| 322 |
messages = PrivateMessage.objects.filter(dst_user=request.user) |
|---|
| 323 |
return {'messages': messages, |
|---|
| 324 |
} |
|---|
| 325 |
pm_inbox = render_to('pybb/pm/inbox.html')(pm_inbox_ctx) |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
@login_required |
|---|
| 329 |
def show_pm_ctx(request, pm_id): |
|---|
| 330 |
msg = get_object_or_404(PrivateMessage, pk=pm_id) |
|---|
| 331 |
if not request.user in [msg.dst_user, msg.src_user]: |
|---|
| 332 |
return HttpRedirectException('/') |
|---|
| 333 |
if request.user == msg.dst_user: |
|---|
| 334 |
inbox = True |
|---|
| 335 |
post_user = msg.src_user |
|---|
| 336 |
else: |
|---|
| 337 |
inbox = False |
|---|
| 338 |
post_user = msg.dst_user |
|---|
| 339 |
return {'msg': msg, |
|---|
| 340 |
'inbox': inbox, |
|---|
| 341 |
'post_user': post_user, |
|---|
| 342 |
} |
|---|
| 343 |
show_pm = render_to('pybb/pm/message.html')(show_pm_ctx) |
|---|