Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

serializers.py 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, Board
  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)
  120. class BoardSerializer(serializers.HyperlinkedModelSerializer):
  121. class Meta:
  122. model = Board
  123. fields = (
  124. "id",
  125. "name",
  126. "pins",
  127. "pins_detail",
  128. "published",
  129. "submitter",
  130. )
  131. read_only_fields = ('submitter', 'published')
  132. pins_detail = PinSerializer(source="pins", many=True, read_only=True)
  133. pins = serializers.HyperlinkedRelatedField(
  134. write_only=True,
  135. queryset=Pin.objects.all(),
  136. view_name="pin-detail",
  137. many=True,
  138. required=False,
  139. )
  140. def create(self, validated_data):
  141. user = self.context['request'].user
  142. if Board.objects.filter(name=validated_data['name'], submitter=user).exists():
  143. raise ValidationError(
  144. detail={"name": "board with this name already exists."}
  145. )
  146. validated_data['submitter'] = user
  147. return super(BoardSerializer, self).create(validated_data)