serializers.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from django.core.urlresolvers import reverse
  2. from rest_framework import serializers
  3. from misago.core.utils import format_plaintext_for_html
  4. from misago.categories.models import Category
  5. __all__ = [
  6. 'BasicCategorySerializer',
  7. 'IndexCategorySerializer',
  8. 'CategorySerializer',
  9. ]
  10. def last_activity_detail(f):
  11. """util for serializing last activity details"""
  12. def decorator(self, obj):
  13. if not obj.last_thread_id:
  14. return None
  15. acl = self.get_acl(obj)
  16. if not all((
  17. acl.get('can_see'),
  18. acl.get('can_browse'),
  19. acl.get('can_see_all_threads')
  20. )):
  21. return None
  22. return f(self, obj)
  23. return decorator
  24. class CategorySerializer(serializers.ModelSerializer):
  25. parent = serializers.SerializerMethodField()
  26. description = serializers.SerializerMethodField()
  27. is_read = serializers.SerializerMethodField()
  28. subcategories = serializers.SerializerMethodField()
  29. absolute_url = serializers.SerializerMethodField()
  30. last_poster_url = serializers.SerializerMethodField()
  31. last_post_url = serializers.SerializerMethodField()
  32. last_thread_url = serializers.SerializerMethodField()
  33. acl = serializers.SerializerMethodField()
  34. api_url = serializers.SerializerMethodField()
  35. class Meta:
  36. model = Category
  37. fields = (
  38. 'id',
  39. 'parent',
  40. 'name',
  41. 'description',
  42. 'is_closed',
  43. 'threads',
  44. 'posts',
  45. 'last_post_on',
  46. 'last_thread_title',
  47. 'last_poster_name',
  48. 'css_class',
  49. 'is_read',
  50. 'subcategories',
  51. 'absolute_url',
  52. 'last_thread_url',
  53. 'last_post_url',
  54. 'last_poster_url',
  55. 'acl',
  56. 'api_url',
  57. )
  58. def get_parent(self, obj):
  59. try:
  60. if obj.parent:
  61. return BasicCategorySerializer(obj.parent).data
  62. else:
  63. return None
  64. except AttributeError:
  65. return None
  66. def get_description(self, obj):
  67. if obj.description:
  68. return {
  69. 'plain': obj.description,
  70. 'html': format_plaintext_for_html(obj.description),
  71. }
  72. else:
  73. return None
  74. def get_is_read(self, obj):
  75. try:
  76. return obj.is_read
  77. except AttributeError:
  78. return None
  79. def get_subcategories(self, obj):
  80. try:
  81. return CategorySerializer(obj.subcategories, many=True).data
  82. except AttributeError:
  83. return []
  84. def get_absolute_url(self, obj):
  85. return obj.get_absolute_url()
  86. @last_activity_detail
  87. def get_last_thread_url(self, obj):
  88. return obj.get_last_thread_url()
  89. @last_activity_detail
  90. def get_last_post_url(self, obj):
  91. return obj.get_last_post_url()
  92. @last_activity_detail
  93. def get_last_poster_url(self, obj):
  94. if obj.last_poster_id:
  95. return reverse('misago:user', kwargs={
  96. 'user_slug': obj.last_poster_slug,
  97. 'user_id': obj.last_poster_id,
  98. })
  99. else:
  100. return None
  101. def get_acl(self, obj):
  102. try:
  103. return obj.acl
  104. except AttributeError:
  105. return {}
  106. def get_api_url(self, obj):
  107. return {
  108. 'read': obj.get_api_read_url(),
  109. }
  110. class IndexCategorySerializer(CategorySerializer):
  111. class Meta:
  112. model = Category
  113. fields = (
  114. 'id',
  115. 'parent',
  116. 'name',
  117. 'description',
  118. 'css_class',
  119. 'absolute_url',
  120. 'api_url',
  121. )
  122. class BasicCategorySerializer(CategorySerializer):
  123. class Meta:
  124. model = Category
  125. fields = (
  126. 'id',
  127. 'name',
  128. 'css_class',
  129. 'absolute_url',
  130. )