This commit represents a comprehensive improvement of the library's architecture, developer experience, and documentation accuracy. Core Refactoring: - apply DRY principle to models.py by introducing BaseNepaliBSField and BaseLocationField - standardize widget logic in widgets.py using NepaliWidgetMixin - centralize admin filtering logic into BaseNepaliDateFilter New Features & API: - add NepaliMonthFilter for Django Admin and NepaliDateMonthFilter for DRF - implementation of HTMX-driven location chaining for dynamic address selects - extend demo project with comprehensive DRF search, ordering, and filter examples Configuration & Sync: - implement global NEPKIT settings in conf.py (DEFAULT_LANGUAGE, ADMIN_DATEPICKER, etc.) - universalize BS_DATE_FORMAT/BS_DATETIME_FORMAT across the entire stack - synchronize frontend datepicker initialization with backend date formats Documentation & DX: - complete rewrite of README.md into high-energy, factual, English-only guides - project-wide audit and simplification of comments/docstrings for beginner accessibility - expanded 'Technical Design' and 'FAQ' sections covering data integrity and storage - added docs/migration_guide.py for bulk AD-to-BS data transitions Quality & Maintenance: - fixed all E402 module-level import errors across the library - verified codebase with project-wide ruff checks (all checks passed)
86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
from django.http import JsonResponse, HttpResponse
|
|
|
|
from django_nepkit.utils import (
|
|
get_districts_by_province,
|
|
get_municipalities_by_district,
|
|
)
|
|
|
|
|
|
from django_nepkit.conf import nepkit_settings
|
|
|
|
|
|
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)
|
|
|
|
default_lang = nepkit_settings.DEFAULT_LANGUAGE
|
|
ne_param = request.GET.get("ne")
|
|
ne = ne_param.lower() == "true" if ne_param else default_lang == "ne"
|
|
|
|
en_param = request.GET.get("en")
|
|
en = en_param.lower() == "true" if en_param else not ne
|
|
|
|
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)
|
|
|
|
default_lang = nepkit_settings.DEFAULT_LANGUAGE
|
|
ne_param = request.GET.get("ne")
|
|
ne = ne_param.lower() == "true" if ne_param else default_lang == "ne"
|
|
|
|
en_param = request.GET.get("en")
|
|
en = en_param.lower() == "true" if en_param else not ne
|
|
|
|
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)
|