Skip to content

Commit

Permalink
refactor(__init__.py): remove unused imports and commented out code
Browse files Browse the repository at this point in the history
feat(blueprint_middleware.py): add BlueprintMiddleware class to handle registering blueprints and routes dynamically
  • Loading branch information
marcuxyz committed Nov 17, 2023
1 parent e15d270 commit b405705
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
33 changes: 4 additions & 29 deletions mvc_flask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from importlib import import_module

from flask import Flask
from flask.blueprints import Blueprint

from .middlewares.http.router_middleware import RouterMiddleware as Router

from .middlewares.http.method_override_middleware import MethodOverrideMiddleware
from .middlewares.http.custom_request_middleware import CustomRequestMiddleware
from .middlewares.hook_middleware import HookMiddleware
from .middlewares.blueprint_middleware import BlueprintMiddleware


from .helpers.html.input_method_helper import InputMethodHelper

Expand All @@ -25,32 +23,9 @@ def init_app(self, app: Flask = None, path="app"):
app.wsgi_app = MethodOverrideMiddleware(app.wsgi_app)

# register blueprint
self.register_blueprint(app)
# register_blueprint(app)
BlueprintMiddleware(app, path).register()

@app.context_processor
def inject_stage_and_region():
return dict(method=InputMethodHelper().input_hidden_method)

def register_blueprint(self, app: Flask):
# load routes defined from users
import_module(f"{self.path}.routes")

for route in Router._method_route().items():
controller = route[0]
blueprint = Blueprint(controller, controller)

obj = import_module(f"{self.path}.controllers.{controller}_controller")
view_func = getattr(obj, f"{controller.title()}Controller")
instance_of_controller = view_func()

HookMiddleware().register(instance_of_controller, blueprint)

for resource in route[1]:
blueprint.add_url_rule(
rule=resource.path,
endpoint=resource.action,
view_func=getattr(instance_of_controller, resource.action),
methods=resource.method,
)

app.register_blueprint(blueprint)
38 changes: 38 additions & 0 deletions mvc_flask/middlewares/blueprint_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from flask import Flask
from importlib import import_module

from flask.blueprints import Blueprint

from .hook_middleware import HookMiddleware

from .http.router_middleware import RouterMiddleware as Router


class BlueprintMiddleware:
def __init__(self, app: Flask, path: str) -> None:
self.app = app
self.path = path

# load routes defined from users
import_module(f"{self.path}.routes")

def register(self):
for route in Router._method_route().items():
controller = route[0]
blueprint = Blueprint(controller, controller)

obj = import_module(f"{self.path}.controllers.{controller}_controller")
view_func = getattr(obj, f"{controller.title()}Controller")
instance_of_controller = view_func()

HookMiddleware().register(instance_of_controller, blueprint)

for resource in route[1]:
blueprint.add_url_rule(
rule=resource.path,
endpoint=resource.action,
view_func=getattr(instance_of_controller, resource.action),
methods=resource.method,
)

self.app.register_blueprint(blueprint)

0 comments on commit b405705

Please sign in to comment.