modify structure
This commit is contained in:
0
server/src/app/__init__.py
Normal file
0
server/src/app/__init__.py
Normal file
BIN
server/src/app/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
server/src/app/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/src/app/__pycache__/database.cpython-311.pyc
Normal file
BIN
server/src/app/__pycache__/database.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/src/app/__pycache__/faker_seed.cpython-311.pyc
Normal file
BIN
server/src/app/__pycache__/faker_seed.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/src/app/__pycache__/main.cpython-311.pyc
Normal file
BIN
server/src/app/__pycache__/main.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/src/app/crud/__pycache__/crud_knowledges.cpython-311.pyc
Normal file
BIN
server/src/app/crud/__pycache__/crud_knowledges.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/src/app/crud/__pycache__/crud_metrics.cpython-311.pyc
Normal file
BIN
server/src/app/crud/__pycache__/crud_metrics.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/src/app/crud/__pycache__/crud_questions.cpython-311.pyc
Normal file
BIN
server/src/app/crud/__pycache__/crud_questions.cpython-311.pyc
Normal file
Binary file not shown.
42
server/src/app/crud/crud_knowledges.py
Normal file
42
server/src/app/crud/crud_knowledges.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from src.app.models.knowledge import Knowledge
|
||||
from src.app.database import engine
|
||||
|
||||
def create_knowledge(knowledge: Knowledge):
|
||||
with Session(engine) as session:
|
||||
session.add(knowledge)
|
||||
session.commit()
|
||||
return knowledge
|
||||
|
||||
def read_knowledges():
|
||||
with Session(engine) as session:
|
||||
statement = select(Knowledge)
|
||||
results = session.exec(statement)
|
||||
knowledges = results.all()
|
||||
return knowledges
|
||||
|
||||
def read_knowledge(knowledge_id: int):
|
||||
with Session(engine) as session:
|
||||
#statement = select(Knowledge).where(Knowledge.id == knowledge_id)
|
||||
#results = session.exec(statement)
|
||||
#knowledge = results.first()
|
||||
knowledge = session.get(Knowledge, knowledge_id)
|
||||
return knowledge
|
||||
|
||||
#TODO adapt logic with args
|
||||
def update_knowledge(knowledge_id: int, content: str, uri: str):
|
||||
with Session(engine) as session:
|
||||
knowledge = session.get(Knowledge, knowledge_id)
|
||||
knowledge.content = content if content else knowledge.content
|
||||
knowledge.uri = uri if uri else knowledge.uri
|
||||
|
||||
session.add(knowledge)
|
||||
session.commit()
|
||||
session.refresh(knowledge)
|
||||
|
||||
def delete_knowledge(knowledge_id: int):
|
||||
with Session(engine) as session:
|
||||
knowledge = session.get(Knowledge, knowledge_id)
|
||||
session.delete(knowledge)
|
||||
session.commit()
|
||||
29
server/src/app/crud/crud_metrics.py
Normal file
29
server/src/app/crud/crud_metrics.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from src.app.models.metric import Metric
|
||||
from src.app.models.question import Question
|
||||
from src.app.database import engine
|
||||
|
||||
def create_metric(metric: Metric):
|
||||
with Session(engine) as session:
|
||||
session.add(metric)
|
||||
session.commit()
|
||||
return metric
|
||||
|
||||
def read_metrics(question):
|
||||
with Session(engine) as session:
|
||||
statement = select(Metric).where(Metric.question_id == question.id)
|
||||
results = session.exec(statement)
|
||||
metrics = results.all()
|
||||
return metrics
|
||||
|
||||
def read_metric(metric_id: int):
|
||||
with Session(engine) as session:
|
||||
metric = session.get(Metric, metric_id)
|
||||
return metric
|
||||
|
||||
def delete_metric(metric_id: int):
|
||||
with Session(engine) as session:
|
||||
metric = session.get(Metric, metric_id)
|
||||
session.delete(metric)
|
||||
session.commit()
|
||||
42
server/src/app/crud/crud_questions.py
Normal file
42
server/src/app/crud/crud_questions.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from src.app.models.question import Question
|
||||
from src.app.models.knowledge import Knowledge
|
||||
from src.app.database import engine
|
||||
|
||||
def create_question(question: Question):
|
||||
with Session(engine) as session:
|
||||
session.add(question)
|
||||
session.commit()
|
||||
return question
|
||||
|
||||
def read_questions(knowledge):
|
||||
with Session(engine) as session:
|
||||
statement = select(Question).where(Question.knowledge_id == knowledge.id)
|
||||
results = session.exec(statement)
|
||||
questions = results.all()
|
||||
return questions
|
||||
|
||||
def read_question(question_id: int):
|
||||
with Session(engine) as session:
|
||||
question = session.get(Question, question_id)
|
||||
return question
|
||||
|
||||
# #TODO adapt logic with args
|
||||
# def update_question(question_id: int, content: str, uri: str):
|
||||
# with Session(engine) as session:
|
||||
# question = session.get(Question, question_id)
|
||||
# question.content = content if content else question.content
|
||||
# question.uri = uri if uri else question.uri
|
||||
|
||||
# session.add(question)
|
||||
# session.commit()
|
||||
# session.refresh(question)
|
||||
|
||||
#TODO : test
|
||||
def delete_question(question_id: int):
|
||||
with Session(engine) as session:
|
||||
question = session.get(Question, question_id)
|
||||
session.delete(question)
|
||||
session.commit()
|
||||
|
||||
17
server/src/app/database.py
Normal file
17
server/src/app/database.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
#import secrets
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
|
||||
load_dotenv()
|
||||
database_uri=os.environ.get("DATABASE_URI")
|
||||
|
||||
connect_args = {"check_same_thread": False}
|
||||
engine = create_engine(database_uri, echo=True, connect_args=connect_args)
|
||||
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
def get_session():
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
39
server/src/app/faker_seed.py
Normal file
39
server/src/app/faker_seed.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from src.app.models.knowledge import Knowledge
|
||||
from src.app.crud.crud_knowledges import create_knowledge, read_knowledges, read_knowledge, update_knowledge, delete_knowledge
|
||||
from src.app.models.question import Question
|
||||
from src.app.crud.crud_questions import create_question, read_questions, read_question
|
||||
from src.app.models.metric import Metric
|
||||
from src.app.crud.crud_metrics import create_metric
|
||||
|
||||
def faker():
|
||||
knowledge1 = Knowledge(content="La connaissance est une notion aux sens multiples, à la fois utilisée dans le langage courant et objet d'étude poussée de la part des sciences cognitives et des philosophes contemporains. ", uri="https://fr.wikipedia.org/wiki/Connaissance")
|
||||
knowledge2 = Knowledge(content="faculté mentale produisant une assimilation par l'esprit d'un contenu objectif préalablement traduit en signes et en idées ;", uri="https://fr.wikipedia.org/wiki/Connaissance#D%C3%A9finition")
|
||||
knowledge3 = Knowledge(content="Pour l'anthropologue, la première connaissance est celle que les hommes ont d'eux-mêmes et de leur environnement, et qui, dans les sociétés primitives, assure leur survie quotidienne.", uri="https://fr.wikipedia.org/wiki/Connaissance#En_anthropologie")
|
||||
|
||||
create_knowledge(knowledge1)
|
||||
create_knowledge(knowledge2)
|
||||
create_knowledge(knowledge3)
|
||||
|
||||
question1 = Question(knowledge = knowledge1, question = "Quel est le sens du mot connaissance ?")
|
||||
question2 = Question(knowledge = knowledge1, question = "Quel est la nature de la connaissance ?")
|
||||
question3 = Question(knowledge = knowledge1, question = "Quel est le moyen de la connaissance ?")
|
||||
question4 = Question(knowledge = knowledge2, question = "Qu'est ce qu'une faculté mentale ?")
|
||||
question5 = Question(knowledge = knowledge3, question = "Qu'est ce qu'une connaissance pour l'anthropologue ?")
|
||||
|
||||
create_question(question1)
|
||||
create_question(question2)
|
||||
create_question(question3)
|
||||
create_question(question4)
|
||||
create_question(question5)
|
||||
|
||||
metric1 = Metric(question = question1, need_index = 89)
|
||||
metric2 = Metric(question = question2, need_index = 28)
|
||||
metric3 = Metric(question = question3, need_index = 12)
|
||||
metric4 = Metric(question = question4, need_index = 59)
|
||||
metric5 = Metric(question = question5, need_index = 91)
|
||||
|
||||
create_metric(metric1)
|
||||
create_metric(metric2)
|
||||
create_metric(metric3)
|
||||
create_metric(metric4)
|
||||
create_metric(metric5)
|
||||
26
server/src/app/main.py
Normal file
26
server/src/app/main.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
|
||||
from src.app.database import create_db_and_tables
|
||||
#TODO : best practice to manage models import
|
||||
from src.app.models.question import Question
|
||||
from src.app.models.knowledge import Knowledge
|
||||
from src.app.models.metric import Metric
|
||||
|
||||
#Test
|
||||
from src.app.faker_seed import faker
|
||||
|
||||
#TODO : alternative @app.on_event("startup") ?
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
#startup
|
||||
create_db_and_tables()
|
||||
faker()
|
||||
yield
|
||||
#shutdown
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello World"}
|
||||
BIN
server/src/app/models/__pycache__/knowledge.cpython-311.pyc
Normal file
BIN
server/src/app/models/__pycache__/knowledge.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/src/app/models/__pycache__/metric.cpython-311.pyc
Normal file
BIN
server/src/app/models/__pycache__/metric.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/src/app/models/__pycache__/question.cpython-311.pyc
Normal file
BIN
server/src/app/models/__pycache__/question.cpython-311.pyc
Normal file
Binary file not shown.
9
server/src/app/models/knowledge.py
Normal file
9
server/src/app/models/knowledge.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from sqlmodel import Field, SQLModel, Relationship
|
||||
#TODO : add pydantic validation
|
||||
|
||||
class Knowledge(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
content: str = Field(index=True)
|
||||
uri: str = Field(index=True)
|
||||
|
||||
questions: list["Question"] = Relationship(back_populates="knowledge", cascade_delete=True) # type: ignore
|
||||
12
server/src/app/models/metric.py
Normal file
12
server/src/app/models/metric.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from sqlmodel import Field, SQLModel, Relationship
|
||||
from src.app.models.question import Question
|
||||
|
||||
#TODO : add pydantic validation
|
||||
|
||||
class Metric(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
question_id: int | None = Field(default=None, foreign_key="question.id", ondelete="CASCADE")
|
||||
question: Question | None = Relationship(back_populates="metrics")
|
||||
|
||||
need_index: int
|
||||
12
server/src/app/models/question.py
Normal file
12
server/src/app/models/question.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from sqlmodel import Field, SQLModel, Relationship
|
||||
from src.app.models.knowledge import Knowledge
|
||||
#TODO : add pydantic validation
|
||||
|
||||
class Question(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
question:str
|
||||
|
||||
knowledge_id: int | None = Field(default=None, foreign_key="knowledge.id", ondelete="CASCADE")
|
||||
knowledge: Knowledge | None = Relationship(back_populates="questions")
|
||||
|
||||
metrics: list["Metric"] = Relationship(back_populates="question", cascade_delete=True) # type: ignore
|
||||
Reference in New Issue
Block a user