mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
Eradicate the dedupe_qs
filter
The new `extend_query` and `urlencode` filters are way cleaner ways to achieve what we did with `dedupe_qs`. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
c9374732c0
commit
210e459ba9
5 changed files with 11 additions and 53 deletions
|
@ -5,7 +5,6 @@ import typing
|
|||
|
||||
from datetime import datetime
|
||||
from http import HTTPStatus
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
from fastapi import APIRouter, Form, HTTPException, Request
|
||||
from fastapi.responses import RedirectResponse, Response
|
||||
|
@ -106,12 +105,12 @@ async def trusted_user(request: Request,
|
|||
context["current_by_next"] = "asc" if current_by == "desc" else "desc"
|
||||
context["past_by_next"] = "asc" if past_by == "desc" else "desc"
|
||||
|
||||
context["q"] = '&'.join([
|
||||
f"coff={current_off}",
|
||||
f"cby={quote_plus(current_by)}",
|
||||
f"poff={past_off}",
|
||||
f"pby={quote_plus(past_by)}"
|
||||
])
|
||||
context["q"] = {
|
||||
"coff": current_off,
|
||||
"cby": current_by,
|
||||
"poff": past_off,
|
||||
"pby": past_by
|
||||
}
|
||||
|
||||
return render_template(request, "tu/index.html", context)
|
||||
|
||||
|
|
|
@ -30,7 +30,6 @@ _env.filters["tn"] = l10n.tn
|
|||
# Utility filters.
|
||||
_env.filters["dt"] = util.timestamp_to_datetime
|
||||
_env.filters["as_timezone"] = util.as_timezone
|
||||
_env.filters["dedupe_qs"] = util.dedupe_qs
|
||||
_env.filters["extend_query"] = util.extend_query
|
||||
_env.filters["urlencode"] = util.to_qs
|
||||
_env.filters["quote_plus"] = quote_plus
|
||||
|
|
|
@ -6,10 +6,9 @@ import re
|
|||
import secrets
|
||||
import string
|
||||
|
||||
from collections import OrderedDict
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict
|
||||
from urllib.parse import quote_plus, urlencode, urlparse
|
||||
from urllib.parse import urlencode, urlparse
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import fastapi
|
||||
|
@ -126,31 +125,8 @@ def as_timezone(dt: datetime, timezone: str):
|
|||
return dt.astimezone(tz=ZoneInfo(timezone))
|
||||
|
||||
|
||||
def dedupe_qs(query_string: str, *additions):
|
||||
""" Dedupe keys found in a query string by rewriting it without
|
||||
duplicates found while iterating from the end to the beginning,
|
||||
using an ordered memo to track keys found and persist locations.
|
||||
|
||||
That is, query string 'a=1&b=1&a=2' will be deduped and converted
|
||||
to 'b=1&a=2'.
|
||||
|
||||
:param query_string: An HTTP URL query string.
|
||||
:param *additions: Optional additional fields to add to the query string.
|
||||
:return: Deduped query string, including *additions at the tail.
|
||||
"""
|
||||
for addition in list(additions):
|
||||
query_string += f"&{addition}"
|
||||
|
||||
qs = OrderedDict()
|
||||
for item in reversed(query_string.split('&')):
|
||||
key, value = item.split('=')
|
||||
if key not in qs:
|
||||
qs[key] = value
|
||||
return '&'.join([f"{k}={quote_plus(v)}" for k, v in reversed(qs.items())])
|
||||
|
||||
|
||||
def extend_query(query: Dict[str, Any], *additions) -> Dict[str, Any]:
|
||||
""" Add additionally key value pairs to query. """
|
||||
""" Add additional key value pairs to query. """
|
||||
for k, v in list(additions):
|
||||
query[k] = v
|
||||
return query
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue