Source code for edri.dataclass.directive.html
from dataclasses import dataclass, field
from typing import Type, Any
from edri.dataclass.event import Event
from edri.dataclass.directive import HTMLResponseDirective
from edri.utility.function import format_url
from edri.api.event_registry import event_type_extensions
__all__ = ["RedirectResponseDirective"]
[docs]
@dataclass
class RedirectResponseDirective(HTMLResponseDirective):
"""Directive for redirecting to another URL, status 302 will be used"""
location: Type[Event] | str # The explicitly required parameter
kwargs: dict[str, Any] = field(init=False, repr=False, default_factory=dict)
def __init__(self, location: Type[Event] | str, /, **kwargs: Any) -> None:
self.location = location
self.kwargs = kwargs
self.__post_init__()
def __post_init__(self) -> None:
if not isinstance(self.location, str):
return
if "\r" in self.location or "\n" in self.location:
raise ValueError("Redirect location must not contain CR/LF characters")
if "\x00" in self.location:
raise ValueError("Redirect location must not contain null bytes")
@property
def path(self) -> str:
if isinstance(self.location, type) and issubclass(self.location, Event):
extensions = event_type_extensions[self.location]
return format_url(extensions["url"], **self.kwargs)
return self.location