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

110
back/alembic.ini Normal file
View File

@@ -0,0 +1,110 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = alembic
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
# for all available tokens
file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .
# timezone to use when rendering the date within the migration file
# as well as the filename.
# If specified, requires the python-dateutil library that can be
# installed by adding `alembic[tz]` to the pip requirements
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =
# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; This defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path.
# The path separator used here should be the separator specified by "version_path_separator" below.
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
# version path separator; As mentioned above, this is the character used to split
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
# Valid values for version_path_separator are:
#
# version_path_separator = :
# version_path_separator = ;
# version_path_separator = space
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
# set to 'true' to search source files recursively
# in each "version_locations" directory
# new in Alembic version 1.10
# recursive_version_locations = false
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = postgresql://postgres:postgres@db/postgres
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

1
back/alembic/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration.

76
back/alembic/env.py Normal file
View File

@@ -0,0 +1,76 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
from core.main import Base
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View File

@@ -0,0 +1,24 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,28 @@
"""empty message
Revision ID: ceaf19d25957
Revises:
Create Date: 2023-09-13 17:27:54.299295
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'ceaf19d25957'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###

0
back/config/__init__.py Normal file
View File

View File

@@ -0,0 +1,19 @@
from core.config_factory import CreateSchemaConfig
from core.typing import CreateSchema
from pydantic import create_model
def create_create_schema(module_name: str) -> CreateSchema:
"""
Creates pydantic module for item creating.
Returns pydantic model.
:param module_name: The name of the module.
"""
config = CreateSchemaConfig(module_name)
return create_model(
config.module.get_name("create_schema"), **config.get_create_field_defs()
)

17
back/config/model.py Normal file
View File

@@ -0,0 +1,17 @@
from core.config_factory import ModelConfig
from core.database import Base
from core.typing import Model
def create_model(module_name: str) -> Model:
"""
Creates SQLAlchemy model.
Returns created SQLAlchemy model.
:param module_name: The name of the module.
"""
config = ModelConfig(module_name)
return type(config.module.get_name("model"), (Base,), config.get_model_attrs())

View File

@@ -0,0 +1,60 @@
from importlib import import_module
from os import listdir
from os.path import isdir, join
from typing import Literal
from core.helper import snake_to_camel
from yaml import safe_load
exclude = "__pycache__"
PATH = join(*__name__.split("."))
MODULES = tuple(
filter(
lambda item: isdir(join(PATH, item)) and item not in exclude,
listdir(PATH),
)
)
class Module:
"""
Represents a module of application.
"""
def __init__(self, title: str) -> None:
self.title = title
@property
def config_path(self) -> str:
return f"config/module/{self.title}/config.yaml"
@property
def create_schema_name(self) -> str:
return snake_to_camel(self.title) + "Create"
@property
def model_name(self) -> str:
return snake_to_camel(self.title)
@property
def schema_name(self) -> str:
return self.model_name
def get_name(self, _type: Literal["model", "schema", "create_schema"]) -> str:
match _type:
case "model" | "schema":
name = snake_to_camel(self.title)
case "create_schema":
name = snake_to_camel(self.title) + "Create"
return name
def get_item(self, _type: Literal["model", "schema", "create_schema"]):
return getattr(import_module(f"config.{_type}"), f"create_{_type}")(self.title)
def get_body(self) -> dict:
with open(self.config_path, "r") as stream:
project = safe_load(stream)
return project

View File

View File

@@ -0,0 +1,9 @@
fields:
author:
type: str
title:
type: str
year:
type: int
pdf:
type: file

View File

@@ -0,0 +1,3 @@
from core.util import update_module_locals
update_module_locals(__name__, locals())

View File

@@ -0,0 +1,3 @@
from core.util import update_module_locals
update_module_locals(__name__, locals())

View File

@@ -0,0 +1,3 @@
from core.util import update_module_locals
update_module_locals(__name__, locals())

21
back/config/schema.py Normal file
View File

@@ -0,0 +1,21 @@
from core.config_factory import SchemaConfig
from core.typing import Schema
from pydantic import create_model
def create_schema(module_name: str) -> Schema:
"""
Creates pydantic module for item returning.
Returns pydantic model.
:param module_name: The name of the module.
"""
config = SchemaConfig(module_name)
return create_model(
config.module.get_name("schema"),
__config__=config.Config,
**config.get_field_defs(),
)

0
back/core/__init__.py Normal file
View File

35
back/core/command.py Normal file
View File

@@ -0,0 +1,35 @@
from os import mkdir
def create_project(name: str) -> None:
"""
Creates project folder and all required files.
from core import command; command.create_project("project")
Args:
name (str): The desired project name.
"""
def _create_file(
file_name: str, # Literal["__init__", "config", "create_schema", "model", "schema"],
content: str = None, # Optional[str] = None,
) -> None:
file = open("config/module/" + name + "/" + file_name, "w")
if content:
file.write(content)
file.close()
mkdir("config/module/" + name)
files_without_content = ("__init__.py", "config.yaml")
files_with_content = ("create_schema.py", "model.py", "schema.py")
content = "from core.util import update_module_locals\n\nupdate_module_locals(__name__, locals())\n"
for file_name in files_without_content:
_create_file(file_name)
for file_name in files_with_content:
_create_file(file_name, content)

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

68
back/core/crud.py Normal file
View File

@@ -0,0 +1,68 @@
import os
from sqlalchemy.orm import Session
def add_item(db: Session, cls, item):
db_item = cls(**item.dict())
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
def view_item(db: Session, cls, item_id: int):
return db.query(cls).filter(cls.id == item_id).first()
def edit_item(db: Session, cls, item_id: int, item):
db_item = db.query(cls).filter(cls.id == item_id).first()
for key, value in item.dict().items():
setattr(db_item, key, value)
db.commit()
db.refresh(db_item)
return db_item
def remove_item(db: Session, cls, item_id: int):
db_item = db.query(cls).filter(cls.id == item_id).first()
db.delete(db_item)
db.commit()
return db_item
def view_items(db: Session, cls, skip: int = 0, limit: int = 100):
return db.query(cls).offset(skip).limit(limit).all()
def add_file(db: Session, cls, item_id: int, field: str, file):
content = file.file.read()
if content:
file_path = os.path.join("media", file.filename)
else:
file_path = None
if file_path:
with open(file_path, "wb") as f:
f.write(content)
db_item = db.query(cls).filter(cls.id == item_id).first()
setattr(db_item, field, file_path)
db.commit()
db.refresh(db_item)
return db_item
def remove_file(db: Session, cls, item_id: int, field: str):
db_item = db.query(cls).filter(cls.id == item_id).first()
file_path = getattr(db_item, field)
if file_path:
os.remove(file_path)
setattr(db_item, field, None)
db.commit()
db.refresh(db_item)
return db_item

13
back/core/database.py Normal file
View File

@@ -0,0 +1,13 @@
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = os.getenv("POSTGRES_DATABASE_URL")
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

0
back/core/exception.py Normal file
View File

27
back/core/field.py Normal file
View File

@@ -0,0 +1,27 @@
from sqlalchemy import Column, Integer, String
def get_field(field_attrs: dict) -> Column:
"""
Construct sqlalchemy field using dictionary with field attributes.
Returns the sqlalchemy column.
:param field_attrs: The dictionary with field attributes.
"""
column_attrs = {}
for key, value in field_attrs.items():
if key == "type":
match value:
case "str":
column_attrs["type_"] = String
case "int":
column_attrs["type_"] = Integer
case "file":
column_attrs["type_"] = String
elif key == "nullable":
column_attrs["nullable"] = value
return Column(**column_attrs)

18
back/core/helper.py Normal file
View File

@@ -0,0 +1,18 @@
def snake_to_camel(snake_string: str) -> str:
"""
Transform string from snake case to string in camel case.
Returns the string in tme camel case.
:param snake_string: The string in the snake case.
"""
return "".join(part.capitalize() for part in snake_string.split("_"))
def get_file_name(name: str) -> str:
return name.split(".")[-1]
def get_parent_directory_name(name: str) -> str:
return name.split(".")[-2]

108
back/core/main.py Normal file
View File

@@ -0,0 +1,108 @@
from typing import Optional
from config.module import MODULES, Module
from core import crud
from core.database import Base, engine
from core.util import get_session
from fastapi import APIRouter, Depends, FastAPI, HTTPException, UploadFile
from fastapi.responses import FileResponse
from sqlalchemy.orm import Session
app = FastAPI()
def get_router(module: Module) -> APIRouter:
cls = module.get_item("model")
Item = module.get_item("schema")
ItemCreate = module.get_item("create_schema")
router = APIRouter(prefix="/" + module.title, tags=[module.title])
@router.post("/", response_model=Item)
async def create_item(item: ItemCreate, db: Session = Depends(get_session)):
return crud.add_item(db, cls, item)
@router.get("/", response_model=list[Item])
async def list_item(
skip: int = 0, limit: int = 100, db: Session = Depends(get_session)
):
items = crud.view_items(db, cls, skip, limit)
return items
@router.get("/{item_id}", response_model=Item)
async def read_item(item_id: int, db: Session = Depends(get_session)):
db_item = crud.view_item(db, cls, item_id)
if db_item is None:
raise HTTPException(status_code=404, detail="Item not found")
return db_item
@router.put("/{item_id}", response_model=Item)
async def update_item(
item_id: int,
item: ItemCreate,
db: Session = Depends(get_session),
):
return crud.edit_item(db, cls, item_id, item)
@router.delete("/{item_id}", response_model=Item)
async def delete_item(item_id: int, db: Session = Depends(get_session)):
db_item = crud.view_item(db, cls, item_id)
if db_item is None:
raise HTTPException(status_code=404, detail="Item not found")
for field in db_item._file_fields:
crud.remove_file(db, cls, item_id, field)
crud.remove_item(db, cls, item_id)
return db_item
@router.get("/{item_id}/{field}", response_class=FileResponse)
async def read_file(item_id: int, field: str, db: Session = Depends(get_session)):
db_item = crud.view_item(db, cls, item_id)
return getattr(db_item, field)
@router.post("/{item_id}/{field}", response_model=Optional[str])
async def create_file(
item_id: int, field: str, file: UploadFile, db: Session = Depends(get_session)
):
try:
db_item = crud.add_file(db, cls, item_id, field, file)
except Exception:
raise HTTPException(status_code=500, detail="Can not upload file")
finally:
file.file.close()
return getattr(db_item, field)
@router.delete("/{item_id}/{field}", response_model=Item)
async def delete_file(item_id: int, field: str, db: Session = Depends(get_session)):
db_item = crud.remove_file(db, cls, item_id, field)
return db_item
return router
for module in tuple(Module(module_name) for module_name in MODULES):
app.include_router(get_router(module))
module_router = APIRouter(prefix="/_module", tags=["_module"])
@module_router.get("/", response_model=list[str])
def list_module(skip: int = 0, limit: int = 100):
return MODULES
@module_router.get("/{module}", response_model=dict)
def read_module(module: str):
if module not in MODULES:
raise HTTPException(status_code=404, detail="Module not found")
return Module(module).get_body()
app.include_router(module_router)
Base.metadata.create_all(bind=engine)

View File

@@ -0,0 +1,10 @@
def get_pydantic_field(field_attrs: dict) -> tuple:
match field_attrs["type"]:
case "int" | "str":
field = (field_attrs["type"], ...)
case "file":
field = (str, None)
case _:
return ()
return field

75
back/core/test.py Normal file
View File

@@ -0,0 +1,75 @@
from re import match
from os.path import exists
from unittest import TestCase, main
from shutil import rmtree
from command import create_project
def increment_name(full_name: str) -> str:
name, index = match(
pattern="^([a-z\d]+)(?:_(\d+))?", string=full_name.lower()
).groups()
if not index:
postfix = "_1"
else:
postfix = f"_{index+1}"
return name + postfix
def get_temporary_directory_name() -> str:
dir_name = "test"
while exists("config/module/" + dir_name + "/"):
dir_name = increment_name(dir_name)
return dir_name
class TestCommandMethods(TestCase):
def test_create_project_creates_project_directory(self: TestCase):
directory = get_temporary_directory_name()
create_project(directory)
directory_exists = exists(f"config/module/{directory}/")
rmtree(f"config/module/{directory}/")
self.assertTrue(directory_exists)
def test_create_project_creates_init(self: TestCase):
directory = get_temporary_directory_name()
create_project(directory)
file_exists = exists(f"config/module/{directory}/__init__.py")
rmtree(f"config/module/{directory}/")
self.assertTrue(file_exists)
def test_create_project_creates_config(self: TestCase):
directory = get_temporary_directory_name()
create_project(directory)
file_exists = exists(f"config/module/{directory}/config.yaml")
rmtree(f"config/module/{directory}/")
self.assertTrue(file_exists)
def test_create_project_creates_create_schema(self: TestCase):
directory = get_temporary_directory_name()
create_project(directory)
file_exists = exists(f"config/module/{directory}/create_schema.py")
rmtree(f"config/module/{directory}/")
self.assertTrue(file_exists)
def test_create_project_creates_model(self: TestCase):
directory = get_temporary_directory_name()
create_project(directory)
file_exists = exists(f"config/module/{directory}/model.py")
rmtree(f"config/module/{directory}/")
self.assertTrue(file_exists)
def test_create_project_creates_schema(self: TestCase):
directory = get_temporary_directory_name()
create_project(directory)
file_exists = exists(f"config/module/{directory}/schema.py")
rmtree(f"config/module/{directory}/")
self.assertTrue(file_exists)
if __name__ == "__main__":
main()

9
back/core/typing.py Normal file
View File

@@ -0,0 +1,9 @@
from typing import TypeVar
from pydantic import BaseModel
from core.database import Base
Model = TypeVar("Model", bound=Base)
Schema = TypeVar("Schema", bound=BaseModel)
CreateSchema = TypeVar("CreateSchema", bound=BaseModel)

24
back/core/util.py Normal file
View File

@@ -0,0 +1,24 @@
from config.module import Module
from core.database import SessionLocal
from core.helper import get_file_name, get_parent_directory_name
def update_module_locals(path, _locals):
item = get_file_name(path)
module_name = get_parent_directory_name(path)
module = Module(module_name)
_locals[module.get_name(item)] = module.get_item(item)
def get_session():
session = SessionLocal()
try:
yield session
finally:
session.close()
def get_module(module: str) -> dict:
return Module(module).get_item("create_schema").construct().schema()

38
back/help.py Executable file
View File

@@ -0,0 +1,38 @@
#!/usr/local/bin/python
from importlib import import_module
from click import argument, command
@command()
@argument("path")
def pyhelp(path: str):
"""
Print the object documentation from docstring.
:param path: The full path to the module, function or class.
"""
module_path = path.rsplit(".", 1)[0]
object_name = path.split(".")[-1]
try:
module = import_module(module_path)
except ModuleNotFoundError:
module = None
if module:
if module_path == object_name:
_object = module
else:
_object = getattr(module, object_name, None)
else:
_object = getattr(__builtins__, object_name, None)
if _object:
help(_object)
if __name__ == "__main__":
pyhelp()

35
back/manage.py Normal file
View File

@@ -0,0 +1,35 @@
#!/usr/local/bin/python
from importlib import import_module
from click import argument, command
@command()
@argument("path")
def pyhelp(path: str):
"""
Вывести документацию к объекту.
"""
module_path = path.rsplit(".", 1)[0]
object_name = path.split(".")[-1]
try:
module = import_module(module_path)
except ModuleNotFoundError:
module = None
if module:
if module_path == object_name:
_object = module
else:
_object = getattr(module, object_name, None)
else:
_object = getattr(__builtins__, object_name, None)
if _object:
help(_object)
if __name__ == "__main__":
pyhelp()