Skip to content

Commit

Permalink
Use Djagno 4.2 storages API
Browse files Browse the repository at this point in the history
This solves a problem where a new S3Storage object was made for each
file, which led to terrible performance from loading the cloudfront key.
  • Loading branch information
DeD1rk committed Oct 30, 2023
1 parent 96428b1 commit 54d9554
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 85 deletions.
22 changes: 14 additions & 8 deletions website/activemembers/migrations/0041_alter_membergroup_photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

from django.db import migrations, models

import thaliawebsite.storage.backend
from django.core.files.storage import storages


def forwards_func(apps, schema_editor):
MemberGroup = apps.get_model('activemembers', 'MemberGroup')
MemberGroup = apps.get_model("activemembers", "MemberGroup")

existing_images = []

Expand All @@ -26,23 +26,29 @@ def forwards_func(apps, schema_editor):


def reverse_func(apps, schema_editor):
MemberGroup = apps.get_model('activemembers', 'MemberGroup')
MemberGroup = apps.get_model("activemembers", "MemberGroup")

for item in MemberGroup.objects.filter(photo__isnull=False):
item.photo.name = f"public/{item.photo.name}"
item.save()

class Migration(migrations.Migration):

class Migration(migrations.Migration):
dependencies = [
('activemembers', '0040_remove_multilang_field'),
("activemembers", "0040_remove_multilang_field"),
]

operations = [
migrations.AlterField(
model_name='membergroup',
name='photo',
field=models.ImageField(blank=True, null=True, storage=thaliawebsite.storage.backend.get_public_storage, upload_to='committeephotos/', verbose_name='Image'),
model_name="membergroup",
name="photo",
field=models.ImageField(
blank=True,
null=True,
storage=storages["public"],
upload_to="committeephotos/",
verbose_name="Image",
),
),
migrations.RunPython(forwards_func, reverse_func),
]
4 changes: 2 additions & 2 deletions website/activemembers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.admin import display as admin_display
from django.contrib.auth.models import Permission
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
from django.core.files.storage import storages
from django.core.validators import MinValueValidator
from django.db import models
from django.urls import reverse
Expand All @@ -15,7 +16,6 @@
from thumbnails.fields import ImageField
from tinymce.models import HTMLField

from thaliawebsite.storage.backend import get_public_storage
from utils.media.services import get_upload_to_function
from utils.snippets import overlaps

Expand Down Expand Up @@ -43,7 +43,7 @@ class MemberGroup(models.Model):
verbose_name=_("Image"),
resize_source_to="source",
upload_to=get_upload_to_function("committeephotos"),
storage=get_public_storage,
storage=storages["public"],
null=True,
blank=True,
)
Expand Down
21 changes: 13 additions & 8 deletions website/announcements/migrations/0013_alter_slide_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import announcements.models
from django.db import migrations, models
import thaliawebsite.storage.backend
from django.core.files.storage import storages


def forwards_func(apps, schema_editor):
Slide = apps.get_model('announcements', 'Slide')
Slide = apps.get_model("announcements", "Slide")

existing_images = []

Expand All @@ -26,24 +26,29 @@ def forwards_func(apps, schema_editor):


def reverse_func(apps, schema_editor):
Slide = apps.get_model('announcements', 'Slide')
Slide = apps.get_model("announcements", "Slide")

for item in Slide.objects.filter(content__isnull=False):
item.photo.name = f"public/{item.content.name}"
item.save()


class Migration(migrations.Migration):

dependencies = [
('announcements', '0012_auto_20201209_1704'),
("announcements", "0012_auto_20201209_1704"),
]

operations = [
migrations.AlterField(
model_name='slide',
name='content',
field=models.FileField(help_text='The content of the slide; what image to display.', storage=thaliawebsite.storage.backend.get_public_storage, upload_to='announcements/slides/', validators=[announcements.models.validate_image], verbose_name='Content'),
model_name="slide",
name="content",
field=models.FileField(
help_text="The content of the slide; what image to display.",
storage=storages["public"],
upload_to="announcements/slides/",
validators=[announcements.models.validate_image],
verbose_name="Content",
),
),
migrations.RunPython(forwards_func, reverse_func),
]
4 changes: 2 additions & 2 deletions website/announcements/migrations/0017_alter_slide_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import announcements.models
from django.db import migrations, models
import functools
import thaliawebsite.storage.backend
from django.core.files.storage import storages
import utils.media.services


Expand All @@ -18,7 +18,7 @@ class Migration(migrations.Migration):
name="content",
field=models.FileField(
help_text="The content of the slide; what image to display.",
storage=thaliawebsite.storage.backend.get_public_storage,
storage=storages["public"],
upload_to=functools.partial(
utils.media.services._generic_upload_to,
*(),
Expand Down
4 changes: 2 additions & 2 deletions website/announcements/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The models defined by the announcement package."""
from django.core.files.storage import storages
from django.core.validators import (
FileExtensionValidator,
get_available_image_extensions,
Expand All @@ -11,7 +12,6 @@

from tinymce.models import HTMLField

from thaliawebsite.storage.backend import get_public_storage
from utils.media.services import get_upload_to_function


Expand Down Expand Up @@ -155,7 +155,7 @@ class Slide(models.Model):
help_text=_("The content of the slide; what image to display."),
blank=False,
upload_to=get_upload_to_function("announcements/slides"),
storage=get_public_storage,
storage=storages["public"],
validators=[validate_image],
)

Expand Down
21 changes: 13 additions & 8 deletions website/members/migrations/0041_alter_profile_photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

from django.db import migrations, models
import members.models.profile
import thaliawebsite.storage.backend
from django.core.files.storage import storages


def forwards_func(apps, schema_editor):
Profile = apps.get_model('members', 'Profile')
Profile = apps.get_model("members", "Profile")

existing_images = []

Expand All @@ -26,24 +26,29 @@ def forwards_func(apps, schema_editor):


def reverse_func(apps, schema_editor):
Profile = apps.get_model('members', 'Profile')
Profile = apps.get_model("members", "Profile")

for item in Profile.objects.filter(photo__isnull=False):
item.photo.name = f"public/{item.photo.name}"
item.save()


class Migration(migrations.Migration):

dependencies = [
('members', '0040_remove_profile_auto_renew'),
("members", "0040_remove_profile_auto_renew"),
]

operations = [
migrations.AlterField(
model_name='profile',
name='photo',
field=models.ImageField(blank=True, null=True, storage=thaliawebsite.storage.backend.get_public_storage, upload_to=members.models.profile._profile_image_path, verbose_name='Photo'),
model_name="profile",
name="photo",
field=models.ImageField(
blank=True,
null=True,
storage=storages["public"],
upload_to=members.models.profile._profile_image_path,
verbose_name="Photo",
),
),
migrations.RunPython(forwards_func, reverse_func),
]
4 changes: 2 additions & 2 deletions website/members/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from django.conf import settings
from django.core import validators
from django.core.exceptions import ValidationError
from django.core.files.storage import storages
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from thumbnails.fields import ImageField

from thaliawebsite.storage.backend import get_public_storage
from utils import countries
from utils.media.services import get_upload_to_function

Expand Down Expand Up @@ -208,7 +208,7 @@ class Profile(models.Model):
verbose_name=_("Photo"),
resize_source_to="source",
upload_to=_profile_image_path,
storage=get_public_storage,
storage=storages["public"],
null=True,
blank=True,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

from django.db import migrations, models

import thaliawebsite.storage.backend
from django.core.files.storage import storages


def forwards_func(apps, schema_editor):
MerchandiseItem = apps.get_model('merchandise', 'MerchandiseItem')
MerchandiseItem = apps.get_model("merchandise", "MerchandiseItem")

existing_images = []

Expand All @@ -26,24 +26,25 @@ def forwards_func(apps, schema_editor):


def reverse_func(apps, schema_editor):
MerchandiseItem = apps.get_model('merchandise', 'MerchandiseItem')
MerchandiseItem = apps.get_model("merchandise", "MerchandiseItem")

for item in MerchandiseItem.objects.filter(image__isnull=False):
item.image.name = f"public/{item.image.name}"
item.save()


class Migration(migrations.Migration):

dependencies = [
('merchandise', '0007_alter_merchandiseitem_price'),
("merchandise", "0007_alter_merchandiseitem_price"),
]

operations = [
migrations.AlterField(
model_name='merchandiseitem',
name='image',
field=models.ImageField(storage=thaliawebsite.storage.backend.get_public_storage, upload_to='merchandise'),
model_name="merchandiseitem",
name="image",
field=models.ImageField(
storage=storages["public"], upload_to="merchandise"
),
),
migrations.RunPython(forwards_func, reverse_func),
]
4 changes: 2 additions & 2 deletions website/merchandise/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Models for the merchandise database tables."""
from django.core.files.storage import storages
from django.db import models

from thumbnails.fields import ImageField

from thaliawebsite.storage.backend import get_public_storage
from utils.media.services import get_upload_to_function

_merchandise_photo_upload_to = get_upload_to_function("merchandise")
Expand All @@ -30,7 +30,7 @@ class MerchandiseItem(models.Model):
#: Image of the merchandise item
image = ImageField(
upload_to=_merchandise_photo_upload_to,
storage=get_public_storage,
storage=storages["public"],
resize_source_to="source",
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
# Generated by Django 4.0.4 on 2022-05-27 07:44
from django.db import migrations, models
import thaliawebsite.storage.backend
from django.core.files.storage import storages

class Migration(migrations.Migration):

class Migration(migrations.Migration):
dependencies = [
('partners', '0023_delete_partnerevent'),
("partners", "0023_delete_partnerevent"),
]

operations = [
migrations.AlterField(
model_name='partner',
name='logo',
field=models.ImageField(storage=thaliawebsite.storage.backend.get_public_storage, upload_to='partners/logos/'),
model_name="partner",
name="logo",
field=models.ImageField(
storage=storages["public"], upload_to="partners/logos/"
),
),
migrations.AlterField(
model_name='partner',
name='site_header',
field=models.ImageField(blank=True, null=True, storage=thaliawebsite.storage.backend.get_public_storage, upload_to='partners/headers/'),
model_name="partner",
name="site_header",
field=models.ImageField(
blank=True,
null=True,
storage=storages["public"],
upload_to="partners/headers/",
),
),
migrations.AlterField(
model_name='partnerimage',
name='image',
field=models.ImageField(storage=thaliawebsite.storage.backend.get_public_storage, upload_to='partners/images/'),
model_name="partnerimage",
name="image",
field=models.ImageField(
storage=storages["public"], upload_to="partners/images/"
),
),
migrations.AlterField(
model_name='vacancy',
name='company_logo',
field=models.ImageField(blank=True, null=True, storage=thaliawebsite.storage.backend.get_public_storage, upload_to='partners/vacancy-logos/', verbose_name='company logo'),
model_name="vacancy",
name="company_logo",
field=models.ImageField(
blank=True,
null=True,
storage=storages["public"],
upload_to="partners/vacancy-logos/",
verbose_name="company logo",
),
),
]
17 changes: 11 additions & 6 deletions website/partners/migrations/0026_partner_alternate_logo.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# Generated by Django 4.0.7 on 2022-10-05 18:11

from django.db import migrations, models
import thaliawebsite.storage.backend
from django.core.files.storage import storages


class Migration(migrations.Migration):

dependencies = [
('partners', '0025_migrate_public_folders'),
("partners", "0025_migrate_public_folders"),
]

operations = [
migrations.AddField(
model_name='partner',
name='alternate_logo',
field=models.ImageField(blank=True, help_text='If set, this logo will be shown on the frontpage banner. Please use files with proper transparency.', null=True, storage=thaliawebsite.storage.backend.get_public_storage, upload_to='partners/logos/'),
model_name="partner",
name="alternate_logo",
field=models.ImageField(
blank=True,
help_text="If set, this logo will be shown on the frontpage banner. Please use files with proper transparency.",
null=True,
storage=storages["public"],
upload_to="partners/logos/",
),
),
]
Loading

0 comments on commit 54d9554

Please sign in to comment.