serializers.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. acl = serializers.SerializerMethodField()
  28. api_url = serializers.SerializerMethodField()
  29. class Meta:
  30. model = Category
  31. fields = [
  32. 'id',
  33. 'parent',
  34. 'name',
  35. 'description',
  36. 'is_closed',
  37. 'threads',
  38. 'posts',
  39. 'last_post_on',
  40. 'last_thread_title',
  41. 'last_poster_name',
  42. 'css_class',
  43. 'is_read',
  44. 'subcategories',
  45. 'absolute_url',
  46. 'last_thread_url',
  47. 'last_post_url',
  48. 'last_poster_url',
  49. 'acl',
  50. 'api_url',
  51. 'level',
  52. 'lft',
  53. 'rght',
  54. ]
  55. def get_description(self, obj):
  56. if obj.description:
  57. return {
  58. 'plain': obj.description,
  59. 'html': format_plaintext_for_html(obj.description),
  60. }
  61. else:
  62. return None
  63. def get_is_read(self, obj):
  64. try:
  65. return obj.is_read
  66. except AttributeError:
  67. return None
  68. def get_subcategories(self, obj):
  69. try:
  70. return CategorySerializer(obj.subcategories, many=True).data
  71. except AttributeError:
  72. return []
  73. def get_absolute_url(self, obj):
  74. return obj.get_absolute_url()
  75. @last_activity_detail
  76. def get_last_thread_url(self, obj):
  77. return obj.get_last_thread_url()
  78. @last_activity_detail
  79. def get_last_post_url(self, obj):
  80. return obj.get_last_post_url()
  81. @last_activity_detail
  82. def get_last_poster_url(self, obj):
  83. if obj.last_poster_id:
  84. return reverse(
  85. 'misago:user', kwargs={
  86. 'slug': obj.last_poster_slug,
  87. 'pk': obj.last_poster_id,
  88. }
  89. )
  90. else:
  91. return None
  92. def get_acl(self, obj):
  93. try:
  94. return obj.acl
  95. except AttributeError:
  96. return {}
  97. def get_api_url(self, obj):
  98. return {
  99. 'read': obj.get_read_api_url(),
  100. }