refs #0 The root commit.

This commit is contained in:
sbps-test user
2023-09-13 20:37:14 +03:00
commit 10eb607154
66 changed files with 1643 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
from config.module import Module
from core.field import get_field
from core.pydantic_field import get_pydantic_field
from sqlalchemy import Column, Integer
from yaml import safe_load
class Config:
"""
Base class for ModelConfig, SchemaConfig and CreateSchemaConfig.
"""
def __init__(self, module_name: str) -> None:
"""
Initialize configuration.
:param module_name: The name of module.
"""
self.module = Module(module_name)
self.project = self.get_project()
def get_project(self) -> dict:
"""
Construct and return dictionary with project attributes from yaml file.
"""
with open(self.module.config_path, "r") as stream:
project = safe_load(stream)
return project
class Config:
orm_mode = True
class ModelConfig(Config):
"""
Configurates sqlalchemy model of module.
"""
def get_model_attrs(self) -> dict:
"""
Returns model configuration for the module.
"""
file_fields = set()
model_attrs = {
"__tablename__": self.module.title,
"id": Column(Integer, primary_key=True, index=True),
}
for field_name, field_attrs in self.project.get("fields").items():
model_attrs[field_name] = get_field(field_attrs)
if field_attrs["type"] == "file":
file_fields.add(field_name)
model_attrs["_file_fields"] = file_fields
return model_attrs
class CreateSchemaConfig(Config):
"""
Configurates pydantic model for creating items of module.
"""
def get_create_field_defs(self):
"""
Returns all pydantic model fields.
"""
create_field_definitions = {
field_name: get_pydantic_field(field_attrs)
for field_name, field_attrs in self.project.get("fields").items()
if field_attrs["type"] != "file" and get_pydantic_field(field_attrs)
}
return create_field_definitions
class SchemaConfig(Config):
"""
Configurates pydantic model for updating and returning items of module.
"""
def get_field_defs(self):
"""
Return all pydantic model fields and ID field.
"""
field_definitions = {}
field_definitions["id"] = (int, ...)
for field_name, field_attrs in self.project.get("fields").items():
if get_pydantic_field(field_attrs):
field_definitions[field_name] = get_pydantic_field(field_attrs)
return field_definitions