mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
add util.dedupe_qs -> dedupe_qs
Jinja2 filter
Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
b1baf76998
commit
d5e650a339
3 changed files with 42 additions and 1 deletions
|
@ -3,8 +3,9 @@ import random
|
|||
import re
|
||||
import string
|
||||
|
||||
from collections import OrderedDict
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlparse
|
||||
from urllib.parse import quote_plus, urlparse
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from email_validator import EmailNotValidError, EmailUndeliverableError, validate_email
|
||||
|
@ -106,6 +107,29 @@ 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 jsonify(obj):
|
||||
""" Perform a conversion on obj if it's needed. """
|
||||
if isinstance(obj, datetime):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue