updatestats.py 1.3 KB

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