subscribe.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from misago.users.models import (
  2. AUTO_SUBSCRIBE_NONE, AUTO_SUBSCRIBE_NOTIFY, AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL)
  3. from ...models import Subscription
  4. from . import PostingEndpoint, PostingMiddleware
  5. class SubscribeMiddleware(PostingMiddleware):
  6. def use_this_middleware(self):
  7. return self.mode != PostingEndpoint.EDIT
  8. def post_save(self, serializer):
  9. self.subscribe_new_thread()
  10. self.subscribe_replied_thread()
  11. def subscribe_new_thread(self):
  12. if self.mode != PostingEndpoint.START:
  13. return
  14. if self.user.subscribe_to_started_threads == AUTO_SUBSCRIBE_NONE:
  15. return
  16. self.user.subscription_set.create(
  17. category=self.thread.category,
  18. thread=self.thread,
  19. send_email=self.user.subscribe_to_started_threads == AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL
  20. )
  21. def subscribe_replied_thread(self):
  22. if self.mode != PostingEndpoint.REPLY:
  23. return
  24. if self.user.subscribe_to_replied_threads == AUTO_SUBSCRIBE_NONE:
  25. return
  26. try:
  27. subscription = self.user.subscription_set.get(thread=self.thread)
  28. return
  29. except Subscription.DoesNotExist:
  30. pass
  31. # we are replying to thread again?
  32. if self.user.post_set.filter(thread=self.thread).count() > 1:
  33. return
  34. self.user.subscription_set.create(
  35. category=self.thread.category,
  36. thread=self.thread,
  37. send_email=self.user.subscribe_to_replied_threads == AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL
  38. )