thread.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from django.core.urlresolvers import reverse
  2. from rest_framework import serializers
  3. from misago.categories.serializers import BasicCategorySerializer
  4. from misago.threads.models import Thread
  5. __all__ = [
  6. 'ThreadSerializer',
  7. 'ThreadListSerializer',
  8. ]
  9. class ThreadSerializer(serializers.ModelSerializer):
  10. category = BasicCategorySerializer()
  11. is_read = serializers.SerializerMethodField()
  12. last_poster_url = serializers.SerializerMethodField()
  13. absolute_url = serializers.SerializerMethodField()
  14. last_post_url = serializers.SerializerMethodField()
  15. new_post_url = serializers.SerializerMethodField()
  16. subscription = serializers.SerializerMethodField()
  17. api_url = serializers.SerializerMethodField()
  18. acl = serializers.SerializerMethodField()
  19. class Meta:
  20. model = Thread
  21. fields = (
  22. 'id',
  23. 'category',
  24. 'title',
  25. 'weight',
  26. 'replies',
  27. 'has_unapproved_posts',
  28. 'is_read',
  29. 'is_unapproved',
  30. 'is_hidden',
  31. 'is_closed',
  32. 'absolute_url',
  33. 'last_poster_url',
  34. 'last_post_url',
  35. 'new_post_url',
  36. 'subscription',
  37. 'api_url',
  38. 'acl',
  39. )
  40. def get_is_read(self, obj):
  41. try:
  42. return obj.is_read
  43. except AttributeError:
  44. return None
  45. def get_last_poster_url(self, obj):
  46. if obj.last_poster_id:
  47. return reverse('misago:user', kwargs={
  48. 'slug': obj.last_poster_slug,
  49. 'pk': obj.last_poster_id,
  50. })
  51. else:
  52. return None
  53. def get_absolute_url(self, obj):
  54. return obj.get_absolute_url()
  55. def get_last_post_url(self, obj):
  56. return obj.get_last_post_url()
  57. def get_new_post_url(self, obj):
  58. return obj.get_new_post_url()
  59. def get_subscription(self, obj):
  60. try:
  61. return obj.subscription.send_email
  62. except AttributeError:
  63. return None
  64. def get_api_url(self, obj):
  65. return obj.get_api_url()
  66. def get_acl(self, obj):
  67. try:
  68. return obj.acl
  69. except AttributeError:
  70. return {}
  71. class ThreadListSerializer(ThreadSerializer):
  72. category = serializers.PrimaryKeyRelatedField(read_only=True)
  73. last_post = serializers.PrimaryKeyRelatedField(read_only=True)
  74. top_category = serializers.SerializerMethodField()
  75. class Meta:
  76. model = Thread
  77. fields = (
  78. 'id',
  79. 'category',
  80. 'title',
  81. 'weight',
  82. 'top_category',
  83. 'replies',
  84. 'has_unapproved_posts',
  85. 'started_on',
  86. 'last_post',
  87. 'last_poster_name',
  88. 'last_poster_url',
  89. 'last_post_on',
  90. 'is_read',
  91. 'is_unapproved',
  92. 'is_hidden',
  93. 'is_closed',
  94. 'absolute_url',
  95. 'last_post_url',
  96. 'new_post_url',
  97. 'subscription',
  98. 'api_url',
  99. 'acl',
  100. )
  101. def get_top_category(self, obj):
  102. try:
  103. return obj.top_category.pk
  104. except AttributeError:
  105. return None
  106. def get_acl(self, obj):
  107. try:
  108. return obj.acl
  109. except AttributeError:
  110. return {}