84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
"""
|
|
Core Application Package (admin-edu-space)
|
|
Refactored to pure FastAPI / SQLAlchemy 2.0 native architecture.
|
|
Eliminated all Flask, Flask-SQLAlchemy, Flask-Babel, Flask-WTF and Flask-Login runtime dependencies.
|
|
"""
|
|
from typing import Any
|
|
from database import db, get_db, engine, SessionLocal, session
|
|
from config.config import Config
|
|
|
|
__version__ = "3.8.38"
|
|
|
|
class DummyContext:
|
|
def __enter__(self):
|
|
return self
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
pass
|
|
def push(self):
|
|
pass
|
|
def pop(self):
|
|
pass
|
|
|
|
class TestClientWrapper:
|
|
"""
|
|
Adapter bridging FastAPI TestClient with legacy client expectations (res.get_json(), res.data).
|
|
"""
|
|
def __init__(self, fastapi_client: Any):
|
|
self._client = fastapi_client
|
|
|
|
def _wrap(self, res: Any) -> Any:
|
|
try:
|
|
db.session.expire_all()
|
|
except Exception:
|
|
pass
|
|
try:
|
|
parsed = res.json()
|
|
if isinstance(parsed, dict):
|
|
if 'detail' in parsed and 'message' not in parsed:
|
|
parsed['message'] = parsed['detail']
|
|
if 'detail' in parsed and 'error' not in parsed:
|
|
parsed['error'] = parsed['detail']
|
|
setattr(res, 'get_json', lambda: parsed)
|
|
except Exception:
|
|
setattr(res, 'get_json', lambda: {})
|
|
setattr(res, 'data', getattr(res, 'content', b''))
|
|
return res
|
|
|
|
def get(self, *args: Any, **kwargs: Any) -> Any:
|
|
return self._wrap(self._client.get(*args, **kwargs))
|
|
|
|
def post(self, *args: Any, **kwargs: Any) -> Any:
|
|
return self._wrap(self._client.post(*args, **kwargs))
|
|
|
|
def put(self, *args: Any, **kwargs: Any) -> Any:
|
|
return self._wrap(self._client.put(*args, **kwargs))
|
|
|
|
def patch(self, *args: Any, **kwargs: Any) -> Any:
|
|
return self._wrap(self._client.patch(*args, **kwargs))
|
|
|
|
def delete(self, *args: Any, **kwargs: Any) -> Any:
|
|
return self._wrap(self._client.delete(*args, **kwargs))
|
|
|
|
class AppCompatWrapper:
|
|
"""
|
|
Compatibility container for test suites and scripts.
|
|
Delegates to FastAPI core without any Flask runtime dependency.
|
|
"""
|
|
def __init__(self, config_class: Any = Config):
|
|
self.config: dict[str, Any] = {
|
|
'TESTING': False,
|
|
'SQLALCHEMY_DATABASE_URI': Config.SQLALCHEMY_DATABASE_URI,
|
|
'SECRET_KEY': Config.SECRET_KEY,
|
|
'LANGUAGES': Config.LANGUAGES
|
|
}
|
|
|
|
def app_context(self):
|
|
return DummyContext()
|
|
|
|
def test_client(self):
|
|
from fastapi.testclient import TestClient
|
|
from main import app as fastapi_app
|
|
return TestClientWrapper(TestClient(fastapi_app))
|
|
|
|
def create_app(config_class: Any = Config) -> AppCompatWrapper:
|
|
return AppCompatWrapper(config_class) |