2024-03-27 14:17:05 +01:00
|
|
|
from pydantic import BaseModel, RootModel
|
|
|
|
from typing import Optional, List
|
|
|
|
import json
|
|
|
|
|
|
|
|
class Discipline(BaseModel):
|
|
|
|
"""
|
|
|
|
Modèle Discipline
|
|
|
|
"""
|
|
|
|
id: Optional[int] = 0
|
|
|
|
intitule: str
|
|
|
|
type: str
|
|
|
|
description: str
|
|
|
|
logo: str
|
|
|
|
|
|
|
|
def loadFromJsonData(self, data: str):
|
|
|
|
"""
|
|
|
|
Charge les données depuis une chaine json
|
|
|
|
|
|
|
|
:param data: Données json
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
data = json.loads(data)
|
|
|
|
for key, value in data.items():
|
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
class ListeDiscipline(RootModel):
|
|
|
|
root: List[Discipline] = []
|
|
|
|
def loadFromJson(self, path: str):
|
|
|
|
"""
|
|
|
|
Charge les données depuis un fichier json
|
|
|
|
|
|
|
|
:param path: Chemin du fichier json
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(path) as f:
|
|
|
|
data = json.load(f)
|
2024-03-27 15:20:10 +01:00
|
|
|
for discipline in data:
|
|
|
|
self.root.append(Discipline(**discipline))
|
2024-03-27 14:17:05 +01:00
|
|
|
except FileNotFoundError:
|
2024-03-27 15:20:10 +01:00
|
|
|
print(f"Le fichier {path} n'existe pas")
|
2024-03-27 14:17:05 +01:00
|
|
|
def loadFromJsonData(self, data: str):
|
|
|
|
"""
|
|
|
|
Charge les données depuis une chaine json
|
|
|
|
:param data: Données json
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
data = json.loads(data)
|
2024-03-27 15:20:10 +01:00
|
|
|
for discipline in data:
|
|
|
|
self.root.append(Discipline(**discipline))
|