updatestats.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django.db.models import F
  2. from misago.categories import THREADS_ROOT_NAME
  3. from . import PostingEndpoint, PostingMiddleware
  4. class UpdateStatsMiddleware(PostingMiddleware):
  5. def save(self, serializer):
  6. self.update_category(self.thread.category, self.thread)
  7. self.update_thread(self.thread, self.post)
  8. self.update_user(self.user)
  9. def update_category(self, category, thread):
  10. if self.mode == PostingEndpoint.START:
  11. category.threads = F('threads') + 1
  12. if self.mode != PostingEndpoint.EDIT:
  13. category.set_last_thread(thread)
  14. category.posts = F('posts') + 1
  15. category.update_all = True
  16. def update_thread(self, thread, post):
  17. if self.mode == PostingEndpoint.START:
  18. thread.set_first_post(post)
  19. if self.mode != PostingEndpoint.EDIT:
  20. thread.set_last_post(post)
  21. if self.mode == PostingEndpoint.REPLY:
  22. thread.replies = F('replies') + 1
  23. thread.update_all = True
  24. def update_user(self, user):
  25. if self.thread.thread_type.root_name == THREADS_ROOT_NAME:
  26. if self.mode == PostingEndpoint.START:
  27. user.threads = F('threads') + 1
  28. user.update_fields.append('threads')
  29. if self.mode != PostingEndpoint.EDIT:
  30. user.posts = F('posts') + 1
  31. user.update_fields.append('posts')