demo-int-table/back/object/models.py

108 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PIL import Image
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.exceptions import ValidationError
from colorfield.fields import ColorField
import logging
logger = logging.getLogger("root")
def group_based_upload_to(instance, filename):
logger.info(instance)
return "files/image/{}/{}/{}".format(
type(instance).__name__.lower(), instance.id, filename
)
class Environment(models.Model):
env_displacementmap = models.FileField(
upload_to=group_based_upload_to, blank=True, null=True
)
env_normalmap = models.FileField(
upload_to=group_based_upload_to, blank=True, null=True
)
clear_color = ColorField(blank=True, null=True)
hdr_gainmap = models.FileField(
upload_to=group_based_upload_to, blank=True, null=True
)
hdr_json = models.FileField(upload_to=group_based_upload_to, blank=True, null=True)
hdr_webp = models.FileField(upload_to=group_based_upload_to, blank=True, null=True)
class Element3D(models.Model):
model_file = models.FileField(upload_to=group_based_upload_to)
name = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
is_enabled = models.BooleanField(default=True)
can_not_disable = models.BooleanField(default=False)
x_pos = models.IntegerField(default=0)
y_pos = models.IntegerField(default=0)
z_pos = models.IntegerField(default=0)
def __str__(self):
return self.name
class Scene3D(models.Model):
name = models.CharField(max_length=120)
description = models.TextField(blank=True, null=True)
elements = models.ManyToManyField(Element3D)
env = models.ForeignKey(Environment, models.RESTRICT, blank=True, null=True)
min_distance = models.IntegerField(
default=10,
validators=[MinValueValidator(1), MaxValueValidator(600)],
)
max_distance = models.IntegerField(
default=20,
validators=[MinValueValidator(2), MaxValueValidator(1000)],
)
def __str__(self):
return self.name
def maximum_size_validator(image):
max_width = 512
max_height = 512
img = Image.open(image)
fw, fh = img.size
if fw > max_width or fh > max_height:
raise ValidationError("Height or Width is larger than what is allowed")
class ClickableArea(models.Model):
name = models.CharField(
"Название",
max_length=255,
help_text="Название кликабельной области",
)
description = models.TextField(
"Описание",
help_text="Описание кликабельной области",
)
target = models.ForeignKey(
Scene3D,
on_delete=models.PROTECT,
related_name="clickable_areas",
blank=True,
null=True,
help_text="На какую сцену ведет клик",
)
source = models.ForeignKey(
Element3D,
on_delete=models.PROTECT,
help_text="В каком элементе искать object_name",
)
object_name = models.CharField(
"Название объекта",
max_length=255,
help_text="Имя mesh или group в элементе 3D",
)
def __str__(self):
return self.name