From 5be1fe2b55e9961e7a1c74015bcaa396b2a6819c Mon Sep 17 00:00:00 2001 From: "Gleb O. Ivaniczkij" Date: Sun, 22 Sep 2024 18:33:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7-=D0=B7=D0=B0=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B1=D0=BB=D0=B5=D0=BC=20=D1=81=20=D0=BC=D0=B8=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B5=D0=B9=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BF=D0=BE=D0=B4=D1=85=D0=BE=D0=B4=20=D0=B3?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B8=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B6=D0=B0?= =?UTF-8?q?=D0=B5=D0=BC=D1=8B=D1=85=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/app/models.py b/app/models.py index 76b3180..b38093d 100644 --- a/app/models.py +++ b/app/models.py @@ -1,20 +1,25 @@ import os +import uuid from django.db.models import Model from django.db.models import OneToOneField, ManyToManyField, CASCADE from django.db.models import CharField, TextField, IntegerField, DecimalField, BooleanField, ImageField +from django.utils.deconstruct import deconstructible -def upload_to(path, /): - def wrapper(instance, filename): +@deconstructible +class UploadTo: + def __init__(self, path): + self.path = path + + def __call__(self, instance, filename): return os.path.join( - path, + self.path, '%s.%s' % ( - instance.pk, + uuid.uuid4(), filename.split('.')[-1], ), ) - return wrapper class RoomImage(Model): @@ -29,7 +34,7 @@ class RoomImage(Model): ) image = ImageField( verbose_name='Изображение', - upload_to=upload_to('media/rooms/images'), + upload_to=UploadTo('media/rooms/images'), ) def __str__(self): @@ -48,7 +53,7 @@ class RoomPanorama(Model): ) image = ImageField( verbose_name='Изображение', - upload_to=upload_to('media/rooms/panoramas'), + upload_to=UploadTo('media/rooms/panoramas'), ) def __str__(self): @@ -105,3 +110,6 @@ class Room(Model): verbose_name='Доступна для аренды', default=True, ) + + def __str__(self): + return self.name