thread.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. from django.urls import reverse
  2. from rest_framework import serializers
  3. from misago.categories.serializers import BasicCategorySerializer
  4. from ..models import Thread
  5. from .poll import PollSerializer
  6. from .threadparticipant import ThreadParticipantSerializer
  7. __all__ = [
  8. 'ThreadSerializer',
  9. 'PrivateThreadSerializer',
  10. 'ThreadsListSerializer',
  11. ]
  12. class ThreadSerializer(serializers.ModelSerializer):
  13. category = BasicCategorySerializer(many=False, read_only=True)
  14. acl = serializers.SerializerMethodField()
  15. is_new = serializers.SerializerMethodField()
  16. is_read = serializers.SerializerMethodField()
  17. path = BasicCategorySerializer(many=True, read_only=True)
  18. poll = PollSerializer(many=False, read_only=True)
  19. subscription = serializers.SerializerMethodField()
  20. api = serializers.SerializerMethodField()
  21. url = serializers.SerializerMethodField()
  22. class Meta:
  23. model = Thread
  24. fields = (
  25. 'id',
  26. 'category',
  27. 'title',
  28. 'replies',
  29. 'has_unapproved_posts',
  30. 'started_on',
  31. 'last_post_on',
  32. 'last_post',
  33. 'last_poster_name',
  34. 'is_unapproved',
  35. 'is_hidden',
  36. 'is_closed',
  37. 'weight',
  38. 'acl',
  39. 'is_new',
  40. 'is_read',
  41. 'path',
  42. 'poll',
  43. 'subscription',
  44. 'api',
  45. 'url',
  46. )
  47. def get_acl(self, obj):
  48. try:
  49. return obj.acl
  50. except AttributeError:
  51. return {}
  52. def get_is_new(self, obj):
  53. try:
  54. return obj.is_new
  55. except AttributeError:
  56. return None
  57. def get_is_read(self, obj):
  58. try:
  59. return obj.is_read
  60. except AttributeError:
  61. return None
  62. def get_top_category(self, obj):
  63. try:
  64. return obj.top_category.pk
  65. except AttributeError:
  66. return None
  67. def get_subscription(self, obj):
  68. try:
  69. return obj.subscription.send_email
  70. except AttributeError:
  71. return None
  72. def get_api(self, obj):
  73. return {
  74. 'index': obj.get_api_url(),
  75. 'editor': obj.get_editor_api_url(),
  76. 'merge': obj.get_merge_api_url(),
  77. 'poll': obj.get_poll_api_url(),
  78. 'posts': {
  79. 'index': obj.get_posts_api_url(),
  80. 'merge': obj.get_post_merge_api_url(),
  81. 'move': obj.get_post_move_api_url(),
  82. 'split': obj.get_post_split_api_url()
  83. }
  84. }
  85. def get_url(self, obj):
  86. return {
  87. 'index': obj.get_absolute_url(),
  88. 'new_post': obj.get_new_post_url(),
  89. 'last_post': obj.get_last_post_url(),
  90. 'unapproved_post': obj.get_unapproved_post_url(),
  91. 'last_poster': self.get_last_poster_url(obj),
  92. }
  93. def get_last_poster_url(self, obj):
  94. if obj.last_poster_id:
  95. return reverse('misago:user', kwargs={
  96. 'slug': obj.last_poster_slug,
  97. 'pk': obj.last_poster_id,
  98. })
  99. else:
  100. return None
  101. class PrivateThreadSerializer(ThreadSerializer):
  102. participants = serializers.SerializerMethodField()
  103. class Meta:
  104. model = Thread
  105. fields = (
  106. 'id',
  107. 'category',
  108. 'title',
  109. 'replies',
  110. 'has_unapproved_posts',
  111. 'started_on',
  112. 'last_post_on',
  113. 'last_post',
  114. 'last_poster_name',
  115. 'is_unapproved',
  116. 'is_hidden',
  117. 'is_closed',
  118. 'weight',
  119. 'acl',
  120. 'is_new',
  121. 'is_read',
  122. 'participants',
  123. 'path',
  124. 'poll',
  125. 'subscription',
  126. 'api',
  127. 'url',
  128. )
  129. def get_participants(self, obj):
  130. return ThreadParticipantSerializer(obj.participants_list, many=True).data
  131. class ThreadsListSerializer(ThreadSerializer):
  132. category = serializers.PrimaryKeyRelatedField(read_only=True)
  133. last_post = serializers.PrimaryKeyRelatedField(read_only=True)
  134. top_category = serializers.SerializerMethodField()
  135. class Meta:
  136. model = Thread
  137. fields = (
  138. 'id',
  139. 'category',
  140. 'title',
  141. 'replies',
  142. 'has_unapproved_posts',
  143. 'started_on',
  144. 'last_post_on',
  145. 'last_post',
  146. 'last_poster_name',
  147. 'weight',
  148. 'is_unapproved',
  149. 'is_hidden',
  150. 'is_closed',
  151. 'acl',
  152. 'is_new',
  153. 'is_read',
  154. 'subscription',
  155. 'top_category',
  156. 'api',
  157. 'url',
  158. )