serializers.py 3.0 KB

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