serializers.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from django.core.urlresolvers import reverse
  2. from rest_framework import serializers
  3. from misago.categories.models import Category
  4. from misago.core.utils import format_plaintext_for_html
  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.PrimaryKeyRelatedField(read_only=True)
  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. 'level',
  58. 'lft',
  59. 'rght',
  60. )
  61. def get_description(self, obj):
  62. if obj.description:
  63. return {
  64. 'plain': obj.description,
  65. 'html': format_plaintext_for_html(obj.description),
  66. }
  67. else:
  68. return None
  69. def get_is_read(self, obj):
  70. try:
  71. return obj.is_read
  72. except AttributeError:
  73. return None
  74. def get_subcategories(self, obj):
  75. try:
  76. return CategorySerializer(obj.subcategories, many=True).data
  77. except AttributeError:
  78. return []
  79. def get_absolute_url(self, obj):
  80. return obj.get_absolute_url()
  81. @last_activity_detail
  82. def get_last_thread_url(self, obj):
  83. return obj.get_last_thread_url()
  84. @last_activity_detail
  85. def get_last_post_url(self, obj):
  86. return obj.get_last_post_url()
  87. @last_activity_detail
  88. def get_last_poster_url(self, obj):
  89. if obj.last_poster_id:
  90. return reverse('misago:user', kwargs={
  91. 'slug': obj.last_poster_slug,
  92. 'pk': obj.last_poster_id,
  93. })
  94. else:
  95. return None
  96. def get_acl(self, obj):
  97. try:
  98. return obj.acl
  99. except AttributeError:
  100. return {}
  101. def get_api_url(self, obj):
  102. return {
  103. 'read': obj.get_api_read_url(),
  104. }
  105. class BasicCategorySerializer(CategorySerializer):
  106. class Meta:
  107. model = Category
  108. fields = (
  109. 'id',
  110. 'parent',
  111. 'name',
  112. 'description',
  113. 'is_closed',
  114. 'css_class',
  115. 'absolute_url',
  116. 'api_url',
  117. 'level',
  118. 'lft',
  119. 'rght',
  120. 'is_read',
  121. )