-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(profiles): pre-cache profile picture dimensions
- Loading branch information
1 parent
574602f
commit 0c021d0
Showing
7 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...accounts/migrations/0027_userprofile_picture_height_userprofile_picture_width_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters