107 lines
4.0 KiB
Python
107 lines
4.0 KiB
Python
import asyncio
|
|
from io import BytesIO
|
|
import io
|
|
import os
|
|
import random
|
|
import string
|
|
|
|
from tortoise.models import Model
|
|
from tortoise import fields
|
|
from tortoise.contrib.pydantic import pydantic_model_creator
|
|
import async_to_sync as sync
|
|
from tortoise.manager import Manager
|
|
from generateur.generateur_main import Generateur
|
|
|
|
from services.io import get_abs_path_from_relative_to_root
|
|
|
|
from .validators import ExoSourceValidator, get_support_compatibility_for_exo_source_from_path, get_support_compatibility_for_exo_source_from_data
|
|
|
|
from .customField import FileField
|
|
|
|
|
|
class Tag(Model):
|
|
id = fields.IntField(pk=True)
|
|
id_code = fields.CharField(unique=True, default="", max_length=15)
|
|
|
|
name = fields.CharField(max_length=35)
|
|
color = fields.CharField(max_length=100)
|
|
|
|
owner = fields.ForeignKeyField('models.UserModel')
|
|
|
|
|
|
class Exercice(Model):
|
|
id = fields.IntField(pk=True)
|
|
id_code = fields.CharField(default="", max_length=10, unique=True)
|
|
|
|
name = fields.CharField(max_length=50)
|
|
|
|
consigne = fields.CharField(max_length=200, null=True, default="")
|
|
|
|
exo_source = FileField(upload_root="/uploads",
|
|
validators=[ExoSourceValidator()])
|
|
|
|
updated_at = fields.DatetimeField(auto_now=True)
|
|
|
|
private = fields.BooleanField(default=False)
|
|
|
|
tags = fields.ManyToManyField('models.Tag', null=True)
|
|
|
|
origin = fields.ForeignKeyField('models.Exercice', null = True)
|
|
isOriginal = fields.BooleanField(default = True)
|
|
|
|
author = fields.ForeignKeyField('models.UserModel')
|
|
|
|
def pdfSupport(self) -> bool:
|
|
if not isinstance(self.exo_source, io.BytesIO):
|
|
if os.path.exists(get_abs_path_from_relative_to_root(self.exo_source)):
|
|
support_compatibility = get_support_compatibility_for_exo_source_from_path(
|
|
get_abs_path_from_relative_to_root(self.exo_source))
|
|
return support_compatibility['isPdf']
|
|
return False
|
|
else:
|
|
self.exo_source.seek(0)
|
|
support_compatibility = get_support_compatibility_for_exo_source_from_data(
|
|
self.exo_source.read())
|
|
return support_compatibility['isPdf']
|
|
|
|
def csvSupport(self) -> bool:
|
|
if not isinstance(self.exo_source, io.BytesIO):
|
|
if os.path.exists(get_abs_path_from_relative_to_root(self.exo_source)):
|
|
support_compatibility = get_support_compatibility_for_exo_source_from_path(
|
|
get_abs_path_from_relative_to_root(self.exo_source))
|
|
return support_compatibility['isCsv']
|
|
return False
|
|
else:
|
|
self.exo_source.seek(0)
|
|
support_compatibility = get_support_compatibility_for_exo_source_from_data(
|
|
self.exo_source.read())
|
|
return support_compatibility['isCsv']
|
|
|
|
def webSupport(self) -> bool:
|
|
|
|
if not isinstance(self.exo_source, io.BytesIO):
|
|
if os.path.exists(get_abs_path_from_relative_to_root(self.exo_source)):
|
|
support_compatibility = get_support_compatibility_for_exo_source_from_path(
|
|
get_abs_path_from_relative_to_root(self.exo_source))
|
|
return support_compatibility['isWeb']
|
|
return False
|
|
else:
|
|
self.exo_source.seek(0)
|
|
support_compatibility = get_support_compatibility_for_exo_source_from_data(
|
|
self.exo_source.read())
|
|
return support_compatibility['isWeb']
|
|
|
|
def examples(self) -> dict:
|
|
if not isinstance(self.exo_source, io.BytesIO):
|
|
return {
|
|
"type": "Csv" if self.csvSupport() else "web" if self.webSupport() else None,
|
|
"data": Generateur(get_abs_path_from_relative_to_root(self.exo_source), 3, "csv" if self.csvSupport() else "web" if self.pdfSupport() else None, True) if self.csvSupport() == True or self.webSupport() == True else None
|
|
}
|
|
return {}
|
|
|
|
class PydanticMeta:
|
|
computed = ["pdfSupport", "csvSupport", "webSupport", 'examples']
|
|
exclude = ["exo_source", 'id', "exercices"]
|
|
|
|
|