serializers.py 4.0 KB

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