Начало разработки курсового проекта

develop
Сергей Ванюшкин 2024-01-05 01:10:27 +03:00
parent f9ac8c343b
commit 33fdd19ff5
16 changed files with 1480 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,25 @@
[tool.poetry]
name = "servicemanager"
version = "0.0.1"
description = "Computer equipment prevention and maintenance management system"
authors = ["Sergey Vanyushkin <pi3c@yandex.ru>"]
license = "MIT"
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
fastapi = {extras = ["all"], version = "^0.108.0"}
SQLAlchemy = "^2.0.25"
asyncpg = "^0.29.0"
pydantic-settings = "^2.1.0"
uvicorn = "^0.25.0"
[tool.poetry.group.dev.dependencies]
pytest = "^7.4.4"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
app = "servicemanager.main:run_app"

View File

@ -0,0 +1,4 @@
[pytest]
pythonpath = . servicemanager
; env_files =
; .test.env

View File

@ -0,0 +1,11 @@
from fastapi import APIRouter
api_router = APIRouter()
@api_router.get("/ping")
async def pong():
"""Тестовый роут"""
return {"ping": "pong!"}

View File

@ -0,0 +1,21 @@
import os
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
site_router = APIRouter()
print(os.path.abspath(os.curdir))
site_router.mount("/static", StaticFiles(directory="servicemanager/frontend/static"), name="static")
templates = Jinja2Templates(directory="servicemanager/frontend/templates")
@site_router.get("/testpage", response_class=HTMLResponse)
async def read_item(request: Request):
return templates.TemplateResponse(
request=request, name="test.html"
)

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>test page</title>
</head>
<body>
<h1>test_data</h1>
</body>
</html>

View File

@ -0,0 +1,44 @@
import asyncio
import sys
from fastapi import FastAPI
import uvicorn
from api.routes import api_router
from frontend.routes import site_router
async def generate_test_data():
"""
Создание БД и наполнение ее данными
"""
print('generating data')
def create_app():
"""
Создание экземпляра приложения FastAPI и врзврат его
"""
app = FastAPI()
app.include_router(api_router)
app.include_router(site_router)
return app
def run_app():
"""
Запуск локального вебсервера для тестов и проверки
"""
uvicorn.run(
app="main:create_app",
reload=True,
factory=True,
)
if __name__ == "__main__":
if "--configure" in sys.argv:
asyncio.run(generate_test_data())
if "--webserver" in sys.argv:
run_app()

View File

@ -0,0 +1,20 @@
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
DB_HOST: str
DB_PORT: int
DB_USER: str
DB_PASS: str
DB_NAME: str
@property
def DATABASE_URL_asyncpg(self):
"""
Возвращает строку подключения к БД необходимую для SQLAlchemy
"""
return f"postgresql+asyncpg://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
model_config = SettingsConfigDict(env_file=".env")
settings = Settings()

View File

@ -0,0 +1,10 @@
from starlette.testclient import TestClient
from servicemanager.main import create_app
client = TestClient(create_app())
def test_testpage():
response = client.get("/testpage")
assert response.status_code == 200

View File

@ -0,0 +1,11 @@
from starlette.testclient import TestClient
from servicemanager.main import create_app
client = TestClient(create_app())
def test_ping():
response = client.get("/ping")
assert response.status_code == 200
assert response.json() == {"ping": "pong!"}