Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Combine migrations

Roland Geidercee0f43c38f2
diff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.py
index ec25479fe..343625224 100644
--- a/wger/core/tests/base_testcase.py
+++ b/wger/core/tests/base_testcase.py
@@ -169,7 +169,7 @@ class BaseTestCase:
# Clear MEDIA_ROOT folder
if self.media_root:
- shutil.rmtree(self.media_root)
+ self.media_root.cleanup()
def init_media_root(self):
"""
@@ -177,24 +177,33 @@ class BaseTestCase:
This is error-prone and ugly, but it's probably ok for the time being
"""
- self.media_root = tempfile.mkdtemp()
- settings.MEDIA_ROOT = self.media_root
+ self.media_root = tempfile.TemporaryDirectory()
+ settings.MEDIA_ROOT = self.media_root.name
- os.makedirs(self.media_root + '/exercise-images/1/')
- os.makedirs(self.media_root + '/exercise-images/2/')
-
- shutil.copy(
- 'wger/exercises/tests/protestschwein.jpg',
- self.media_root + '/exercise-images/1/protestschwein.jpg',
+ pathlib.Path(self.media_root.name, 'exercise-images', '1').mkdir(
+ parents=True, exist_ok=True
)
- shutil.copy(
- 'wger/exercises/tests/wildschwein.jpg',
- self.media_root + '/exercise-images/1/wildschwein.jpg',
+ pathlib.Path(self.media_root.name, 'exercise-images', '2').mkdir(
+ parents=True, exist_ok=True
)
- shutil.copy(
- 'wger/exercises/tests/wildschwein.jpg',
- self.media_root + '/exercise-images/2/wildschwein.jpg',
+
+ pathlib.Path(
+ self.media_root.name, 'exercise-images', '1', 'protestschwein.jpg'
+ ).write_bytes(
+ pathlib.Path('wger/exercises/tests/protestschwein.jpg').read_bytes(),
)
+ pathlib.Path(
+ self.media_root.name,
+ 'exercise-images',
+ '1',
+ 'wildschwein.jpg',
+ ).write_bytes(pathlib.Path('wger/exercises/tests/wildschwein.jpg').read_bytes())
+ pathlib.Path(
+ self.media_root.name,
+ 'exercise-images',
+ '2',
+ 'wildschwein.jpg',
+ ).write_bytes(pathlib.Path('wger/exercises/tests/wildschwein.jpg').read_bytes())
class WgerTestCase(BaseTestCase, TestCase):
diff --git a/wger/exercises/fixtures/test-exercise-images.json b/wger/exercises/fixtures/test-exercise-images.json
index fb674a79b..2f016a577 100644
--- a/wger/exercises/fixtures/test-exercise-images.json
+++ b/wger/exercises/fixtures/test-exercise-images.json
@@ -7,6 +7,8 @@
"exercise": 1,
"is_main": true,
"license": "1",
+ "width": 500,
+ "height": 332,
"created": "2023-08-10T00:00:00+01:00",
"last_update": "2023-08-10T00:00:00+01:00"
}
@@ -19,6 +21,8 @@
"exercise": 1,
"is_main": false,
"license": "2",
+ "width": 481,
+ "height": 320,
"created": "2023-08-10T00:00:00+01:00",
"last_update": "2023-08-10T00:00:00+01:00"
}
@@ -31,6 +35,8 @@
"exercise": 2,
"is_main": false,
"license": "2",
+ "width": 481,
+ "height": 320,
"created": "2023-08-10T00:00:00+01:00",
"last_update": "2023-08-10T00:00:00+01:00"
}
diff --git a/wger/exercises/migrations/0033_add_exercise_image_dimensions.py b/wger/exercises/migrations/0033_add_exercise_image_dimensions.py
new file mode 100644
index 000000000..d827bf826
--- /dev/null
+++ b/wger/exercises/migrations/0033_add_exercise_image_dimensions.py
@@ -0,0 +1,60 @@
+# Generated by Django 5.2.6 on 2025-10-02 06:32
+
+import django.db.models.deletion
+import wger.exercises.models.image
+import wger.utils.images
+from django.db import migrations, models
+
+
+def update_image_dimensions(apps, schema_editor):
+ ExerciseImage = apps.get_model('exercises', 'ExerciseImage')
+ for image in ExerciseImage.objects.all():
+ if not image.image:
+ print(f'ExerciseImage {image.pk} has no corresponding file!')
+ continue
+
+ try:
+ image.width = image.image.width
+ image.height = image.image.height
+ image.save(update_fields=['width', 'height'])
+ except FileNotFoundError as e:
+ print('Error while accessing image file!', e)
+ pass
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ('exercises', '0032_rename_exercise'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='exerciseimage',
+ name='height',
+ field=models.PositiveIntegerField(
+ blank=True, editable=False, null=True, verbose_name='Height'
+ ),
+ ),
+ migrations.AddField(
+ model_name='exerciseimage',
+ name='width',
+ field=models.PositiveIntegerField(
+ blank=True, editable=False, null=True, verbose_name='Width'
+ ),
+ ),
+ migrations.AddField(
+ model_name='historicalexerciseimage',
+ name='height',
+ field=models.PositiveIntegerField(
+ blank=True, editable=False, null=True, verbose_name='Height'
+ ),
+ ),
+ migrations.AddField(
+ model_name='historicalexerciseimage',
+ name='width',
+ field=models.PositiveIntegerField(
+ blank=True, editable=False, null=True, verbose_name='Width'
+ ),
+ ),
+ migrations.RunPython(update_image_dimensions, migrations.RunPython.noop),
+ ]
diff --git a/wger/exercises/migrations/0033_rename_exercises_e_name_ac11f4_gin_exercises_t_name_6264c9_gin_and_more.py b/wger/exercises/migrations/0033_rename_exercises_e_name_ac11f4_gin_exercises_t_name_6264c9_gin_and_more.py
deleted file mode 100644
index 3dba45b2c..000000000
--- a/wger/exercises/migrations/0033_rename_exercises_e_name_ac11f4_gin_exercises_t_name_6264c9_gin_and_more.py
+++ /dev/null
@@ -1,116 +0,0 @@
-# Generated by Django 5.2.6 on 2025-10-02 06:32
-
-import django.db.models.deletion
-import wger.exercises.models.image
-import wger.utils.images
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('exercises', '0032_rename_exercise'),
- ]
-
- operations = [
- migrations.RenameIndex(
- model_name='translation',
- new_name='exercises_t_name_6264c9_gin',
- old_name='exercises_e_name_ac11f4_gin',
- ),
- migrations.AddField(
- model_name='exerciseimage',
- name='height',
- field=models.PositiveIntegerField(blank=True, editable=False, null=True, verbose_name='Height'),
- ),
- migrations.AddField(
- model_name='exerciseimage',
- name='width',
- field=models.PositiveIntegerField(blank=True, editable=False, null=True, verbose_name='Width'),
- ),
- migrations.AddField(
- model_name='historicalexerciseimage',
- name='height',
- field=models.PositiveIntegerField(blank=True, editable=False, null=True, verbose_name='Height'),
- ),
- migrations.AddField(
- model_name='historicalexerciseimage',
- name='width',
- field=models.PositiveIntegerField(blank=True, editable=False, null=True, verbose_name='Width'),
- ),
- migrations.AlterField(
- model_name='deletionlog',
- name='replaced_by',
- field=models.UUIDField(default=None, editable=False, help_text='UUID of the object replaced by the deleted one. At the moment only available for exercises', null=True, verbose_name='Replaced by'),
- ),
- migrations.AlterField(
- model_name='exercise',
- name='license_author',
- field=models.TextField(blank=True, help_text='If you are not the author, enter the name or source here.', max_length=3500, null=True, verbose_name='Author(s)'),
- ),
- migrations.AlterField(
- model_name='exerciseimage',
- name='image',
- field=models.ImageField(height_field='height', help_text='Only PNG and JPEG formats are supported', upload_to=wger.exercises.models.image.exercise_image_upload_dir, validators=[wger.utils.images.validate_image_static_no_animation], verbose_name='Image', width_field='width'),
- ),
- migrations.AlterField(
- model_name='exerciseimage',
- name='is_main',
- field=models.BooleanField(default=False, verbose_name='Main picture'),
- ),
- migrations.AlterField(
- model_name='exerciseimage',
- name='license_author',
- field=models.TextField(blank=True, help_text='If you are not the author, enter the name or source here.', max_length=3500, null=True, verbose_name='Author(s)'),
- ),
- migrations.AlterField(
- model_name='exercisevideo',
- name='license_author',
- field=models.TextField(blank=True, help_text='If you are not the author, enter the name or source here.', max_length=3500, null=True, verbose_name='Author(s)'),
- ),
- migrations.AlterField(
- model_name='historicalexercise',
- name='license_author',
- field=models.TextField(blank=True, help_text='If you are not the author, enter the name or source here.', max_length=3500, null=True, verbose_name='Author(s)'),
- ),
- migrations.AlterField(
- model_name='historicalexerciseimage',
- name='image',
- field=models.TextField(help_text='Only PNG and JPEG formats are supported', max_length=100, validators=[wger.utils.images.validate_image_static_no_animation], verbose_name='Image'),
- ),
- migrations.AlterField(
- model_name='historicalexerciseimage',
- name='is_main',
- field=models.BooleanField(default=False, verbose_name='Main picture'),
- ),
- migrations.AlterField(
- model_name='historicalexerciseimage',
- name='license_author',
- field=models.TextField(blank=True, help_text='If you are not the author, enter the name or source here.', max_length=3500, null=True, verbose_name='Author(s)'),
- ),
- migrations.AlterField(
- model_name='historicalexercisevideo',
- name='license_author',
- field=models.TextField(blank=True, help_text='If you are not the author, enter the name or source here.', max_length=3500, null=True, verbose_name='Author(s)'),
- ),
- migrations.AlterField(
- model_name='historicaltranslation',
- name='exercise',
- field=models.ForeignKey(blank=True, db_constraint=False, default=None, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='exercises.exercise'),
- ),
- migrations.AlterField(
- model_name='historicaltranslation',
- name='license_author',
- field=models.TextField(blank=True, help_text='If you are not the author, enter the name or source here.', max_length=3500, null=True, verbose_name='Author(s)'),
- ),
- migrations.AlterField(
- model_name='translation',
- name='exercise',
- field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='translations', to='exercises.exercise'),
- ),
- migrations.AlterField(
- model_name='translation',
- name='license_author',
- field=models.TextField(blank=True, help_text='If you are not the author, enter the name or source here.', max_length=3500, null=True, verbose_name='Author(s)'),
- ),
- ]
diff --git a/wger/exercises/migrations/0034_populate_image_dimensions.py b/wger/exercises/migrations/0034_populate_image_dimensions.py
deleted file mode 100644
index 4e6c46f21..000000000
--- a/wger/exercises/migrations/0034_populate_image_dimensions.py
+++ /dev/null
@@ -1,27 +0,0 @@
-# Generated by Django 5.2.6 on 2025-10-02 06:20
-
-from django.db import migrations
-
-
-def update_image_dimensions(apps, schema_editor):
- ExerciseImage = apps.get_model('exercises', 'ExerciseImage')
- for image in ExerciseImage.objects.all():
- if image.image:
- try:
- image.width = image.image.width
- image.height = image.image.height
- image.save(update_fields=['width', 'height'])
- except FileNotFoundError:
- # Handle missing image files gracefully
- pass
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('exercises', '0033_rename_exercises_e_name_ac11f4_gin_exercises_t_name_6264c9_gin_and_more'),
- ]
-
- operations = [
- migrations.RunPython(update_image_dimensions, migrations.RunPython.noop),
- ]
\ No newline at end of file
diff --git a/wger/exercises/models/image.py b/wger/exercises/models/image.py
index 5dd7a2f10..7e9fc0363 100644
--- a/wger/exercises/models/image.py
+++ b/wger/exercises/models/image.py
@@ -79,7 +79,7 @@ class ExerciseImage(AbstractLicenseModel, AbstractHistoryMixin, models.Model, Ba
image = models.ImageField(
verbose_name=_('Image'),
- help_text='Only PNG and JPEG formats are supported',
+ help_text='Only JPEG, PNG and WebP formats are supported',
upload_to=exercise_image_upload_dir,
validators=[validate_image_static_no_animation],
height_field='height',
diff --git a/wger/utils/images.py b/wger/utils/images.py
index ebb4c0322..979cbd8b3 100644
--- a/wger/utils/images.py
+++ b/wger/utils/images.py
@@ -24,6 +24,8 @@ from PIL import (
MAX_FILE_SIZE_MB = 20
+IMAGE_ALLOWED_FORMATS = ('jpeg', 'jpg', 'png', 'webp')
+
def validate_image_static_no_animation(value):
# File size check
@@ -39,10 +41,9 @@ def validate_image_static_no_animation(value):
raise ValidationError('File is not a valid image.')
# Supported types
- allowed_formats = {'jpeg', 'jpg', 'png', 'webp'}
- if img_format not in allowed_formats:
+ if img_format not in IMAGE_ALLOWED_FORMATS:
raise ValidationError(
- f'File type is not supported. Allowed formats: {", ".join(allowed_formats)}.'
+ f'File type is not supported. Allowed formats: {", ".join(IMAGE_ALLOWED_FORMATS)}.'
)
# Check for animation