74 lines
2 KiB
Python
74 lines
2 KiB
Python
import os
|
|
|
|
from django.db import models
|
|
from django.core.exceptions import ValidationError
|
|
from django.urls import reverse
|
|
|
|
from users.models import School
|
|
|
|
|
|
class Category(models.Model):
|
|
"""Une catégorie de contenu."""
|
|
name = models.CharField(
|
|
max_length=255,
|
|
verbose_name="Nom de la catégorie"
|
|
)
|
|
description_short = models.TextField(
|
|
verbose_name="Description courte",
|
|
null=True,
|
|
)
|
|
description = models.TextField(
|
|
verbose_name="Description de la catégorie",
|
|
default="",
|
|
blank=True
|
|
)
|
|
image = models.ImageField(
|
|
verbose_name="Illustration de la catégorie",
|
|
null=True,
|
|
)
|
|
|
|
def get_absolute_url(self):
|
|
return reverse('content:category', kwargs={'pk': self.pk})
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
|
|
def get_upload_to(instance, filename):
|
|
extension = filename.split('.')[-1]
|
|
proper_school = ''.join(e for e in instance.school_owner.name if e.isalnum() and ord(e)<128)
|
|
proper_name = ''.join(e for e in instance.category.name if e.isalnum() and ord(e)<128)
|
|
return "static/media/"+proper_school+"/"+proper_name+'.'+extension
|
|
|
|
|
|
def validate_file_extension(value):
|
|
ext = os.path.splitext(value.name)[1] # [0] returns path+filename
|
|
valid_extensions = ['.mp4', '.avi', '.mov']
|
|
if not ext.lower() in valid_extensions:
|
|
raise ValidationError(u'Format non supporté : {}'.format(ext))
|
|
|
|
|
|
class Content(models.Model):
|
|
"""Un contenu du site (vidéo)."""
|
|
name = models.CharField(
|
|
max_length=255,
|
|
verbose_name="Nom du contenu"
|
|
)
|
|
school_owner = models.ForeignKey(
|
|
School,
|
|
on_delete=models.CASCADE,
|
|
)
|
|
category = models.ForeignKey(
|
|
Category,
|
|
on_delete=models.SET_NULL,
|
|
verbose_name="Catégorie",
|
|
null=True
|
|
)
|
|
file = models.FileField(
|
|
verbose_name="Fichier",
|
|
validators=[validate_file_extension],
|
|
upload_to=get_upload_to
|
|
)
|
|
|
|
def __str__(self):
|
|
return self.name
|