thread.py 4.9 KB

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