thread.py 5.0 KB

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