Add SQLAlchemy and database boilerplate code
This commit is contained in:
parent
2da6e43797
commit
31df889dcb
15 changed files with 454 additions and 60 deletions
1
tofu_api/common/rest/__init__.py
Normal file
1
tofu_api/common/rest/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .base_blueprint import BaseBlueprint
|
||||
72
tofu_api/common/rest/base_blueprint.py
Normal file
72
tofu_api/common/rest/base_blueprint.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
from abc import ABC, abstractmethod
|
||||
from typing import Callable, Type, TYPE_CHECKING
|
||||
|
||||
from flask import Blueprint
|
||||
from flask.views import View
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from tofu_api.app import App
|
||||
|
||||
__all__ = [
|
||||
'BaseBlueprint',
|
||||
]
|
||||
|
||||
|
||||
class BaseBlueprint(Blueprint, ABC):
|
||||
"""
|
||||
Base class for Flask blueprints.
|
||||
"""
|
||||
|
||||
# Reference to the application object
|
||||
app: 'App'
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def name(self) -> str:
|
||||
"""
|
||||
The name of the blueprint. Will be prepended to each endpoint name.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def import_name(self) -> str:
|
||||
"""
|
||||
The name of the blueprint package. Should be set to `__name__`.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def url_prefix(self) -> str:
|
||||
"""
|
||||
Prefix for all URLs.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def __init__(self, app: 'App'):
|
||||
"""
|
||||
Initialize blueprint. Needs the Flask application object.
|
||||
"""
|
||||
super().__init__(
|
||||
name=self.name,
|
||||
import_name=self.import_name,
|
||||
url_prefix=self.url_prefix,
|
||||
)
|
||||
self.app = app
|
||||
self.init_blueprint()
|
||||
|
||||
@abstractmethod
|
||||
def init_blueprint(self) -> None:
|
||||
"""
|
||||
Register child blueprints and URL rules.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@staticmethod
|
||||
def create_view_func(view_cls: Type[View], *args, **kwargs) -> Callable:
|
||||
"""
|
||||
Helper function to create a view function from a `View` class using `view_cls.as_view()`.
|
||||
All arguments are passed to the constructor of the view class.
|
||||
"""
|
||||
return view_cls.as_view(view_cls.__name__, *args, **kwargs)
|
||||
Loading…
Add table
Add a link
Reference in a new issue