Skip to content

Commit

Permalink
feat(profiles): pre-cache profile picture dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Jan 23, 2024
1 parent 574602f commit 0c021d0
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 5 deletions.
5 changes: 5 additions & 0 deletions charts/templates/web-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ metadata:
{{- include "fiesta.componentLabels" "web" | nindent 4 }}
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
selector:
matchLabels:
{{- include "fiesta.selectorLabels" . | nindent 6 }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.8 on 2024-01-23 20:16

import apps.accounts.models.profile
import apps.files.storage
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0026_alter_userprofile_avatar_slug'),
]

operations = [
migrations.AddField(
model_name='userprofile',
name='picture_height',
field=models.PositiveSmallIntegerField(blank=True, editable=False, null=True, verbose_name='profile picture width'),
),
migrations.AddField(
model_name='userprofile',
name='picture_width',
field=models.PositiveSmallIntegerField(blank=True, editable=False, null=True, verbose_name='profile picture width'),
),
migrations.AlterField(
model_name='userprofile',
name='picture',
field=models.ImageField(blank=True, height_field='picture_height', null=True, storage=apps.files.storage.NamespacedFilesStorage('profile-picture', has_permission=apps.accounts.models.profile.has_permission_for_profile_picture_view), upload_to=apps.files.storage.NamespacedFilesStorage.upload_to, verbose_name='profile picture', width_field='picture_width'),
),
]
14 changes: 14 additions & 0 deletions fiesta/apps/accounts/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ class Gender(TextChoices):
verbose_name=_("profile picture"),
null=True,
blank=True,
width_field="picture_width",
height_field="picture_height",
)
picture_width = models.PositiveSmallIntegerField(
verbose_name=_("profile picture width"),
null=True,
blank=True,
editable=False,
)
picture_height = models.PositiveSmallIntegerField(
verbose_name=_("profile picture width"),
null=True,
blank=True,
editable=False,
)

interests = ArrayFieldWithDisplayableChoices(
Expand Down
17 changes: 12 additions & 5 deletions fiesta/apps/fiestatables/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import django_tables2 as tables
from django.contrib.humanize.templatetags.humanize import NaturalTimeFormatter
from django.db.models import Choices, Model
from django.db.models.fields.files import FieldFile
from django.db.models.fields.files import ImageFieldFile
from django.utils.html import format_html
from django_countries import countries
from django_countries.fields import Country
Expand All @@ -14,24 +14,31 @@

# TODO: probably not needed anymore
class ImageColumn(tables.Column):
def render(self, value: FieldFile):
return format_html('<img src="{}" class="h-12" />', value.url)
def render(self, value: ImageFieldFile):
return format_html(
'<img src="{}" class="h-12" width={} height={} />',
value.url,
value.width,
value.height,
)

def value(self, record, value):
return value.url


class AvatarColumn(ImageColumn):
def render(self, value: FieldFile):
def render(self, value: ImageFieldFile):
return format_html(
"""
<div class="avatar">
<div class="w-12 rounded-xl">
<img src="{}" />
<img src="{}" width={} height={} />
</div>
</div>
""",
value.url,
value.width,
value.height,
)


Expand Down
27 changes: 27 additions & 0 deletions fiesta/apps/files/_patches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from __future__ import annotations


def monkeypatch_image_dimensions_caching():
from django.core.files.images import ImageFile, get_image_dimensions

def _get_image_dimensions(self):
from numbers import Number

if not hasattr(self, "_dimensions_cache"):
close = self.closed
if self.field.width_field and self.field.height_field:
width = getattr(self.instance, self.field.width_field)
height = getattr(self.instance, self.field.height_field)
# check if the fields have proper values
if isinstance(width, Number) and isinstance(height, Number):
self._dimensions_cache = (width, height)
else:
self.open()
self._dimensions_cache = get_image_dimensions(self, close=close)
else:
self.open()
self._dimensions_cache = get_image_dimensions(self, close=close)

return self._dimensions_cache

ImageFile._get_image_dimensions = _get_image_dimensions
5 changes: 5 additions & 0 deletions fiesta/apps/files/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

from django.apps import AppConfig

from apps.files._patches import monkeypatch_image_dimensions_caching


class FilesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.files"

def ready(self):
monkeypatch_image_dimensions_caching()
1 change: 1 addition & 0 deletions fiesta/fiesta/settings/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def DEFAULT_FROM_EMAIL(self):
"apps.pickup_system.apps.PickupSystemConfig",
"apps.dashboard.apps.DashboardConfig",
"apps.esncards.apps.ESNcardsConfig",
"apps.files.apps.FilesConfig",
"apps.fiestaforms.apps.FiestaFormsConfig",
"apps.fiestarequests.apps.FiestaRequestsConfig",
"apps.fiestatables.apps.FiestaTablesConfig",
Expand Down

0 comments on commit 0c021d0

Please sign in to comment.