96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
from PIL import Image
|
|
from django.db import models
|
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
|
from django.core.exceptions import ValidationError
|
|
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 Element3D(models.Model):
|
|
parent = models.ForeignKey("self", on_delete=models.PROTECT, blank=True, null=True)
|
|
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)
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
|
|
class Scene3D(models.Model):
|
|
filter_horizontal = ("elements",)
|
|
name = models.CharField(
|
|
max_length=120,
|
|
)
|
|
elements = models.ManyToManyField(Element3D)
|
|
|
|
min_distance = models.IntegerField(
|
|
validators=[MinValueValidator(1), MaxValueValidator(600)], blank=True, null=True
|
|
)
|
|
max_distance = models.IntegerField(
|
|
validators=[MinValueValidator(2), MaxValueValidator(1000)],
|
|
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)
|
|
|
|
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)
|
|
description = models.TextField("описание")
|
|
object_name = models.CharField("название объекта", max_length=255)
|
|
target_name = models.CharField(
|
|
max_length=200,
|
|
blank=True,
|
|
null=True,
|
|
)
|
|
target = models.ForeignKey(
|
|
Scene3D,
|
|
on_delete=models.PROTECT,
|
|
related_name="clickable_areas",
|
|
blank=True,
|
|
null=True,
|
|
)
|
|
source = models.ForeignKey(
|
|
Element3D,
|
|
on_delete=models.PROTECT,
|
|
)
|
|
image = models.ImageField(
|
|
"Картинка",
|
|
upload_to=group_based_upload_to,
|
|
validators=[
|
|
maximum_size_validator,
|
|
],
|
|
blank=True,
|
|
null=True,
|
|
)
|
|
|
|
def __str__(self):
|
|
return self.name
|