mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
aurweb.util: add extend_query and to_qs helpers
The first addition, extend_query, can be used to take an existing query parameter dictionary and inject an *additions as replacement key/value pairs. The second, to_qs, converts a query parameter dictionary to a query string in the form "a=b&c=d". These two functions simplify and make dedupe_qs and quote_plus more efficient in terms of constructing custom query string overrides. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
49cc12f99d
commit
a114bd3e16
2 changed files with 28 additions and 1 deletions
|
@ -8,7 +8,8 @@ import string
|
||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from urllib.parse import quote_plus, urlparse
|
from typing import Any, Dict
|
||||||
|
from urllib.parse import quote_plus, urlencode, urlparse
|
||||||
from zoneinfo import ZoneInfo
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
import fastapi
|
import fastapi
|
||||||
|
@ -148,6 +149,17 @@ def dedupe_qs(query_string: str, *additions):
|
||||||
return '&'.join([f"{k}={quote_plus(v)}" for k, v in reversed(qs.items())])
|
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. """
|
||||||
|
for k, v in list(additions):
|
||||||
|
query[k] = v
|
||||||
|
return query
|
||||||
|
|
||||||
|
|
||||||
|
def to_qs(query: Dict[str, Any]) -> str:
|
||||||
|
return urlencode(query, doseq=True)
|
||||||
|
|
||||||
|
|
||||||
def get_vote(voteinfo, request: fastapi.Request):
|
def get_vote(voteinfo, request: fastapi.Request):
|
||||||
from aurweb.models.tu_vote import TUVote
|
from aurweb.models.tu_vote import TUVote
|
||||||
return voteinfo.tu_votes.filter(TUVote.User == request.user).first()
|
return voteinfo.tu_votes.filter(TUVote.User == request.user).first()
|
||||||
|
|
|
@ -35,3 +35,18 @@ def test_dedupe_qs():
|
||||||
def test_number_format():
|
def test_number_format():
|
||||||
assert util.number_format(0.222, 2) == "0.22"
|
assert util.number_format(0.222, 2) == "0.22"
|
||||||
assert util.number_format(0.226, 2) == "0.23"
|
assert util.number_format(0.226, 2) == "0.23"
|
||||||
|
|
||||||
|
|
||||||
|
def test_extend_query():
|
||||||
|
""" Test extension of a query via extend_query. """
|
||||||
|
query = {"a": "b"}
|
||||||
|
extended = util.extend_query(query, ("a", "c"), ("b", "d"))
|
||||||
|
assert extended.get("a") == "c"
|
||||||
|
assert extended.get("b") == "d"
|
||||||
|
|
||||||
|
|
||||||
|
def test_to_qs():
|
||||||
|
""" Test conversion from a query dictionary to a query string. """
|
||||||
|
query = {"a": "b", "c": [1, 2, 3]}
|
||||||
|
qs = util.to_qs(query)
|
||||||
|
assert qs == "a=b&c=1&c=2&c=3"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue