serializers.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. acl = serializers.SerializerMethodField()
  24. api = serializers.SerializerMethodField()
  25. url = serializers.SerializerMethodField()
  26. class Meta:
  27. model = Category
  28. fields = [
  29. 'id',
  30. 'parent',
  31. 'name',
  32. 'description',
  33. 'is_closed',
  34. 'threads',
  35. 'posts',
  36. 'last_post_on',
  37. 'last_thread_title',
  38. 'last_poster',
  39. 'last_poster_name',
  40. 'css_class',
  41. 'is_read',
  42. 'subcategories',
  43. 'acl',
  44. 'level',
  45. 'lft',
  46. 'rght',
  47. 'api',
  48. 'url',
  49. ]
  50. def get_description(self, obj):
  51. if obj.description:
  52. return {
  53. 'plain': obj.description,
  54. 'html': format_plaintext_for_html(obj.description),
  55. }
  56. return None
  57. def get_is_read(self, obj):
  58. try:
  59. return obj.is_read
  60. except AttributeError:
  61. return None
  62. def get_subcategories(self, obj):
  63. try:
  64. return CategorySerializer(obj.subcategories, many=True).data
  65. except AttributeError:
  66. return []
  67. def get_acl(self, obj):
  68. try:
  69. return obj.acl
  70. except AttributeError:
  71. return {}
  72. @last_activity_detail
  73. def get_last_poster(self, obj):
  74. if obj.last_poster_id:
  75. return {
  76. 'id': obj.last_poster_id,
  77. 'avatars': obj.last_poster.avatars,
  78. 'url': reverse(
  79. 'misago:user', kwargs={
  80. 'slug': obj.last_poster_slug,
  81. 'pk': obj.last_poster_id,
  82. }
  83. )
  84. }
  85. return None
  86. def get_api(self, obj):
  87. return {
  88. 'read': obj.get_read_api_url(),
  89. }
  90. def get_url(self, obj):
  91. return {
  92. 'index': obj.get_absolute_url(),
  93. 'last_thread': self.get_last_thread_url(obj),
  94. 'last_thread_new': self.get_last_thread_new_url(obj),
  95. 'last_post': self.get_last_post_url(obj),
  96. }
  97. @last_activity_detail
  98. def get_last_thread_url(self, obj):
  99. return obj.get_last_thread_url()
  100. @last_activity_detail
  101. def get_last_thread_new_url(self, obj):
  102. return obj.get_last_thread_new_url()
  103. @last_activity_detail
  104. def get_last_post_url(self, obj):
  105. return obj.get_last_post_url()
  106. class CategoryWithPosterSerializer(CategorySerializer):
  107. last_poster = serializers.SerializerMethodField()
  108. def get_subcategories(self, obj):
  109. try:
  110. return CategoryWithPosterSerializer(obj.subcategories, many=True).data
  111. except AttributeError:
  112. return []
  113. CategoryWithPosterSerializer = CategoryWithPosterSerializer.extend_fields('last_poster')