Commit 57115bb0 authored by Welch Canavan's avatar Welch Canavan
Browse files

setup CDN for media

Closes #12
parent 2ba7e640
......@@ -118,11 +118,10 @@ STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
AWS_ACCESS_KEY_ID = env("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env("AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = env("AWS_STORAGE_BUCKET_NAME")
AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
AWS_S3_CUSTOM_DOMAIN = env("AWS_S3_CUSTOM_DOMAIN")
AWS_S3_OBJECT_PARAMETERS = {"CacheControl": "max-age=86400"}
AWS_DEFAULT_ACL = "public-read"
AWS_LOCATION = "uploads"
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
MEDIA_URL = f"{AWS_S3_CUSTOM_DOMAIN}/"
MEDIAFILES_LOCATION = "media"
DEFAULT_FILE_STORAGE = "content.storage_backends.MediaStorage"
......
# Generated by Django 2.2b1 on 2019-03-03 13:25
# Generated by Django 2.2b1 on 2019-03-03 17:26
from django.db import migrations, models
......@@ -13,11 +13,21 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='image',
name='thumbnail',
field=models.ImageField(null=True, upload_to=''),
field=models.ImageField(null=True, upload_to='uploads/0afba0b32c'),
),
migrations.AddField(
model_name='image',
name='thumbnail_webp',
field=models.ImageField(null=True, upload_to='uploads/0afba0b32c'),
),
migrations.AddField(
model_name='image',
name='webp',
field=models.ImageField(null=True, upload_to=''),
field=models.ImageField(null=True, upload_to='uploads/0afba0b32c'),
),
migrations.AlterField(
model_name='image',
name='upload',
field=models.ImageField(height_field='height', upload_to='uploads/0afba0b32c', width_field='width'),
),
]
......@@ -46,6 +46,7 @@ class Image(DirtyFieldsMixin, models.Model):
"Work", on_delete=models.CASCADE, blank=True, null=True, related_name="images"
)
thumbnail = models.ImageField(null=True, upload_to=file_path)
thumbnail_webp = models.ImageField(null=True, upload_to=file_path)
upload = models.ImageField(height_field="height", width_field="width", upload_to=file_path)
uploaded_at = models.DateTimeField(auto_now_add=True)
webp = models.ImageField(null=True, upload_to=file_path)
......@@ -53,9 +54,9 @@ class Image(DirtyFieldsMixin, models.Model):
def set_thumbnail(self):
original_image = PillowImage.open(self.upload)
filename = f"{self.upload.name.split('.')[0]}.square.jpg"
filename = f"{self.upload.name.split('.')[0]}.thumbnail.jpg"
image_io = BytesIO()
image = ImageOps.fit(original_image, (1000, 1000), PillowImage.ANTIALIAS)
image = ImageOps.fit(original_image, (500, 500), PillowImage.ANTIALIAS)
image = image.convert(mode="RGB")
image.save(image_io, "JPEG", quality=70, optimize=True, progressive=True)
......@@ -65,6 +66,19 @@ class Image(DirtyFieldsMixin, models.Model):
save=False
)
def set_thumbnail_webp(self):
original_image = PillowImage.open(self.upload)
filename = f"{self.upload.name.split('.')[0]}.thumbnail.webp"
image_io = BytesIO()
image = ImageOps.fit(original_image, (500, 500), PillowImage.ANTIALIAS)
image.save(image_io, "WEBP", quality=70, method=6)
self.thumbnail_webp.save(
filename,
ContentFile(image_io.getvalue()),
save=False
)
def set_webp(self):
original_image = PillowImage.open(self.upload)
filename = f"{self.upload.name.split('.')[0]}.webp"
......@@ -81,6 +95,7 @@ class Image(DirtyFieldsMixin, models.Model):
def save(self, *args, **kwargs):
if not self.pk or 'upload' in self.get_dirty_fields():
self.set_thumbnail()
self.set_thumbnail_webp()
self.set_webp()
super().save(*args, **kwargs)
......@@ -147,7 +162,8 @@ class Post(DirtyFieldsMixin, ContentBase):
}
r = requests.post(url, headers=headers, json=payload)
self.shortlink = r.json()[u'id']
if r.status_code is 200:
self.shortlink = r.json()[u'id']
def save(self, *args, **kwargs):
if not self.shortlink or 'slug' in self.get_dirty_fields():
......
from rest_framework import serializers
from .models import Image, Post, Work
class AlternateImagesField(serializers.Field):
class ThumbnailField(serializers.Field):
def to_representation(self, value):
return {
"thumbnail": {
"src": value.thumbnail.url,
"height": value.thumbnail.height,
"width": value.thumbnail.width
},
"webp": {
"src": value.webp.url
},
"src": value.thumbnail.url,
"height": value.thumbnail.height,
"width": value.thumbnail.width,
"webp_src": value.thumbnail_webp.url
}
class ImageSerializer(serializers.ModelSerializer):
alt_imgs = AlternateImagesField(source='*')
# alt_imgs = AlternateImagesField(source='*')
thumbnail = ThumbnailField(source='*')
webp_src = serializers.CharField(source='webp.url', read_only=True)
src = serializers.CharField(source='upload.url', read_only=True)
class Meta:
model = Image
fields = ("src", "caption", "width", "height", "alt_imgs")
# def get_test(self, obj):
# return obj.webp.url
fields = ("src", "caption", "width", "height", "webp_src", "thumbnail")
class BaseContentSerializer(serializers.HyperlinkedModelSerializer):
......@@ -38,7 +33,6 @@ class PostSerializer(BaseContentSerializer):
fields = (
"body",
"hero",
"images",
"published_at",
"shortlink",
"slug",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment