2022-05-18 10:15:54 +02:00
from django . db import models
import random
import string
2022-06-24 13:42:16 +02:00
from django . db . models . fields import CharField , IntegerField , TextField , BooleanField , DateTimeField
2022-05-18 10:15:54 +02:00
from django . db . models . fields . json import JSONField
2022-06-11 23:39:03 +02:00
from users . models import CustomUser
2022-05-18 10:15:54 +02:00
# Create your models here.
def generate_unique_code_step ( ) :
length = 6
while True :
code = ' ' . join ( random . choices ( string . ascii_uppercase , k = length ) )
if Exercice . objects . filter ( id_code = code ) . count ( ) == 0 :
break
return code
def generate_unique_code_tag ( ) :
length = 6
while True :
code = ' ' . join ( random . choices ( string . ascii_uppercase , k = length ) )
if Tag . objects . filter ( id_code = code ) . count ( ) == 0 :
break
return code
class Tag ( models . Model ) :
color = CharField ( max_length = 50 , default = ' ' )
id_code = CharField ( max_length = 50 , unique = True ,
2022-07-29 11:52:32 +02:00
default = generate_unique_code_tag ) # Pour la migration initiale : si pas en "" renvoie erreur car table utilisée dans fonction avant d'être crée
2022-06-11 23:39:03 +02:00
user = models . ForeignKey ( ' users.CustomUser ' , null = True , on_delete = models . CASCADE )
2022-05-18 10:15:54 +02:00
name = CharField ( max_length = 25 , default = ' ' )
def exo_code ( instance , filename ) :
return f ' uploads/exercices/exo_ { instance . id_code } / { filename } '
class ExerciceManager ( models . Manager ) :
def delete ( self ) :
for obj in self . get_queryset ( ) :
obj . delete ( )
class Exercice ( models . Model ) :
name = CharField ( max_length = 50 )
id_code = CharField (
2022-07-29 11:52:32 +02:00
unique = True , max_length = 50 , default = generate_unique_code_step ) # Pour la migration initiale : si pas en "" renvoie erreur car table utilisée dans fonction avant d'être crée
2022-05-18 10:15:54 +02:00
exo_model = models . FileField ( upload_to = exo_code )
consigne = CharField ( max_length = 100 , default = ' ' , blank = True )
2022-06-11 23:39:03 +02:00
tags = models . ManyToManyField ( Tag )
2022-05-18 10:15:54 +02:00
exemple = JSONField ( default = dict )
isPdf = BooleanField ( default = True )
isCsv = BooleanField ( default = True )
isWeb = BooleanField ( default = True )
2022-06-24 13:42:16 +02:00
last_update = DateTimeField ( auto_now_add = True )
2022-06-11 23:39:03 +02:00
original = BooleanField ( default = True )
origin = models . ForeignKey ( ' self ' , null = True , on_delete = models . SET_NULL )
private = BooleanField ( default = False )
author = models . ForeignKey ( CustomUser , null = True , on_delete = models . CASCADE )
2022-05-18 10:15:54 +02:00
objects = ExerciceManager ( )
def delete ( self , using = None , keep_parents = False ) :
self . exo_model . storage . delete ( self . exo_model . name )
super ( ) . delete ( )