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 add_fast_api_root from database.utils.ExoSourceValidator import ExoSourceValidator from services.exoValidation import get_support_from_path, get_support_from_data from database.utils.FileField import FileField class Tag(Model): id = fields.IntField(pk=True) id_code = fields.CharField(unique=True, default="", max_length=255) label = fields.CharField(max_length=35) color = fields.CharField(max_length=100) owner = fields.ForeignKeyField('models.User') 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.User') def pdfSupport(self) -> bool: if not isinstance(self.exo_source, io.BytesIO) and not isinstance(self.exo_source, io.StringIO): if os.path.exists(add_fast_api_root(self.exo_source)): support_compatibility = get_support_from_path( add_fast_api_root(self.exo_source)) return support_compatibility['isPdf'] return False else: self.exo_source.seek(0) support_compatibility = get_support_from_data( self.exo_source.read()) return support_compatibility['isPdf'] def csvSupport(self) -> bool: if not isinstance(self.exo_source, io.BytesIO) and not isinstance(self.exo_source, io.StringIO): if os.path.exists(add_fast_api_root(self.exo_source)): support_compatibility = get_support_from_path( add_fast_api_root(self.exo_source)) return support_compatibility['isCsv'] return False else: self.exo_source.seek(0) support_compatibility = get_support_from_data( self.exo_source.read()) return support_compatibility['isCsv'] def webSupport(self) -> bool: if not isinstance(self.exo_source, io.BytesIO) and not isinstance(self.exo_source, io.StringIO): if os.path.exists(add_fast_api_root(self.exo_source)): support_compatibility = get_support_from_path( add_fast_api_root(self.exo_source)) return support_compatibility['isWeb'] return False else: self.exo_source.seek(0) support_compatibility = get_support_from_data( self.exo_source.read()) return support_compatibility['isWeb'] def examples(self) -> dict: if not isinstance(self.exo_source, io.BytesIO) and not isinstance(self.exo_source, io.StringIO): return { "type": "Csv" if self.csvSupport() else "web" if self.webSupport() else None, "data": Generateur(add_fast_api_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", "exercices", 'id']