thread.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. 'title',
  24. 'weight',
  25. 'category',
  26. 'replies',
  27. 'is_closed',
  28. 'is_read',
  29. 'absolute_url',
  30. 'last_poster_url',
  31. 'last_post_url',
  32. 'new_post_url',
  33. 'subscription',
  34. 'api_url',
  35. 'acl',
  36. )
  37. def get_is_read(self, obj):
  38. try:
  39. return obj.is_read
  40. except AttributeError:
  41. return None
  42. def get_last_poster_url(self, obj):
  43. if obj.last_poster_id:
  44. return reverse('misago:user', kwargs={
  45. 'slug': obj.last_poster_slug,
  46. 'pk': obj.last_poster_id,
  47. })
  48. else:
  49. return None
  50. def get_absolute_url(self, obj):
  51. return obj.get_absolute_url()
  52. def get_last_post_url(self, obj):
  53. return obj.get_last_post_url()
  54. def get_new_post_url(self, obj):
  55. return obj.get_new_post_url()
  56. def get_subscription(self, obj):
  57. try:
  58. return obj.subscription.send_email
  59. except AttributeError:
  60. return None
  61. def get_api_url(self, obj):
  62. return obj.get_api_url()
  63. def get_acl(self, obj):
  64. try:
  65. return obj.acl
  66. except AttributeError:
  67. return {}
  68. class ThreadListSerializer(ThreadSerializer):
  69. category = serializers.PrimaryKeyRelatedField(read_only=True)
  70. last_post = serializers.PrimaryKeyRelatedField(read_only=True)
  71. top_category = serializers.SerializerMethodField()
  72. class Meta:
  73. model = Thread
  74. fields = (
  75. 'id',
  76. 'title',
  77. 'weight',
  78. 'category',
  79. 'top_category',
  80. 'replies',
  81. 'started_on',
  82. 'last_post',
  83. 'last_poster_name',
  84. 'last_poster_url',
  85. 'last_post_on',
  86. 'is_closed',
  87. 'is_read',
  88. 'absolute_url',
  89. 'last_post_url',
  90. 'new_post_url',
  91. 'subscription',
  92. 'api_url',
  93. 'acl',
  94. )
  95. def get_top_category(self, obj):
  96. try:
  97. return obj.top_category.pk
  98. except AttributeError:
  99. return None
  100. def get_acl(self, obj):
  101. try:
  102. return obj.acl
  103. except AttributeError:
  104. return {}