Fix typing errors in form.py

This commit is contained in:
Yannik Rödel 2024-04-04 17:48:16 +02:00
parent 717acb8efe
commit e2623df390

View file

@ -131,15 +131,16 @@ if form_disabled:
form = cgi.FieldStorage() form = cgi.FieldStorage()
@overload @overload
def get_form_value(name: str, default: Optional[str], cast: type[str] = str) -> str: ... def get_form_value(name: str, default: None = ..., *, cast: type[bytes]) -> tuple[str, bytes]:...
@overload @overload
def get_form_value(name: str, default: Optional[int], cast: type[int]) -> int:... def get_form_value(name: str, default: Optional[int] = ..., *, cast: type[int]) -> int:...
@overload @overload
def get_form_value(name: str, default: None = ..., cast: type[bytes] = ...) -> tuple[str, bytes]:... def get_form_value(name: str, default: Optional[str] = ..., *, cast: type[str] = ...) -> str: ...
def get_form_value( def get_form_value(
name: str, name: str,
default: Any = None, default: Any = None,
cast: type[str] | type[int] | type[io.BytesIO] = str, *,
cast: type[str] | type[int] | type[bytes] = str,
) -> Any: ) -> Any:
if name not in form: if name not in form:
if default is None: if default is None:
@ -166,7 +167,7 @@ def get_form_value(
# constant-time string comparison here. # constant-time string comparison here.
given_csrf_token = get_form_value("csrftoken") given_csrf_token = get_form_value("csrftoken")
if not hmac.compare_digest(csrf_token, given_csrf_token): if not hmac.compare_digest(csrf_token, given_csrf_token):
fail("400 Bad Request", f"Invalid CSRF token") fail("400 Bad Request", "Invalid CSRF token")
# If the honeypot field was not empty, back off. # If the honeypot field was not empty, back off.
@ -194,7 +195,7 @@ if not EMAIL_REGEX.fullmatch(contact_email):
message = get_form_value("message", "[Keine Nachricht hinterlassen]") message = get_form_value("message", "[Keine Nachricht hinterlassen]")
attachment: Optional[tuple[str, bytes]] = None attachment: Optional[tuple[str, bytes]] = None
ticket_details = collections.OrderedDict() ticket_details = collections.OrderedDict[str, str | int]()
ticket_details["Kontaktperson"] = contact_name ticket_details["Kontaktperson"] = contact_name
ticket_details["Email"] = contact_email ticket_details["Email"] = contact_email
@ -220,9 +221,9 @@ match request_uri:
ticket_details["Adresse"] = get_form_value("addressline") ticket_details["Adresse"] = get_form_value("addressline")
ticket_details["PLZ"] = get_form_value("postalcode") ticket_details["PLZ"] = get_form_value("postalcode")
ticket_details["Stadt"] = get_form_value("city") ticket_details["Stadt"] = get_form_value("city")
ticket_details["Anzahl Desktops"] = get_form_value("desktopcount", 0, int) ticket_details["Anzahl Desktops"] = get_form_value("desktopcount", 0, cast=int)
ticket_details["Anzahl Laptops"] = get_form_value("laptopcount", 0, int) ticket_details["Anzahl Laptops"] = get_form_value("laptopcount", 0, cast=int)
ticket_details["Anzahl Drucker"] = get_form_value("printercount", 0, int) ticket_details["Anzahl Drucker"] = get_form_value("printercount", 0, cast=int)
case "/computer-beantragen/privat": case "/computer-beantragen/privat":
form_name = "Computerantrag (privat)" form_name = "Computerantrag (privat)"
@ -256,13 +257,13 @@ match request_uri:
ticket_details["Teilnehmenden-Name"] = get_form_value("participantname", "-") ticket_details["Teilnehmenden-Name"] = get_form_value("participantname", "-")
ticket_details["Telefonnummer"] = get_form_value("contactphone", "-") ticket_details["Telefonnummer"] = get_form_value("contactphone", "-")
ticket_details["Fotos?"] = get_form_value("photos") ticket_details["Fotos?"] = get_form_value("photos")
case "/party": case "/party":
form_name = "CoderDojo Minecraft LAN" form_name = "CoderDojo Minecraft LAN"
form_group = "CoderDojo" form_group = "CoderDojo"
ticket_details["Java-Spielername"] = get_form_value("javaname", "") ticket_details["Java-Spielername"] = get_form_value("javaname", "")
ticket_details["Bedrock-Spielername"] = get_form_value("bedrockname", "") ticket_details["Bedrock-Spielername"] = get_form_value("bedrockname", "")
case "/freizeit": case "/freizeit":
form_name = "CoderCamp Umfrage" form_name = "CoderCamp Umfrage"
form_group = "CoderDojo" form_group = "CoderDojo"