-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create support for calling callbacks on specific actions (#56)
* feat(callback_middleware.py): add register method to CallbackMiddleware class to register before_request and after_request hooks * chore(controllers.md): update callback syntax in HomeController to use dictionary format for better readability and maintainability docs(usage.md): update form method override syntax to use `{{ method('PUT|DELETE|PATCH') }}` for better clarity and consistency chore(__version__.py): bump up version to 2.9.0 chore(pyproject.toml): bump up version to 2.9.0 test(version_test.py): update test case to check for version 2.9.0 instead of 2.8.2 * update docs * chore(callback_middleware.py): remove unnecessary blank lines for better code readability
- Loading branch information
Showing
65 changed files
with
11,678 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "2.8.2" | ||
__version__ = "2.9.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from flask import Flask, request | ||
|
||
|
||
class CallbackMiddleware: | ||
def __init__(self, app: Flask, controller_name: str, controller) -> None: | ||
""" | ||
Initializes the CallbackMiddleware instance. | ||
Parameters: | ||
app (Flask): The Flask application where the middleware is being registered. | ||
controller_name (str): The name of the controller where the hooks are defined. | ||
controller: The controller instance where the hooks are defined. | ||
""" | ||
self.app = app | ||
self.controller_name = controller_name | ||
self.controller = controller | ||
|
||
def register(self): | ||
""" | ||
Registers before_request and after_request hooks to the Flask application. | ||
The before_request hook is executed before the request is processed. | ||
The after_request hook is executed after the request is processed. | ||
The hooks are retrieved using the get_hook_method function and executed using the execute_hook function. | ||
""" | ||
|
||
def before_request_hook(): | ||
hook_method, actions = self.get_hook_method("before_request") | ||
if hook_method: | ||
self.execute_hook(hook_method, actions) | ||
|
||
def after_request_hook(response): | ||
hook_method, actions = self.get_hook_method("after_request") | ||
if hook_method: | ||
self.execute_hook(hook_method, actions, response) | ||
return response | ||
|
||
self.app.before_request_funcs.setdefault(None, []).append(before_request_hook) | ||
self.app.after_request_funcs.setdefault(None, []).append(after_request_hook) | ||
|
||
def get_hook_method(self, hook_name): | ||
""" | ||
Retrieves the hook method associated with the given hook name from the controller. | ||
Parameters: | ||
hook_name (str): The name of the hook method to retrieve. | ||
Returns: | ||
tuple: A tuple containing the callback method associated with the hook and the actions to be performed. | ||
If the hook does not exist, returns (False, False). | ||
""" | ||
if hasattr(self.controller, hook_name): | ||
hook_attribute = getattr(self.controller, hook_name) | ||
callback = hook_attribute["callback"] | ||
actions = self.actions(hook_attribute) | ||
|
||
return getattr(self.controller, callback), actions | ||
|
||
return False, False | ||
|
||
def execute_hook(self, hook_method, actions, response=None): | ||
""" | ||
Executes the specified hook method for each action in the provided list of actions | ||
if the current request endpoint matches the controller and action name. | ||
Parameters: | ||
hook_method (function): The hook method to be executed. | ||
actions (list): A list of action names. | ||
response (flask.Response, optional): The response object to be passed to the hook method. Defaults to None. | ||
""" | ||
for action in actions: | ||
endpoint = f"{self.controller_name}.{action}" | ||
if request.endpoint == endpoint: | ||
if response is None: | ||
hook_method() | ||
else: | ||
hook_method(response) | ||
|
||
def actions(self, values): | ||
""" | ||
Splits the actions string from the given values dictionary into a list of individual actions. | ||
Parameters: | ||
values (dict): A dictionary containing an "actions" key whose value is a string of action names separated by spaces. | ||
Returns: | ||
list: A list of individual action names. | ||
""" | ||
return values["actions"].split() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "mvc-flask" | ||
version = "2.8.2" | ||
version = "2.9.0" | ||
description = "turn standard Flask into mvc" | ||
authors = ["Marcus Pereira <[email protected]>"] | ||
|
||
|
Oops, something went wrong.