thread.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from django.shortcuts import get_object_or_404
  2. from django.utils.translation import gettext as _
  3. from misago.acl import add_acl
  4. from misago.categories.models import Category
  5. from misago.core.shortcuts import validate_slug
  6. from misago.readtracker.threadstracker import make_read_aware
  7. from ..models import Thread
  8. from ..permissions.threads import allow_see_thread
  9. from ..serializers import ThreadSerializer
  10. from ..subscriptions import make_subscription_aware
  11. BASE_RELATIONS = ('category', 'starter', 'starter__rank', 'starter__ban_cache', 'starter__online_tracker')
  12. class ViewModel(object):
  13. def __init__(self, request, pk, slug=None, read_aware=False, subscription_aware=False, select_for_update=False):
  14. model = self.get_thread(request, pk, slug, select_for_update)
  15. model.path = self.get_thread_path(model.category)
  16. add_acl(request.user, model.category)
  17. add_acl(request.user, model)
  18. if read_aware:
  19. make_read_aware(request.user, model)
  20. if subscription_aware:
  21. make_subscription_aware(request.user, model)
  22. self._model = model
  23. self._category = model.category
  24. self._path = model.path
  25. @property
  26. def model(self):
  27. return self._model
  28. @property
  29. def category(self):
  30. return self._category
  31. @property
  32. def path(self):
  33. return self._path
  34. def get_thread(self, request, pk, slug=None, select_for_update=False):
  35. raise NotImplementedError('Thread view model has to implement get_thread(request, pk, slug=None)')
  36. def get_thread_path(self, category):
  37. thread_path = []
  38. if category.level:
  39. categories = Category.objects.filter(
  40. tree_id=category.tree_id,
  41. lft__lte=category.lft,
  42. rght__gte=category.rght
  43. ).order_by('level')
  44. thread_path = list(categories)
  45. else:
  46. thread_path = [category]
  47. thread_path[0].name = self.get_root_name()
  48. return thread_path
  49. def get_root_name(self):
  50. raise NotImplementedError('Thread view model has to implement get_root_name()')
  51. def get_frontend_context(self):
  52. return ThreadSerializer(self._model).data
  53. def get_template_context(self):
  54. return {
  55. 'thread': self._model,
  56. 'category': self._category,
  57. 'breadcrumbs': self._path
  58. }
  59. class ForumThread(ViewModel):
  60. def get_thread(self, request, pk, slug=None, select_for_update=False):
  61. if select_for_update:
  62. queryset = Thread.objects.select_for_update().select_related('category')
  63. else:
  64. queryset = Thread.objects.select_related(*BASE_RELATIONS)
  65. thread = get_object_or_404(
  66. queryset,
  67. pk=pk,
  68. category__tree_id=Category.objects.root_category().tree_id
  69. )
  70. allow_see_thread(request.user, thread)
  71. if slug:
  72. validate_slug(thread, slug)
  73. return thread
  74. def get_root_name(self):
  75. return _("Threads")