|
1 | | -from http import HTTPStatus |
| 1 | +from http import HTTPMethod, HTTPStatus |
2 | 2 | from typing import ( |
3 | 3 | Any, |
4 | 4 | ClassVar, |
@@ -83,10 +83,14 @@ class Controller(View, Generic[_SerializerT_co]): # noqa: WPS214 |
83 | 83 | controller_validator_cls: ClassVar[type[ControllerValidator]] = ( |
84 | 84 | ControllerValidator |
85 | 85 | ) |
| 86 | + # str and not HTTPMethod, because of `meta` method: |
86 | 87 | api_endpoints: ClassVar[dict[str, Endpoint]] |
87 | 88 | validate_responses: ClassVar[bool | Empty] = EmptyObj |
88 | 89 | responses: ClassVar[list[ResponseDescription]] = [] |
89 | 90 | responses_from_components: ClassVar[bool] = True |
| 91 | + http_methods: ClassVar[frozenset[str]] = frozenset( |
| 92 | + {method.name.lower() for method in HTTPMethod} - {'options'} | {'meta'}, |
| 93 | + ) |
90 | 94 |
|
91 | 95 | # Internal API: |
92 | 96 | _component_parsers: ClassVar[list[_ComponentParserSpec]] |
@@ -116,9 +120,12 @@ def __init_subclass__(cls) -> None: |
116 | 120 | ] |
117 | 121 | cls.serializer_context = cls.serializer_context_cls(cls) |
118 | 122 | cls.api_endpoints = { |
119 | | - meth: cls.endpoint_cls(func, controller_cls=cls) |
| 123 | + # Rename `meta` back to `options`: |
| 124 | + 'options' if meth == 'meta' else meth: cls.endpoint_cls( |
| 125 | + getattr(cls, meth), |
| 126 | + controller_cls=cls, |
| 127 | + ) |
120 | 128 | for meth in cls.existing_http_methods() |
121 | | - if (func := getattr(cls, meth)) is not getattr(View, meth, None) |
122 | 129 | } |
123 | 130 | cls._is_async = cls.controller_validator_cls()(cls) |
124 | 131 |
|
@@ -218,6 +225,90 @@ def http_method_not_allowed( |
218 | 225 | 'use `handle_method_not_allowed` instead', |
219 | 226 | ) |
220 | 227 |
|
| 228 | + @override |
| 229 | + @deprecated( |
| 230 | + # It is not actually deprecated, but type checkers have no other |
| 231 | + # ways to raise custom errors. |
| 232 | + 'Please do not use `options` method with `django-modern-rest`, ' |
| 233 | + 'define your own `meta` method instead', |
| 234 | + ) |
| 235 | + def options( |
| 236 | + self, |
| 237 | + request: HttpRequest, |
| 238 | + *args: Any, |
| 239 | + **kwargs: Any, |
| 240 | + ) -> HttpResponse: |
| 241 | + """ |
| 242 | + Do not use, define your own `meta` method instead. |
| 243 | +
|
| 244 | + Django's `View.options` has incompatible signature with |
| 245 | + ``django-modern-rest``. It would be a typing error |
| 246 | + to define something like: |
| 247 | +
|
| 248 | + .. warning:: |
| 249 | +
|
| 250 | + Don't do this! |
| 251 | +
|
| 252 | + .. code:: python |
| 253 | +
|
| 254 | + >>> from http import HTTPStatus |
| 255 | + >>> from django_modern_rest import Controller, validate |
| 256 | + >>> from django_modern_rest.plugins.pydantic import ( |
| 257 | + ... PydanticSerializer, |
| 258 | + ... ) |
| 259 | + >>> class MyController(Controller[PydanticSerializer]): |
| 260 | + ... @validate( |
| 261 | + ... ResponseDescription( |
| 262 | + ... None, |
| 263 | + ... status_code=HTTPStatus.NO_CONTENT, |
| 264 | + ... ), |
| 265 | + ... ) |
| 266 | + ... def options(self) -> HttpResponse: # <- typing problem |
| 267 | + ... ... |
| 268 | +
|
| 269 | + That's why instead of ``options`` you should define |
| 270 | + our own ``meta`` method: |
| 271 | +
|
| 272 | + .. code:: python |
| 273 | +
|
| 274 | + >>> class MyController(Controller[PydanticSerializer]): |
| 275 | + ... @validate( |
| 276 | + ... ResponseDescription( |
| 277 | + ... None, |
| 278 | + ... status_code=HTTPStatus.NO_CONTENT, |
| 279 | + ... ), |
| 280 | + ... ) |
| 281 | + ... def meta(self) -> HttpResponse: |
| 282 | + ... allow = ','.join( |
| 283 | + ... method.upper() for method in self.http_methods |
| 284 | + ... ) |
| 285 | + ... return self.to_response( |
| 286 | + ... None, |
| 287 | + ... status_code=HTTPStatus.NO_CONTENT, |
| 288 | + ... headers={'Allow': allow}, |
| 289 | + ... ) |
| 290 | +
|
| 291 | + .. note:: |
| 292 | +
|
| 293 | + By default ``meta`` method is not provided for you. |
| 294 | + If you want to support ``OPTIONS`` http method |
| 295 | + with the default implementation, use: |
| 296 | +
|
| 297 | + .. code:: python |
| 298 | +
|
| 299 | + >>> from django_modern_rest import MetaMixin |
| 300 | +
|
| 301 | + >>> class ControllerWithMeta( |
| 302 | + ... MetaMixin, |
| 303 | + ... Controller[PydanticSerializer], |
| 304 | + ... ): ... |
| 305 | +
|
| 306 | + """ |
| 307 | + raise NotImplementedError( |
| 308 | + 'Please do not use `options` method with `django-modern-rest`, ' |
| 309 | + 'define your own `meta` method instead', |
| 310 | + ) |
| 311 | + |
221 | 312 | @classmethod |
222 | 313 | def handle_method_not_allowed( |
223 | 314 | cls, |
@@ -251,7 +342,7 @@ def existing_http_methods(cls) -> set[str]: |
251 | 342 | """Returns and caches what HTTP methods are implemented in this view.""" |
252 | 343 | return { |
253 | 344 | method |
254 | | - for method in cls.http_method_names |
| 345 | + for method in cls.http_methods |
255 | 346 | if getattr(cls, method, None) is not None |
256 | 347 | } |
257 | 348 |
|
|
0 commit comments