fix image

This commit is contained in:
Lucie Galland 2021-06-08 13:31:41 +02:00
parent de50d9b187
commit e36311774b
4 changed files with 37 additions and 2 deletions

View file

@ -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]),
),
]

View file

@ -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")

View file

@ -6,7 +6,7 @@
<div id="main">
<section class="wrapper style1">
<div class="inner">
<form action="" method="post">
<form action="" method="post" enctype="multipart/form-data" >
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans "Enregistrer" %}" />

View file

@ -11,6 +11,7 @@ from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import (CreateView, DeleteView, ListView,
TemplateView, UpdateView)
import os
from calendrier.forms import ChangeDoodleName
from gestion.forms import (ChangeFormUser, ChangeMembreForm,