phase5: add saved searches, watchlist, and authenticated htmx flows
This commit is contained in:
38
apps/scouting/services/saved_searches.py
Normal file
38
apps/scouting/services/saved_searches.py
Normal file
@ -0,0 +1,38 @@
|
||||
from decimal import Decimal
|
||||
from urllib.parse import urlencode
|
||||
|
||||
from apps.players.forms import PlayerSearchForm
|
||||
|
||||
IGNORED_QUERY_KEYS = {"page", "csrfmiddlewaretoken"}
|
||||
|
||||
|
||||
def _serialize_value(value):
|
||||
if value is None:
|
||||
return None
|
||||
if hasattr(value, "pk"):
|
||||
return value.pk
|
||||
if isinstance(value, Decimal):
|
||||
return str(value)
|
||||
return value
|
||||
|
||||
|
||||
def extract_filters_from_params(params):
|
||||
payload = params.copy()
|
||||
for key in IGNORED_QUERY_KEYS:
|
||||
payload.pop(key, None)
|
||||
|
||||
form = PlayerSearchForm(payload)
|
||||
if not form.is_valid():
|
||||
return {}
|
||||
|
||||
result = {}
|
||||
for key, value in form.cleaned_data.items():
|
||||
serialized = _serialize_value(value)
|
||||
if serialized in (None, ""):
|
||||
continue
|
||||
result[key] = serialized
|
||||
return result
|
||||
|
||||
|
||||
def saved_search_to_querystring(filters: dict) -> str:
|
||||
return urlencode(filters, doseq=True)
|
||||
Reference in New Issue
Block a user