2024-09-22 18:05:46 +03:00
|
|
|
import os
|
2024-09-22 18:33:28 +03:00
|
|
|
import uuid
|
2024-09-22 18:05:46 +03:00
|
|
|
|
2024-09-22 17:10:43 +03:00
|
|
|
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
|
2024-09-22 18:33:28 +03:00
|
|
|
from django.utils.deconstruct import deconstructible
|
2024-09-22 17:10:43 +03:00
|
|
|
|
|
|
|
|
2024-09-22 18:33:28 +03:00
|
|
|
@deconstructible
|
|
|
|
class UploadTo:
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def __call__(self, instance, filename):
|
2024-09-22 18:05:46 +03:00
|
|
|
return os.path.join(
|
2024-09-22 18:33:28 +03:00
|
|
|
self.path,
|
2024-09-22 18:05:46 +03:00
|
|
|
'%s.%s' % (
|
2024-09-22 18:33:28 +03:00
|
|
|
uuid.uuid4(),
|
2024-09-22 18:05:46 +03:00
|
|
|
filename.split('.')[-1],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-09-22 17:10:43 +03:00
|
|
|
class RoomImage(Model):
|
|
|
|
class Meta:
|
2024-09-22 18:08:49 +03:00
|
|
|
verbose_name = 'изображение комнаты'
|
|
|
|
verbose_name_plural = 'изображения комнат'
|
2024-09-22 17:10:43 +03:00
|
|
|
|
2024-09-22 18:10:24 +03:00
|
|
|
name = CharField(
|
|
|
|
verbose_name='Название',
|
|
|
|
max_length=32,
|
|
|
|
unique=True,
|
|
|
|
)
|
2024-09-22 17:10:43 +03:00
|
|
|
image = ImageField(
|
|
|
|
verbose_name='Изображение',
|
2024-09-22 18:33:28 +03:00
|
|
|
upload_to=UploadTo('media/rooms/images'),
|
2024-09-22 17:10:43 +03:00
|
|
|
)
|
|
|
|
|
2024-09-22 18:10:24 +03:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2024-09-22 17:10:43 +03:00
|
|
|
|
|
|
|
class RoomPanorama(Model):
|
|
|
|
class Meta:
|
2024-09-22 18:08:49 +03:00
|
|
|
verbose_name = 'панорама комнаты'
|
|
|
|
verbose_name_plural = 'панорамы комнат'
|
2024-09-22 17:10:43 +03:00
|
|
|
|
2024-09-22 18:10:24 +03:00
|
|
|
name = CharField(
|
|
|
|
verbose_name='Название',
|
|
|
|
max_length=32,
|
|
|
|
unique=True,
|
|
|
|
)
|
2024-09-22 17:10:43 +03:00
|
|
|
image = ImageField(
|
|
|
|
verbose_name='Изображение',
|
2024-09-22 18:33:28 +03:00
|
|
|
upload_to=UploadTo('media/rooms/panoramas'),
|
2024-09-22 17:10:43 +03:00
|
|
|
)
|
|
|
|
|
2024-09-22 18:10:24 +03:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2024-09-22 17:10:43 +03:00
|
|
|
|
|
|
|
class Room(Model):
|
|
|
|
class Meta:
|
2024-09-22 18:08:49 +03:00
|
|
|
verbose_name = 'комната'
|
|
|
|
verbose_name_plural = 'комнаты'
|
2024-09-22 17:10:43 +03:00
|
|
|
|
|
|
|
name = CharField(
|
|
|
|
verbose_name='Название',
|
|
|
|
max_length=32,
|
|
|
|
unique=True,
|
|
|
|
)
|
|
|
|
description = TextField(
|
|
|
|
verbose_name='Описание',
|
|
|
|
max_length=4096,
|
|
|
|
)
|
|
|
|
preview_image = OneToOneField(
|
|
|
|
verbose_name='Изображение в каталоге',
|
|
|
|
to=RoomImage,
|
|
|
|
on_delete=CASCADE,
|
|
|
|
related_name='room_preview',
|
|
|
|
)
|
|
|
|
room_images = ManyToManyField(
|
|
|
|
verbose_name='Изображения',
|
|
|
|
to=RoomImage,
|
|
|
|
related_name='room_images',
|
|
|
|
)
|
|
|
|
panorama_images = ManyToManyField(
|
|
|
|
verbose_name='Панорамы',
|
|
|
|
to=RoomPanorama,
|
|
|
|
)
|
|
|
|
area = DecimalField(
|
|
|
|
verbose_name='Площадь',
|
|
|
|
max_digits=4,
|
|
|
|
decimal_places=1,
|
|
|
|
)
|
|
|
|
price = DecimalField(
|
|
|
|
verbose_name='Стоимость',
|
|
|
|
max_digits=10,
|
|
|
|
decimal_places=2,
|
|
|
|
)
|
|
|
|
ordinal = IntegerField(
|
|
|
|
verbose_name='Порядковый номер',
|
|
|
|
)
|
|
|
|
is_visible = BooleanField(
|
|
|
|
verbose_name='Отображать в каталоге',
|
|
|
|
default=True,
|
|
|
|
)
|
|
|
|
is_available = BooleanField(
|
2024-09-22 18:10:24 +03:00
|
|
|
verbose_name='Доступна для аренды',
|
2024-09-22 17:10:43 +03:00
|
|
|
default=True,
|
|
|
|
)
|
2024-09-22 18:33:28 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|