2018-03-08 22:16:03 +00:00
|
|
|
import os
|
|
|
|
|
2018-01-14 12:19:11 +00:00
|
|
|
from django.db import models
|
2018-03-08 22:16:03 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2018-01-30 18:54:01 +00:00
|
|
|
from django.urls import reverse
|
2018-01-14 12:19:11 +00:00
|
|
|
|
2018-03-08 22:16:03 +00:00
|
|
|
from users.models import School
|
|
|
|
|
2018-01-14 18:05:53 +00:00
|
|
|
|
|
|
|
class Category(models.Model):
|
2018-01-22 13:03:25 +00:00
|
|
|
"""Une catégorie de contenu."""
|
2018-01-14 18:05:53 +00:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
2018-01-24 10:44:54 +00:00
|
|
|
verbose_name="Nom de la catégorie"
|
2018-01-14 18:05:53 +00:00
|
|
|
)
|
2018-03-09 10:44:49 +00:00
|
|
|
description_short = models.TextField(
|
|
|
|
verbose_name="Description courte",
|
|
|
|
null=True,
|
|
|
|
)
|
2018-02-28 14:06:55 +00:00
|
|
|
description = models.TextField(
|
2018-03-09 10:44:49 +00:00
|
|
|
verbose_name="Description de la catégorie",
|
2018-02-28 14:06:55 +00:00
|
|
|
default=""
|
|
|
|
)
|
|
|
|
image = models.ImageField(
|
|
|
|
verbose_name="Illustration de la catégorie",
|
|
|
|
null=True,
|
|
|
|
)
|
2018-03-08 22:16:03 +00:00
|
|
|
|
2018-01-30 18:54:01 +00:00
|
|
|
def get_absolute_url(self):
|
2018-03-09 10:44:49 +00:00
|
|
|
return reverse('content:category', kwargs={'pk': self.pk})
|
2018-01-14 18:05:53 +00:00
|
|
|
|
2018-01-30 21:56:30 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-01-14 18:05:53 +00:00
|
|
|
|
2018-03-08 22:16:03 +00:00
|
|
|
def get_upload_to(instance, filename):
|
|
|
|
extension = filename.split('.')[-1]
|
2018-03-09 12:18:03 +00:00
|
|
|
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
|
2018-03-08 22:16:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_file_extension(value):
|
|
|
|
ext = os.path.splitext(value.name)[1] # [0] returns path+filename
|
2018-03-09 12:18:03 +00:00
|
|
|
valid_extensions = ['.mp4', '.avi', '.mov']
|
2018-03-08 22:16:03 +00:00
|
|
|
if not ext.lower() in valid_extensions:
|
2018-03-09 12:18:03 +00:00
|
|
|
raise ValidationError(u'Format non supporté : {}'.format(ext))
|
2018-03-08 22:16:03 +00:00
|
|
|
|
|
|
|
|
2018-01-14 18:05:53 +00:00
|
|
|
class Content(models.Model):
|
2018-01-22 13:03:25 +00:00
|
|
|
"""Un contenu du site (vidéo)."""
|
2018-01-14 18:05:53 +00:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
2018-01-24 10:33:05 +00:00
|
|
|
verbose_name="Nom du contenu"
|
2018-01-14 18:05:53 +00:00
|
|
|
)
|
2018-02-28 20:25:44 +00:00
|
|
|
school_owner = models.ForeignKey(
|
2018-03-08 22:16:03 +00:00
|
|
|
School,
|
2018-01-14 18:05:53 +00:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2018-01-24 10:33:05 +00:00
|
|
|
category = models.ForeignKey(
|
|
|
|
Category,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
verbose_name="Catégorie",
|
|
|
|
null=True
|
2018-01-14 18:05:53 +00:00
|
|
|
)
|
2018-02-28 20:25:44 +00:00
|
|
|
file = models.FileField(
|
2018-03-08 22:16:03 +00:00
|
|
|
verbose_name="Fichier",
|
|
|
|
validators=[validate_file_extension],
|
|
|
|
upload_to=get_upload_to
|
2018-02-28 20:25:44 +00:00
|
|
|
)
|
2018-01-30 21:56:30 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|