Skip to content

Commit

Permalink
Put receivers in their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed Jan 23, 2013
1 parent 34181f4 commit 93a1e57
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 72 deletions.
61 changes: 0 additions & 61 deletions agora/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db import models from django.db import models
from django.db.models.signals import post_save, post_delete, pre_delete
from django.utils import timezone from django.utils import timezone
from django.utils.html import conditional_escape from django.utils.html import conditional_escape


Expand Down Expand Up @@ -402,63 +401,3 @@ def setup_onsite(cls):
threads_by_replies = ForumReply.objects.filter(author=user).distinct().values_list("thread", flat=True) threads_by_replies = ForumReply.objects.filter(author=user).distinct().values_list("thread", flat=True)
for thread in set(threads).union(threads_by_replies): for thread in set(threads).union(threads_by_replies):
ForumThread.objects.get(pk=thread).subscribe(user, "onsite") ForumThread.objects.get(pk=thread).subscribe(user, "onsite")


def signal(signals, sender=None):
def _wrapped(func):
if not hasattr(signals, "__iter__"):
_s = [signals]
else:
_s = signals
for s in _s:
s.connect(func, sender=sender)
return func
return _wrapped


@signal(post_save, ForumThread)
def forum_thread_save(sender, instance=None, created=False, **kwargs):
if instance and created:
forum = instance.forum
forum.new_post(instance)

# @@@ this next part could be manager method
post_count, created = UserPostCount.objects.get_or_create(user=instance.author)
post_count.count += 1
post_count.save()


@signal(post_save, ForumReply)
def forum_reply_save(sender, instance=None, created=False, **kwargs):
if instance and created:
thread = instance.thread
thread.new_reply(instance)

# @@@ this next part could be manager method
post_count, created = UserPostCount.objects.get_or_create(user=instance.author)
post_count.count += 1
post_count.save()


@signal(pre_delete, ForumThread)
def forum_thread_delete(sender, **kwargs):
thread = kwargs["instance"]
if thread.id == thread.forum.last_thread_id:
thread.forum.update_last_thread()
thread.forum.update_view_count()
thread.forum.update_post_count()


@signal(pre_delete, ForumReply)
def forum_reply_delete(sender, **kwargs):
reply = kwargs["instance"]
if reply.id == reply.thread.last_reply_id:
reply.thread.update_last_reply()
reply.thread.forum.update_post_count()


@signal([post_save, post_delete], ThreadSubscription)
def forum_subscription_update(sender, instance=None, created=False, **kwargs):
if instance and created:
thread = instance.thread
thread.update_subscriber_count()
52 changes: 52 additions & 0 deletions agora/receivers.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete, pre_delete

from agora.models import ForumThread, ForumReply, ThreadSubscription, UserPostCount


@receiver(post_save, ForumThread)
def forum_thread_save(sender, instance=None, created=False, **kwargs):
if instance and created:
forum = instance.forum
forum.new_post(instance)

# @@@ this next part could be manager method
post_count, created = UserPostCount.objects.get_or_create(user=instance.author)
post_count.count += 1
post_count.save()


@receiver(post_save, ForumReply)
def forum_reply_save(sender, instance=None, created=False, **kwargs):
if instance and created:
thread = instance.thread
thread.new_reply(instance)

# @@@ this next part could be manager method
post_count, created = UserPostCount.objects.get_or_create(user=instance.author)
post_count.count += 1
post_count.save()


@receiver(pre_delete, ForumThread)
def forum_thread_delete(sender, **kwargs):
thread = kwargs["instance"]
if thread.id == thread.forum.last_thread_id:
thread.forum.update_last_thread()
thread.forum.update_view_count()
thread.forum.update_post_count()


@receiver(pre_delete, ForumReply)
def forum_reply_delete(sender, **kwargs):
reply = kwargs["instance"]
if reply.id == reply.thread.last_reply_id:
reply.thread.update_last_reply()
reply.thread.forum.update_post_count()


@receiver([post_save, post_delete], ThreadSubscription)
def forum_subscription_update(sender, instance=None, created=False, **kwargs):
if instance and created:
thread = instance.thread
thread.update_subscriber_count()
24 changes: 13 additions & 11 deletions agora/urls.py
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,17 @@
from django.conf.urls.defaults import * from django.conf.urls.defaults import *


from agora import receivers


urlpatterns = patterns("",
url(r"^$", "agora.views.forums", name="agora_forums"), urlpatterns = patterns("agora.views",
url(r"^category/(\d+)/$", "agora.views.forum_category", name="agora_category"), url(r"^$", "forums", name="agora_forums"),
url(r"^forum/(\d+)/$", "agora.views.forum", name="agora_forum"), url(r"^category/(\d+)/$", "forum_category", name="agora_category"),
url(r"^thread/(\d+)/$", "agora.views.forum_thread", name="agora_thread"), url(r"^forum/(\d+)/$", "forum", name="agora_forum"),
url(r"^new_post/(\d+)/$", "agora.views.post_create", name="agora_post_create"), url(r"^thread/(\d+)/$", "forum_thread", name="agora_thread"),
url(r"^reply/(\d+)/$", "agora.views.reply_create", name="agora_reply_create"), url(r"^new_post/(\d+)/$", "post_create", name="agora_post_create"),
url(r"^post_edit/(thread|reply)/(\d+)/$", "agora.views.post_edit", name="agora_post_edit"), url(r"^reply/(\d+)/$", "reply_create", name="agora_reply_create"),
url(r"^subscribe/(\d+)/$", "agora.views.subscribe", name="agora_subscribe"), url(r"^post_edit/(thread|reply)/(\d+)/$", "post_edit", name="agora_post_edit"),
url(r"^unsubscribe/(\d+)/$", "agora.views.unsubscribe", name="agora_unsubscribe"), url(r"^subscribe/(\d+)/$", "subscribe", name="agora_subscribe"),
url(r"^thread_updates/$", "agora.views.thread_updates", name="agora_thread_updates"), url(r"^unsubscribe/(\d+)/$", "unsubscribe", name="agora_unsubscribe"),
url(r"^thread_updates/$", "thread_updates", name="agora_thread_updates"),
) )

0 comments on commit 93a1e57

Please sign in to comment.