first
This commit is contained in:
commit
fa9d154db9
260 files changed
+17643
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import json
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def location_data():
|
||||
path = Path(settings.BASE_DIR) / "static" / "data" / "iran-locations.json"
|
||||
return json.loads(path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def provinces():
|
||||
return tuple(location_data())
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def cities_by_province():
|
||||
return {item["province"]: tuple(item["cities"]) for item in location_data()}
|
||||
|
||||
|
||||
def validate_location(province, city):
|
||||
province = (province or "").strip()
|
||||
city = (city or "").strip()
|
||||
if not province and not city:
|
||||
return province, city, None
|
||||
if city and not province:
|
||||
return province, city, "برای انتخاب شهر، ابتدا استان را انتخاب کنید."
|
||||
cities = cities_by_province()
|
||||
if province not in cities:
|
||||
return province, city, "استان انتخابشده معتبر نیست."
|
||||
if city and city not in cities[province]:
|
||||
return province, city, "شهر انتخابشده متعلق به این استان نیست."
|
||||
return province, city, None
|
||||
Reference in new issue
Block a user