Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Put receivers in their own module

  • Loading branch information...
commit 93a1e57e812c898fe006ea1246960c0ef91dd087 1 parent 34181f4
@paltman paltman authored
Showing with 65 additions and 72 deletions.
  1. +0 −61 agora/models.py
  2. +52 −0 agora/receivers.py
  3. +13 −11 agora/urls.py
View
61 agora/models.py
@@ -4,7 +4,6 @@
from django.core.urlresolvers import reverse
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.html import conditional_escape
@@ -402,63 +401,3 @@ def setup_onsite(cls):
threads_by_replies = ForumReply.objects.filter(author=user).distinct().values_list("thread", flat=True)
for thread in set(threads).union(threads_by_replies):
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()
View
52 agora/receivers.py
@@ -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()
View
24 agora/urls.py
@@ -1,15 +1,17 @@
from django.conf.urls.defaults import *
+from agora import receivers
-urlpatterns = patterns("",
- url(r"^$", "agora.views.forums", name="agora_forums"),
- url(r"^category/(\d+)/$", "agora.views.forum_category", name="agora_category"),
- url(r"^forum/(\d+)/$", "agora.views.forum", name="agora_forum"),
- url(r"^thread/(\d+)/$", "agora.views.forum_thread", name="agora_thread"),
- url(r"^new_post/(\d+)/$", "agora.views.post_create", name="agora_post_create"),
- url(r"^reply/(\d+)/$", "agora.views.reply_create", name="agora_reply_create"),
- url(r"^post_edit/(thread|reply)/(\d+)/$", "agora.views.post_edit", name="agora_post_edit"),
- url(r"^subscribe/(\d+)/$", "agora.views.subscribe", name="agora_subscribe"),
- url(r"^unsubscribe/(\d+)/$", "agora.views.unsubscribe", name="agora_unsubscribe"),
- url(r"^thread_updates/$", "agora.views.thread_updates", name="agora_thread_updates"),
+
+urlpatterns = patterns("agora.views",
+ url(r"^$", "forums", name="agora_forums"),
+ url(r"^category/(\d+)/$", "forum_category", name="agora_category"),
+ url(r"^forum/(\d+)/$", "forum", name="agora_forum"),
+ url(r"^thread/(\d+)/$", "forum_thread", name="agora_thread"),
+ url(r"^new_post/(\d+)/$", "post_create", name="agora_post_create"),
+ url(r"^reply/(\d+)/$", "reply_create", name="agora_reply_create"),
+ url(r"^post_edit/(thread|reply)/(\d+)/$", "post_edit", name="agora_post_edit"),
+ url(r"^subscribe/(\d+)/$", "subscribe", name="agora_subscribe"),
+ url(r"^unsubscribe/(\d+)/$", "unsubscribe", name="agora_unsubscribe"),
+ url(r"^thread_updates/$", "thread_updates", name="agora_thread_updates"),
)
Please sign in to comment.
Something went wrong with that request. Please try again.