post.py 3.4 KB

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