25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

140 lines
3.8 KiB

  1. from django.conf import settings
  2. from rest_framework import serializers
  3. from rest_framework.exceptions import ValidationError
  4. from taggit.models import Tag
  5. from core.models import Image
  6. from core.models import Pin
  7. from django_images.models import Thumbnail
  8. from users.models import User
  9. class UserSerializer(serializers.HyperlinkedModelSerializer):
  10. class Meta:
  11. model = User
  12. fields = (
  13. 'username',
  14. 'gravatar',
  15. settings.DRF_URL_FIELD_NAME,
  16. )
  17. class ThumbnailSerializer(serializers.HyperlinkedModelSerializer):
  18. class Meta:
  19. model = Thumbnail
  20. fields = (
  21. "image",
  22. "width",
  23. "height",
  24. )
  25. class ImageSerializer(serializers.ModelSerializer):
  26. class Meta:
  27. model = Image
  28. fields = (
  29. "id",
  30. "image",
  31. "width",
  32. "height",
  33. "standard",
  34. "thumbnail",
  35. "square",
  36. )
  37. extra_kwargs = {
  38. "width": {"read_only": True},
  39. "height": {"read_only": True},
  40. }
  41. standard = ThumbnailSerializer(read_only=True)
  42. thumbnail = ThumbnailSerializer(read_only=True)
  43. square = ThumbnailSerializer(read_only=True)
  44. def create(self, validated_data):
  45. image = super(ImageSerializer, self).create(validated_data)
  46. Thumbnail.objects.get_or_create_at_sizes(image, settings.IMAGE_SIZES.keys())
  47. return image
  48. class TagSerializer(serializers.SlugRelatedField):
  49. class Meta:
  50. model = Tag
  51. fields = ("name",)
  52. queryset = Tag.objects.all()
  53. def __init__(self, **kwargs):
  54. super(TagSerializer, self).__init__(
  55. slug_field="name",
  56. **kwargs
  57. )
  58. def to_internal_value(self, data):
  59. obj, _ = self.get_queryset().get_or_create(
  60. defaults={self.slug_field: data, "slug": data},
  61. **{self.slug_field: data}
  62. )
  63. return obj
  64. class PinSerializer(serializers.HyperlinkedModelSerializer):
  65. class Meta:
  66. model = Pin
  67. fields = (
  68. settings.DRF_URL_FIELD_NAME,
  69. "id",
  70. "submitter",
  71. "url",
  72. "origin",
  73. "description",
  74. "referer",
  75. "image",
  76. "image_by_id",
  77. "tags",
  78. )
  79. submitter = UserSerializer(read_only=True)
  80. tags = TagSerializer(
  81. many=True,
  82. source="tag_list",
  83. required=False,
  84. )
  85. image = ImageSerializer(required=False, read_only=True)
  86. image_by_id = serializers.PrimaryKeyRelatedField(
  87. queryset=Image.objects.all(),
  88. write_only=True,
  89. required=False,
  90. )
  91. def create(self, validated_data):
  92. if 'url' not in validated_data and\
  93. 'image_by_id' not in validated_data:
  94. raise ValidationError(
  95. detail={
  96. "url-or-image": "Either url or image_by_id is required."
  97. },
  98. )
  99. submitter = self.context['request'].user
  100. if 'url' in validated_data and validated_data['url']:
  101. url = validated_data['url']
  102. image = Image.objects.create_for_url(
  103. url,
  104. validated_data.get('referer', url),
  105. )
  106. else:
  107. image = validated_data.pop("image_by_id")
  108. tags = validated_data.pop('tag_list', [])
  109. pin = Pin.objects.create(submitter=submitter, image=image, **validated_data)
  110. if tags:
  111. pin.tags.set(*tags)
  112. return pin
  113. def update(self, instance, validated_data):
  114. tags = validated_data.pop('tag_list', None)
  115. if tags:
  116. instance.tags.set(*tags)
  117. # change for image-id or image is not allowed
  118. validated_data.pop('image_by_id', None)
  119. return super(PinSerializer, self).update(instance, validated_data)