modify structure
This commit is contained in:
@@ -7,11 +7,17 @@ fastapi dev main.py
|
|||||||
```
|
```
|
||||||
|
|
||||||
# Best Practice
|
# Best Practice
|
||||||
|
|
||||||
|
**To alpha-2 switch to**
|
||||||
|
## SQLEModel-boilerplate
|
||||||
|
https://github.com/benavlabs/SQLModel-boilerplate/tree/main
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
## Simple Boilerplate
|
## Simple Boilerplate
|
||||||
https://github.com/anthonycepeda/fastapi-sqlmodel/tree/main
|
https://github.com/anthonycepeda/fastapi-sqlmodel/tree/main
|
||||||
|
|
||||||
## Global Boilerplate
|
## Global Boilerplate
|
||||||
https://github.com/zhanymkanov/fastapi-best-practices?tab=readme-ov-file#excessively-use-pydantic
|
https://github.com/zhanymkanov/fastapi-best-practices?tab=readme-ov-file#excessively-use-pydantic
|
||||||
|
|
||||||
## SQLEModel-boilerplate
|
|
||||||
https://github.com/benavlabs/SQLModel-boilerplate/tree/main
|
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
from contextlib import asynccontextmanager
|
|
||||||
from fastapi import FastAPI
|
|
||||||
|
|
||||||
from src.database import create_db_and_tables
|
|
||||||
#TODO : best practice to manage models import
|
|
||||||
from src.questions.models import Question
|
|
||||||
from src.knowledges.models import Knowledge
|
|
||||||
from src.metrics.models import Metric
|
|
||||||
|
|
||||||
#Test
|
|
||||||
from src.questions.controller import create_question, read_question
|
|
||||||
from src.knowledges.controller import create_knowledge, read_knowledges, delete_knowledge
|
|
||||||
|
|
||||||
#TODO : alternative @app.on_event("startup") ?
|
|
||||||
@asynccontextmanager
|
|
||||||
async def lifespan(app: FastAPI):
|
|
||||||
#startup
|
|
||||||
create_db_and_tables()
|
|
||||||
delete_knowledge()
|
|
||||||
yield
|
|
||||||
#shutdown
|
|
||||||
|
|
||||||
app = FastAPI(lifespan=lifespan)
|
|
||||||
|
|
||||||
@app.get("/")
|
|
||||||
async def root():
|
|
||||||
return {"message": "Hello World"}
|
|
||||||
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.
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.
@@ -1,12 +1,13 @@
|
|||||||
from sqlmodel import Session, select
|
from sqlmodel import Session, select
|
||||||
|
|
||||||
from src.knowledges.models import Knowledge
|
from src.app.models.knowledge import Knowledge
|
||||||
from src.database import engine
|
from src.app.database import engine
|
||||||
|
|
||||||
def create_knowledge(knowledge: Knowledge):
|
def create_knowledge(knowledge: Knowledge):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.add(knowledge)
|
session.add(knowledge)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
return knowledge
|
||||||
|
|
||||||
def read_knowledges():
|
def read_knowledges():
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
@@ -38,5 +39,4 @@ def delete_knowledge(knowledge_id: int):
|
|||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
knowledge = session.get(Knowledge, knowledge_id)
|
knowledge = session.get(Knowledge, knowledge_id)
|
||||||
session.delete(knowledge)
|
session.delete(knowledge)
|
||||||
session.commit()
|
session.commit()
|
||||||
#TODO : delete join questions
|
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
from sqlmodel import Session, select
|
from sqlmodel import Session, select
|
||||||
|
|
||||||
from src.metrics.models import Metric
|
from src.app.models.metric import Metric
|
||||||
from src.questions.models import Question
|
from src.app.models.question import Question
|
||||||
from src.database import engine
|
from src.app.database import engine
|
||||||
|
|
||||||
def create_metric(Metric: Metric):
|
def create_metric(metric: Metric):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.add(Metric)
|
session.add(metric)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
return metric
|
||||||
|
|
||||||
def read_metrics(question):
|
def read_metrics(question):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
from sqlmodel import Session, select
|
from sqlmodel import Session, select
|
||||||
|
|
||||||
from src.questions.models import Question
|
from src.app.models.question import Question
|
||||||
from src.knowledges.models import Knowledge
|
from src.app.models.knowledge import Knowledge
|
||||||
from src.database import engine
|
from src.app.database import engine
|
||||||
|
|
||||||
def create_question(Question: Question):
|
def create_question(question: Question):
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
session.add(Question)
|
session.add(question)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
return question
|
||||||
|
|
||||||
def read_questions(knowledge):
|
def read_questions(knowledge):
|
||||||
with Session(engine) as session:
|
with Session(engine) as 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"}
|
||||||
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.
@@ -1,7 +1,12 @@
|
|||||||
from sqlmodel import Field, SQLModel
|
from sqlmodel import Field, SQLModel, Relationship
|
||||||
|
from src.app.models.question import Question
|
||||||
|
|
||||||
#TODO : add pydantic validation
|
#TODO : add pydantic validation
|
||||||
|
|
||||||
class Metric(SQLModel, table=True):
|
class Metric(SQLModel, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
|
||||||
question_id: int | None = Field(default=None, foreign_key="question.id", ondelete="CASCADE")
|
question_id: int | None = Field(default=None, foreign_key="question.id", ondelete="CASCADE")
|
||||||
|
question: Question | None = Relationship(back_populates="metrics")
|
||||||
|
|
||||||
need_index: int
|
need_index: int
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from sqlmodel import Field, SQLModel, Relationship
|
from sqlmodel import Field, SQLModel, Relationship
|
||||||
from src.knowledges.models import Knowledge
|
from src.app.models.knowledge import Knowledge
|
||||||
#TODO : add pydantic validation
|
#TODO : add pydantic validation
|
||||||
|
|
||||||
class Question(SQLModel, table=True):
|
class Question(SQLModel, table=True):
|
||||||
@@ -9,4 +9,4 @@ class Question(SQLModel, table=True):
|
|||||||
knowledge_id: int | None = Field(default=None, foreign_key="knowledge.id", ondelete="CASCADE")
|
knowledge_id: int | None = Field(default=None, foreign_key="knowledge.id", ondelete="CASCADE")
|
||||||
knowledge: Knowledge | None = Relationship(back_populates="questions")
|
knowledge: Knowledge | None = Relationship(back_populates="questions")
|
||||||
|
|
||||||
metrics: list["Metrics"] = Relationship(back_populates="question", cascade_delete=True) # type: ignore
|
metrics: list["Metric"] = Relationship(back_populates="question", cascade_delete=True) # type: ignore
|
||||||
Binary file not shown.
Binary file not shown.
@@ -1,19 +0,0 @@
|
|||||||
from src.knowledges.models import Knowledge
|
|
||||||
from src.knowledges.crud import create_knowledge as create_knowledge_model, read_knowledges as read_knowledges_model, read_knowledge as read_knowledge_model, update_knowledge as update_knowledge_model, delete_knowledge as delete_knowledge_model
|
|
||||||
|
|
||||||
def create_knowledge():
|
|
||||||
connaissance = Knowledge(content="La connaissance n'est pas.", uri="https://perdu.fr")
|
|
||||||
create_knowledge_model(connaissance)
|
|
||||||
return read_knowledges()[-1]
|
|
||||||
|
|
||||||
def read_knowledges():
|
|
||||||
return read_knowledges_model()
|
|
||||||
|
|
||||||
def read_knowledge(knowledge_id):
|
|
||||||
return read_knowledge_model(knowledge_id)
|
|
||||||
|
|
||||||
def update_knowledge():
|
|
||||||
return update_knowledge_model(2, "faculté mentale produisant une assimilation par l'esprit d'un contenu objectif préalablement traduit en signes et en idées ;", "https://fr.wikipedia.org/wiki/Connaissance#D%C3%A9finition")
|
|
||||||
|
|
||||||
def delete_knowledge():
|
|
||||||
return delete_knowledge_model(7)
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,13 +0,0 @@
|
|||||||
from src.questions.models import Question
|
|
||||||
from src.questions.crud import create_question as create_question_model, read_questions as read_questions_model, read_question as read_question_model
|
|
||||||
from src.knowledges.models import Knowledge
|
|
||||||
|
|
||||||
def create_question(knowledge: Knowledge):
|
|
||||||
question = Question(knowledge = knowledge, question = "Quel est le sens du mot connaissance ?")
|
|
||||||
create_question_model(question)
|
|
||||||
|
|
||||||
def read_questions(knowledge):
|
|
||||||
return read_questions_model(knowledge)
|
|
||||||
|
|
||||||
def read_question(question_id):
|
|
||||||
return read_question_model(question_id)
|
|
||||||
Reference in New Issue
Block a user