Skip to content

Refactor the code, create documentation to code, and update tests #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
__pycache__
.vscode
.env
dist
dist

# Ignore configuration of database
migrations
instance
8 changes: 4 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
.PHONY: test
test:
FLAKS_ENV=testing ward
FLAKS_ENV=testing pytest -vvv



.PHONY: format
format:
@black . -l 79
@black . -l 89


.PHONY: check
check:
@black . -l 79 --check
@black . -l 89 --check

6 changes: 2 additions & 4 deletions mvc_flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask.blueprints import Blueprint

from .router import Router
from .middleware.http_method_override import (
from .middlewares.http_method_override import (
HTTPMethodOverrideMiddleware,
CustomRequest,
)
Expand Down Expand Up @@ -33,9 +33,7 @@ def register_blueprint(self, app: Flask):
controller = route[0]
blueprint = Blueprint(controller, controller)

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

self.hook.register(view_func, blueprint)
Expand Down
2 changes: 1 addition & 1 deletion mvc_flask/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.5.0"
__version__ = "2.7.1"
111 changes: 102 additions & 9 deletions mvc_flask/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,49 @@


class Router:
"""
Router class for managing routes in a web application.

This class provides methods to define and manage different HTTP routes
(GET, POST, PUT, DELETE) for the application's controllers and actions.

Attributes:
ROUTES (list): A class-level list that stores all the routes registered
with their respective HTTP methods, paths, controllers,
and actions.

Methods:
_method_route(): Private method that organizes routes by HTTP method.

namespace(name: str): Static method to create a namespace for routes.

get(path: str, resource: str): Static method to define a GET route.

post(path: str, resource: str): Static method to define a POST route.

put(path: str, resource: str): Static method to define a PUT route.

delete(path: str, resource: str): Static method to define a DELETE route.

all(resource: str, only=None, base_path=""): Static method to define routes for all
standard RESTful actions for a resource.

_add_routes(name, actions, base_path): Private method to add routes for specified
actions under a given name and base path.
"""

ROUTES = []

@staticmethod
def _method_route():
"""
Organizes routes by HTTP method.

Returns:
dict: A dictionary where each key is an HTTP method and the value is a list
of routes associated with that method.
"""

routes = {}

for route in Router.ROUTES:
Expand All @@ -23,38 +62,83 @@ def _method_route():

@staticmethod
def namespace(name: str):
"""
Creates a namespace for routes.

Args:
name (str): The name of the namespace.

Returns:
Namespace: An instance of Namespace associated with the given name.
"""

return Namespace(name, Router)

@staticmethod
def get(path: str, resource: str):
"""
Defines a GET route.

Args:
path (str): URL path for the route.
resource (str): The 'controller#action' string specifying the controller and action.
"""

controller, action = resource.split("#")
Router.ROUTES.append(
{controller: Model(["GET"], path, controller, action)}
)
Router.ROUTES.append({controller: Model(["GET"], path, controller, action)})

@staticmethod
def post(path: str, resource: str):
"""
Defines a POST route.

Args:
path (str): URL path for the route.
resource (str): The 'controller#action' string specifying the controller and action.
"""

controller, action = resource.split("#")
Router.ROUTES.append(
{controller: Model(["POST"], path, controller, action)}
)
Router.ROUTES.append({controller: Model(["POST"], path, controller, action)})

@staticmethod
def put(path: str, resource: str):
"""
Defines a PUT route.

Args:
path (str): URL path for the route.
resource (str): The 'controller#action' string specifying the controller and action.
"""

controller, action = resource.split("#")
Router.ROUTES.append(
{controller: Model(["PUT", "PATCH"], path, controller, action)},
)

@staticmethod
def delete(path: str, resource: str):
"""
Defines a DELETE route.

Args:
path (str): URL path for the route.
resource (str): The 'controller#action' string specifying the controller and action.
"""

controller, action = resource.split("#")
Router.ROUTES.append(
{controller: Model(["DELETE"], path, controller, action)}
)
Router.ROUTES.append({controller: Model(["DELETE"], path, controller, action)})

@staticmethod
def all(resource: str, only=None, base_path=""):
"""
Defines routes for all standard RESTful actions for a resource.

Args:
resource (str): The name of the resource.
only (str or None): A space-separated string of actions to limit the routes to.
base_path (str): The base path to prepend to the resource path.
"""

group = [
"index",
"show",
Expand All @@ -69,6 +153,15 @@ def all(resource: str, only=None, base_path=""):

@staticmethod
def _add_routes(name, actions, base_path):
"""
Adds routes for specified actions under a given name and base path.

Args:
name (str): The name of the resource.
actions (list): A list of actions to create routes for.
base_path (str): The base path to prepend to the resource path.
"""

groups = {
"index": "get",
"new": "get",
Expand Down
Loading