ofite/app/website/models/background_video.py

49 lines
1.5 KiB
Python
Raw Permalink 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.

import logging
import os
import traceback
from django.core.validators import FileExtensionValidator
from django.db import models
error_logger = logging.getLogger('django.error')
class BackgroundVideo(models.Model):
mp4_file = models.FileField(
upload_to='videos/',
validators=[FileExtensionValidator(allowed_extensions=["mp4"])],
blank=True,
null=True,
help_text="Загрузите видеофайл в формате MP4."
)
image = models.ImageField(
upload_to='videos/images/',
blank=True,
null=True,
verbose_name="Связанное изображение",
help_text="Загрузите изображение, связанное с видео."
)
def __str__(self):
if self.mp4_file:
return self.mp4_file.name.rsplit('/', 1)[-1].split('.')[0]
elif self.image:
return self.image.name.rsplit('/', 1)[-1].split('.')[0]
return "Unnamed Background Video"
def delete(self, *args, **kwargs):
if self.image:
image_path = self.image.path
if os.path.exists(image_path):
os.remove(self.image.path)
if self.mp4_file:
mp4_path = self.mp4_file.path
if os.path.exists(mp4_path):
os.remove(self.mp4_file.path)
super().delete(*args, **kwargs)
class Meta:
verbose_name = "Фоновое видео"
verbose_name_plural = "Фоновые видео"
ordering = ['-id']