diff --git a/argocd/deployment.yaml b/argocd/deployment.yaml index 5c3ff96..d931d6a 100644 --- a/argocd/deployment.yaml +++ b/argocd/deployment.yaml @@ -18,7 +18,7 @@ spec: fsGroupChangePolicy: "OnRootMismatch" initContainers: - name: loader - image: git.baumann.gr/adebaumann/labhelper-data-loader:0.010 + image: git.baumann.gr/adebaumann/labhelper-data-loader:0.011 securityContext: runAsUser: 0 command: [ "sh","-c","if [ ! -f /data/db.sqlite3 ] || [ ! -s /data/db.sqlite3 ]; then cp preload/preload.sqlite3 /data/db.sqlite3 && echo 'Database copied from preload'; else echo 'Existing database preserved'; fi" ] @@ -27,7 +27,7 @@ spec: mountPath: /data containers: - name: web - image: git.baumann.gr/adebaumann/labhelper:0.032 + image: git.baumann.gr/adebaumann/labhelper:0.033 imagePullPolicy: Always ports: - containerPort: 8000 diff --git a/boxes/migrations/0004_alter_thing_picture.py b/boxes/migrations/0004_alter_thing_picture.py new file mode 100644 index 0000000..092fefe --- /dev/null +++ b/boxes/migrations/0004_alter_thing_picture.py @@ -0,0 +1,19 @@ +# Generated by Django 5.2.9 on 2025-12-29 18:26 + +import boxes.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('boxes', '0003_convert_thingtype_to_mptt'), + ] + + operations = [ + migrations.AlterField( + model_name='thing', + name='picture', + field=models.ImageField(blank=True, upload_to=boxes.models.thing_picture_upload_path), + ), + ] diff --git a/boxes/models.py b/boxes/models.py index 59950bb..df95989 100644 --- a/boxes/models.py +++ b/boxes/models.py @@ -1,7 +1,19 @@ +import os from django.db import models +from django.utils.text import slugify from mptt.models import MPTTModel, TreeForeignKey +def thing_picture_upload_path(instance, filename): + """Generate a custom path for thing pictures in format: -.""" + extension = os.path.splitext(filename)[1] + safe_name = slugify(instance.name) + if instance.pk: + return f'things/{instance.pk}-{safe_name}{extension}' + else: + return f'things/temp-{safe_name}{extension}' + + class BoxType(models.Model): """A type of storage box with specific dimensions.""" @@ -72,10 +84,29 @@ class Thing(models.Model): related_name='things' ) description = models.TextField(blank=True) - picture = models.ImageField(upload_to='things/', blank=True) + picture = models.ImageField(upload_to=thing_picture_upload_path, blank=True) class Meta: ordering = ['name'] + def save(self, *args, **kwargs): + """Override save to rename picture file after instance gets a pk.""" + if self.picture and not self.pk: + picture = self.picture + super().save(*args, **kwargs) + new_path = thing_picture_upload_path(self, picture.name) + if picture.name != new_path: + try: + old_path = self.picture.path + if os.path.exists(old_path): + new_full_path = os.path.join(os.path.dirname(old_path), os.path.basename(new_path)) + os.rename(old_path, new_full_path) + self.picture.name = new_path + super().save(update_fields=['picture']) + except (AttributeError, FileNotFoundError): + pass + else: + super().save(*args, **kwargs) + def __str__(self): return self.name diff --git a/boxes/templates/boxes/thing_detail.html b/boxes/templates/boxes/thing_detail.html index 20fdfd3..733a9ed 100644 --- a/boxes/templates/boxes/thing_detail.html +++ b/boxes/templates/boxes/thing_detail.html @@ -20,10 +20,10 @@
{% if thing.picture %} {% thumbnail thing.picture "400x400" crop="center" as thumb %} - {{ thing.name }} + {{ thing.name }} {% endthumbnail %} {% else %} -
+
No image diff --git a/data-loader/preload.sqlite3 b/data-loader/preload.sqlite3 index fa0e717..2c608a0 100644 Binary files a/data-loader/preload.sqlite3 and b/data-loader/preload.sqlite3 differ