89 строки
2.2 KiB
Python
89 строки
2.2 KiB
Python
import os
|
|
|
|
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
|
|
|
|
|
|
def upload_to(path, /):
|
|
def wrapper(instance, filename):
|
|
return os.path.join(
|
|
path,
|
|
'%s.%s' % (
|
|
instance.pk,
|
|
filename.split('.')[-1],
|
|
),
|
|
)
|
|
return wrapper
|
|
|
|
|
|
class RoomImage(Model):
|
|
class Meta:
|
|
verbose_name_plural = 'Изображения комнат'
|
|
|
|
image = ImageField(
|
|
verbose_name='Изображение',
|
|
upload_to=upload_to('media/rooms/images'),
|
|
)
|
|
|
|
|
|
class RoomPanorama(Model):
|
|
class Meta:
|
|
verbose_name_plural = 'Панорамы комнат'
|
|
|
|
image = ImageField(
|
|
verbose_name='Изображение',
|
|
upload_to=upload_to('media/rooms/panoramas'),
|
|
)
|
|
|
|
|
|
class Room(Model):
|
|
class Meta:
|
|
verbose_name_plural = 'Комнаты'
|
|
|
|
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(
|
|
verbose_name='Доступно для аренды',
|
|
default=True,
|
|
)
|