navigation.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """Canonical page URLs for return navigation and authentication recovery."""
  2. import re
  3. from urllib.parse import parse_qsl, quote, unquote, urlencode, urlsplit
  4. from .query_filter import import_filter_config
  5. LIST_PATH = '/app/forderungen/liste'
  6. DETAIL_PREFIX = '/app/forderungen/details/'
  7. DETAIL_PAGE = re.compile(r'^/app/forderungen/details/([^/]+)(?:/(?:kommentare|buchungen))?/?$')
  8. def valid_focus(value: str | None) -> bool:
  9. if not value or any(ord(c) < 32 or ord(c) == 127 or c in '/\\' for c in value):
  10. return False
  11. tenant, separator, number = value.partition('_')
  12. return bool(separator and tenant and number and len(tenant) <= 20 and len(number) <= 100)
  13. def _local_parts(value):
  14. if not isinstance(value, str) or not value.startswith('/') or value.startswith('//'):
  15. return None
  16. if any(ord(c) < 32 or ord(c) == 127 or c == '\\' for c in value):
  17. return None
  18. try:
  19. parts = urlsplit(value)
  20. except ValueError:
  21. return None
  22. decoded = unquote(parts.path)
  23. if parts.scheme or parts.netloc or decoded.startswith('//') or '\\' in decoded:
  24. return None
  25. if any(ord(c) < 32 or ord(c) == 127 for c in decoded):
  26. return None
  27. return parts
  28. def list_url(filters=None, focus: str | None = None) -> str:
  29. filters = filters or {}
  30. params = {f.name: filters[f.name] for f in import_filter_config('forderungen')
  31. if f.name in filters and filters[f.name] not in ('', None)}
  32. if valid_focus(focus):
  33. params['focus'] = focus
  34. return LIST_PATH + ('?' + urlencode(params) if params else '')
  35. def list_return_url(value: str | None, primary_key: str) -> str:
  36. parts = _local_parts(value)
  37. filters = dict(parse_qsl(parts.query)) if parts and parts.path == LIST_PATH else {}
  38. return list_url(filters, primary_key)
  39. def _current_parts(value):
  40. """Extract context, never a redirect origin, from the browser's current URL."""
  41. if not value:
  42. return None
  43. try:
  44. parts = urlsplit(value)
  45. except ValueError:
  46. return None
  47. if parts.scheme and parts.scheme not in ('http', 'https'):
  48. return None
  49. return _local_parts(parts.path + ('?' + parts.query if parts.query else ''))
  50. def return_url_for_request(request, primary_key: str, explicit: str | None = None) -> str:
  51. value = explicit if explicit is not None else request.query_params.get('return_to')
  52. if value is None:
  53. current = _current_parts(request.headers.get('HX-Current-URL'))
  54. match = DETAIL_PAGE.fullmatch(unquote(current.path)) if current else None
  55. if match and match[1] == primary_key:
  56. value = dict(parse_qsl(current.query)).get('return_to')
  57. return list_return_url(value, primary_key)
  58. def detail_navigation_context(request, primary_key: str, explicit: str | None = None) -> dict:
  59. return_url = return_url_for_request(request, primary_key, explicit)
  60. return {
  61. 'return_url': return_url,
  62. 'detail_post_url': DETAIL_PREFIX + quote(primary_key, safe='') + '?' + urlencode({'return_to': return_url}),
  63. }
  64. def normalize_page_url(value: str | None, default='/select', current_url: str | None = None) -> str:
  65. parts = _local_parts(value)
  66. if not parts:
  67. return default
  68. params = dict(parse_qsl(parts.query))
  69. current = _current_parts(current_url)
  70. if parts.path == LIST_PATH:
  71. focus = params.get('focus')
  72. if not focus and current and current.path == LIST_PATH:
  73. previous = dict(parse_qsl(current.query))
  74. if list_url(params) == list_url(previous):
  75. focus = previous.get('focus')
  76. return list_url(params, focus)
  77. match = DETAIL_PAGE.fullmatch(unquote(parts.path))
  78. if match and valid_focus(match[1]):
  79. primary_key = match[1]
  80. value = params.get('return_to')
  81. if value is None and current:
  82. previous = DETAIL_PAGE.fullmatch(unquote(current.path))
  83. if previous and previous[1] == primary_key:
  84. value = dict(parse_qsl(current.query)).get('return_to')
  85. detail = DETAIL_PREFIX + quote(primary_key, safe='')
  86. return detail + ('?' + urlencode({'return_to': list_return_url(value, primary_key)}) if value else '')
  87. return parts.path + ('?' + parts.query if parts.query else '') + ('#' + parts.fragment if parts.fragment else '')