serializers.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. class Meta:
  35. model = Category
  36. fields = (
  37. 'id',
  38. 'parent',
  39. 'name',
  40. 'description',
  41. 'is_closed',
  42. 'threads',
  43. 'posts',
  44. 'last_post_on',
  45. 'last_thread_title',
  46. 'last_poster_name',
  47. 'css_class',
  48. 'is_read',
  49. 'subcategories',
  50. 'absolute_url',
  51. 'last_thread_url',
  52. 'last_post_url',
  53. 'last_poster_url',
  54. 'acl',
  55. )
  56. def get_parent(self, obj):
  57. try:
  58. if obj.parent:
  59. return BasicCategorySerializer(obj.parent).data
  60. else:
  61. return None
  62. except AttributeError:
  63. return None
  64. def get_description(self, obj):
  65. if obj.description:
  66. return {
  67. 'plain': obj.description,
  68. 'html': format_plaintext_for_html(obj.description),
  69. }
  70. else:
  71. return None
  72. def get_is_read(self, obj):
  73. try:
  74. return obj.is_read
  75. except AttributeError:
  76. return None
  77. def get_subcategories(self, obj):
  78. try:
  79. return CategorySerializer(obj.subcategories, many=True).data
  80. except AttributeError:
  81. return []
  82. def get_absolute_url(self, obj):
  83. return obj.get_absolute_url()
  84. @last_activity_detail
  85. def get_last_thread_url(self, obj):
  86. return obj.get_last_thread_url()
  87. @last_activity_detail
  88. def get_last_post_url(self, obj):
  89. return obj.get_last_post_url()
  90. @last_activity_detail
  91. def get_last_poster_url(self, obj):
  92. if obj.last_poster_id:
  93. return reverse('misago:user', kwargs={
  94. 'user_slug': obj.last_poster_slug,
  95. 'user_id': obj.last_poster_id,
  96. })
  97. else:
  98. return None
  99. def get_acl(self, obj):
  100. try:
  101. return obj.acl
  102. except AttributeError:
  103. return {}
  104. class IndexCategorySerializer(CategorySerializer):
  105. class Meta:
  106. model = Category
  107. fields = (
  108. 'id',
  109. 'parent',
  110. 'name',
  111. 'description',
  112. 'css_class',
  113. 'absolute_url',
  114. )
  115. class BasicCategorySerializer(CategorySerializer):
  116. class Meta:
  117. model = Category
  118. fields = (
  119. 'id',
  120. 'name',
  121. 'css_class',
  122. 'absolute_url',
  123. )