serializers.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if not all((acl.get('can_see'), acl.get('can_browse'), acl.get('can_see_all_threads'))):
  14. return None
  15. return f(self, obj)
  16. return decorator
  17. class CategorySerializer(serializers.ModelSerializer, MutableFields):
  18. parent = serializers.PrimaryKeyRelatedField(read_only=True)
  19. description = serializers.SerializerMethodField()
  20. is_read = serializers.SerializerMethodField()
  21. subcategories = serializers.SerializerMethodField()
  22. absolute_url = serializers.SerializerMethodField()
  23. last_poster_url = serializers.SerializerMethodField()
  24. last_post_url = serializers.SerializerMethodField()
  25. last_thread_url = serializers.SerializerMethodField()
  26. acl = serializers.SerializerMethodField()
  27. api_url = serializers.SerializerMethodField()
  28. class Meta:
  29. model = Category
  30. fields = (
  31. 'id', 'parent', 'name', 'description', 'is_closed', 'threads', 'posts', 'last_post_on',
  32. 'last_thread_title', 'last_poster_name', 'css_class', 'is_read', 'subcategories',
  33. 'absolute_url', 'last_thread_url', 'last_post_url', 'last_poster_url', 'acl', 'api_url',
  34. 'level', 'lft', 'rght',
  35. )
  36. def get_description(self, obj):
  37. if obj.description:
  38. return {
  39. 'plain': obj.description,
  40. 'html': format_plaintext_for_html(obj.description),
  41. }
  42. else:
  43. return None
  44. def get_is_read(self, obj):
  45. try:
  46. return obj.is_read
  47. except AttributeError:
  48. return None
  49. def get_subcategories(self, obj):
  50. try:
  51. return CategorySerializer(obj.subcategories, many=True).data
  52. except AttributeError:
  53. return []
  54. def get_absolute_url(self, obj):
  55. return obj.get_absolute_url()
  56. @last_activity_detail
  57. def get_last_thread_url(self, obj):
  58. return obj.get_last_thread_url()
  59. @last_activity_detail
  60. def get_last_post_url(self, obj):
  61. return obj.get_last_post_url()
  62. @last_activity_detail
  63. def get_last_poster_url(self, obj):
  64. if obj.last_poster_id:
  65. return reverse(
  66. 'misago:user', kwargs={
  67. 'slug': obj.last_poster_slug,
  68. 'pk': obj.last_poster_id,
  69. }
  70. )
  71. else:
  72. return None
  73. def get_acl(self, obj):
  74. try:
  75. return obj.acl
  76. except AttributeError:
  77. return {}
  78. def get_api_url(self, obj):
  79. return {
  80. 'read': obj.get_read_api_url(),
  81. }