post.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from django.core.urlresolvers import reverse
  2. from rest_framework import serializers
  3. from misago.users.serializers import UserSerializer
  4. from ..models import Post
  5. __all__ = [
  6. 'PostSerializer',
  7. ]
  8. class PostSerializer(serializers.ModelSerializer):
  9. poster = UserSerializer(many=False, read_only=True)
  10. parsed = serializers.SerializerMethodField()
  11. attachments_cache = serializers.SerializerMethodField()
  12. last_editor = serializers.PrimaryKeyRelatedField(read_only=True)
  13. hidden_by = serializers.PrimaryKeyRelatedField(read_only=True)
  14. acl = serializers.SerializerMethodField()
  15. is_read = serializers.SerializerMethodField()
  16. is_new = serializers.SerializerMethodField()
  17. api = serializers.SerializerMethodField()
  18. url = serializers.SerializerMethodField()
  19. class Meta:
  20. model = Post
  21. fields = (
  22. 'id',
  23. 'poster',
  24. 'poster_name',
  25. 'poster_ip',
  26. 'parsed',
  27. 'has_attachments',
  28. 'attachments_cache',
  29. 'posted_on',
  30. 'updated_on',
  31. 'hidden_on',
  32. 'edits',
  33. 'last_editor',
  34. 'last_editor_name',
  35. 'last_editor_slug',
  36. 'hidden_by',
  37. 'hidden_by_name',
  38. 'hidden_by_slug',
  39. 'is_unapproved',
  40. 'is_hidden',
  41. 'is_protected',
  42. 'is_event',
  43. 'event_type',
  44. 'event_context',
  45. 'acl',
  46. 'is_new',
  47. 'is_read',
  48. 'api',
  49. 'url',
  50. )
  51. def get_parsed(self, obj):
  52. if obj.is_valid and not obj.is_event and (not obj.is_hidden or obj.acl['can_see_hidden']):
  53. return obj.parsed
  54. else:
  55. return None
  56. def get_attachments_cache(self, obj):
  57. # TODO: check if user can download attachments before we'll expose them here
  58. return None
  59. def get_acl(self, obj):
  60. try:
  61. return obj.acl
  62. except AttributeError:
  63. return None
  64. def get_is_new(self, obj):
  65. try:
  66. return obj.is_new
  67. except AttributeError:
  68. return None
  69. def get_is_read(self, obj):
  70. try:
  71. return obj.is_read
  72. except AttributeError:
  73. return None
  74. def get_api(self, obj):
  75. return {
  76. 'index': obj.get_api_url(),
  77. 'editor': obj.get_editor_api_url(),
  78. 'read': 'nada',
  79. }
  80. def get_url(self, obj):
  81. return {
  82. 'index': obj.get_absolute_url(),
  83. 'last_editor': self.get_last_editor_url(obj),
  84. 'hidden_by': self.get_hidden_by_url(obj),
  85. }
  86. def get_last_editor_url(self, obj):
  87. if obj.last_editor_id:
  88. return reverse('misago:user', kwargs={
  89. 'pk': obj.last_editor_id,
  90. 'slug': obj.last_editor_slug
  91. })
  92. else:
  93. return None
  94. def get_hidden_by_url(self, obj):
  95. if obj.hidden_by_id:
  96. return reverse('misago:user', kwargs={
  97. 'pk': obj.hidden_by_id,
  98. 'slug': obj.hidden_by_slug
  99. })
  100. else:
  101. return None