Browse Source

Feature: Use new image-creation api instead of the old

pull/139/head
winkidney 5 years ago
parent
commit
33d9aeee48
4 changed files with 26 additions and 17 deletions
  1. +16
    -12
      core/drf_api.py
  2. +3
    -3
      core/models.py
  3. +3
    -0
      core/views.py
  4. +4
    -2
      pinry/static/js/pin-form.js

+ 16
- 12
core/drf_api.py View File

@@ -32,6 +32,7 @@ class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = (
"id",
"image",
"width",
"height",
@@ -42,9 +43,9 @@ class ImageSerializer(serializers.ModelSerializer):
extra_kwargs = {
"width": {"read_only": True},
"height": {"read_only": True},
"image": {"read_only": True},
}


standard = ThumbnailSerializer(read_only=True)
thumbnail = ThumbnailSerializer(read_only=True)
square = ThumbnailSerializer(read_only=True)
@@ -74,9 +75,12 @@ class PinSerializer(serializers.HyperlinkedModelSerializer):
"description",
"referer",
"image",
"image_by_id",
"tags",
)

extra_kwargs = {
"submitter": {"read_only": True},
}

tags = serializers.SlugRelatedField(
many=True,
@@ -84,19 +88,22 @@ class PinSerializer(serializers.HyperlinkedModelSerializer):
queryset=Tag.objects.all(),
slug_field="name",
)
image = ImageSerializer(required=False)
image = ImageSerializer(required=False, read_only=True)
image_by_id = serializers.PrimaryKeyRelatedField(
queryset=Image.objects.all(),
write_only=True,
)

def create(self, validated_data):
image_file = validated_data.pop('image')
if validated_data['url']:
submitter = self.context['request'].user
image = validated_data.pop("image_by_id")
if 'url' in validated_data and validated_data['url']:
image = Image.objects.create_for_url(
validated_data['url'],
validated_data['referer'],
)
else:
image = Image.objects.create(image=image_file['image'])
pin = Pin.objects.create(image=image, **validated_data)
tags = validated_data.pop('tag_list')
pin = Pin.objects.create(submitter=submitter, image=image, **validated_data)
if tags:
pin.tags.set(*tags)
return pin
@@ -105,8 +112,5 @@ class PinSerializer(serializers.HyperlinkedModelSerializer):
tags = validated_data.pop('tag_list')
if tags:
instance.tags.set(*tags)
image_file = validated_data.pop('image', None)
if image_file:
image = Image.objects.create(image=image_file['image'])
instance.image = image
validated_data.pop('image_id')
return super(PinSerializer, self).update(instance, validated_data)

+ 3
- 3
core/models.py View File

@@ -72,9 +72,9 @@ class Image(BaseImage):

class Pin(models.Model):
submitter = models.ForeignKey(User)
url = models.URLField(null=True)
origin = models.URLField(null=True)
referer = models.URLField(null=True)
url = models.URLField(null=True, blank=True)
origin = models.URLField(null=True, blank=True)
referer = models.URLField(null=True, blank=True)
description = models.TextField(blank=True, null=True)
image = models.ForeignKey(Image, related_name='pin')
published = models.DateTimeField(auto_now_add=True)


+ 3
- 0
core/views.py View File

@@ -16,6 +16,9 @@ class ImageViewSet(mixins.CreateModelMixin, GenericViewSet):
queryset = Image.objects.all()
serializer_class = api.ImageSerializer

def create(self, request, *args, **kwargs):
super(ImageViewSet, self).create(request, *args, **kwargs)


class PinViewSet(viewsets.ModelViewSet):
queryset = Pin.objects.all()


+ 4
- 2
pinry/static/js/pin-form.js View File

@@ -9,6 +9,8 @@


$(window).load(function() {
var api_base = "/api/v2/";

var uploadedImage = false;
var editedPin = null;

@@ -99,8 +101,8 @@ $(window).load(function() {
}
// Drag and drop upload
$('#pin-form-image-upload').dropzone({
url: '/pins/create-image/',
paramName: 'qqfile',
url: api_base + "images/",
paramName: 'image',
parallelUploads: 1,
uploadMultiple: false,
maxFiles: 1,


Loading…
Cancel
Save