Hello, I’m lokking for a way to work with custom methods in the class based view. e.g. get_by_id()
, delete_by_id()
.
I already did subclass the HTTPMethodView
class to handle and dispatch the requests:
class BaseHandler(HTTPMethodView):
@classmethod
def as_view(
cls, suffix: str = "", *class_args: Any, **class_kwargs: Any
) -> "RouteHandler":
...
def dispatch_request(
self, request: "Request", *args: Any, **kwargs: Any
) -> "HTTPResponse":
suffix = getattr(self, "_suffix", "")
method = request.method.lower()
if suffix:
handler = getattr(self, "{0}_{1}".format(method, suffix), None)
else:
handler = getattr(self, method, None)
...
But from what I see in the Sanic code https://github.com/sanic-org/sanic/blob/a64d7fe6edb65ac60537f91d4d6c34fe7bc224b9/sanic/mixins/routes.py#L283:
# Handle HTTPMethodView differently
if hasattr(handler, "view_class"):
methods = set()
for method in HTTP_METHODS:
view_class = getattr(handler, "view_class")
_handler = getattr(view_class, method.lower(), None)
if _handler:
methods.add(method)
if hasattr(_handler, "is_stream"):
stream = True
The routes are not declared.
Question is, is there a way to overwrite the Sanic application routes declaration to handle this case ? because when I set app.add_route = functools.partial(custom_add_route, sanic_app)
I got the following:
Setting variables on Sanic instances is not allowed. You should change your Sanic instance to use instance.ctx.add_route instead
Thanks for the help.