- Implement server-side location chaining with HTMX support via `htmx=True` in fields and widgets. - Update location views to handle HTMX requests and return HTML <option> fragments. - Add NepaliDateYearFilter and NepaliDateRangeFilter for robust DRF API filtering. - Expand default `DATE_INPUT_FORMATS` in configuration to support multiple common Nepali date styles. - Update DRF serializers to automatically respect global `NEPKIT` configuration settings. - Add `django-filter` as an optional dependency under the `drf` extra in pyproject.toml. - Correct underlying dependency references from `nepali-datetime` to nepali library. - Complete overhaul of README.md for premium DX, including new sections for storage behavior, timezone awareness, and API usage.
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
from django.http import JsonResponse, HttpResponse
|
|
from django.template.loader import render_to_string
|
|
|
|
from django_nepkit.utils import (
|
|
get_districts_by_province,
|
|
get_municipalities_by_district,
|
|
)
|
|
|
|
|
|
def _render_options(data, placeholder):
|
|
"""Internal helper to render list of options as HTML."""
|
|
options = [f'<option value="">{placeholder}</option>']
|
|
for item in data:
|
|
options.append(f'<option value="{item["id"]}">{item["text"]}</option>')
|
|
return HttpResponse("\n".join(options))
|
|
|
|
|
|
def district_list_view(request):
|
|
province = request.GET.get("province")
|
|
|
|
# Fallback: take the first non-internal parameter as province
|
|
if not province:
|
|
for key, value in request.GET.items():
|
|
if key not in ["ne", "en", "html"] and value:
|
|
province = value
|
|
break
|
|
|
|
if not province:
|
|
return JsonResponse([], safe=False)
|
|
|
|
ne = request.GET.get("ne", "false").lower() == "true"
|
|
en = request.GET.get("en", "true").lower() == "true"
|
|
as_html = request.GET.get("html", "false").lower() == "true" or request.headers.get("HX-Request") == "true"
|
|
|
|
data = get_districts_by_province(province, ne=ne, en=en)
|
|
|
|
if as_html:
|
|
placeholder = "जिल्ला छान्नुहोस्" if ne else "Select District"
|
|
return _render_options(data, placeholder)
|
|
|
|
return JsonResponse(data, safe=False)
|
|
|
|
|
|
def municipality_list_view(request):
|
|
district = request.GET.get("district")
|
|
|
|
# Fallback: take the first non-internal parameter as district
|
|
if not district:
|
|
for key, value in request.GET.items():
|
|
if key not in ["ne", "en", "html"] and value:
|
|
district = value
|
|
break
|
|
|
|
if not district:
|
|
return JsonResponse([], safe=False)
|
|
|
|
ne = request.GET.get("ne", "false").lower() == "true"
|
|
en = request.GET.get("en", "true").lower() == "true"
|
|
as_html = request.GET.get("html", "false").lower() == "true" or request.headers.get("HX-Request") == "true"
|
|
|
|
data = get_municipalities_by_district(district, ne=ne, en=en)
|
|
|
|
if as_html:
|
|
placeholder = "नगरपालिका छान्नुहोस्" if ne else "Select Municipality"
|
|
return _render_options(data, placeholder)
|
|
|
|
return JsonResponse(data, safe=False)
|