diff --git a/gestion/migrations/0006_auto_20210608_1029.py b/gestion/migrations/0006_auto_20210608_1029.py new file mode 100644 index 0000000..0829dd0 --- /dev/null +++ b/gestion/migrations/0006_auto_20210608_1029.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.17 on 2021-06-08 10:29 + +from django.db import migrations, models +import gestion.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('gestion', '0005_auto_20210427_1834'), + ] + + operations = [ + migrations.AlterField( + model_name='photo', + name='image', + field=models.ImageField(default=None, upload_to='trombonoscope/deco', validators=[gestion.models.Photo.validate_image]), + ), + ] diff --git a/gestion/models.py b/gestion/models.py index f9a8611..81da941 100644 --- a/gestion/models.py +++ b/gestion/models.py @@ -5,6 +5,8 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models from django.utils.translation import gettext_lazy as _ +import os +from django.conf import settings class Photo(models.Model): @@ -38,12 +40,25 @@ class Photo(models.Model): ) color = RGBColorField(_("Couleur du nom de l'auteur"), default="#ffffff") image = models.ImageField( - upload_to="deco", default=None, validators=[validate_image] + upload_to="trombonoscope/deco", default=None, validators=[validate_image] ) def __str__(self): return self.name + def delete(self): + os.remove(self.image.path) + return super(Photo, self).delete() + + def save(self, *args, **kwargs): + try: + this = Photo.objects.get(id=self.id) + if this.image.path != self.image.path: + os.remove(this.image.path) + except Photo.DoesNotExist: + pass + super(Photo, self).save(*args, **kwargs) + class Meta: verbose_name = _("Photo") verbose_name_plural = _("Photos") diff --git a/gestion/templates/gestion/update_photo.html b/gestion/templates/gestion/update_photo.html index f6967ec..550af37 100644 --- a/gestion/templates/gestion/update_photo.html +++ b/gestion/templates/gestion/update_photo.html @@ -6,7 +6,7 @@