refactor: remove code redundancy and improve maintainability

- Extract duplicate view logic into reusable helper functions in views.py
- Move circular import from module level to method scope in models.py
- Create constants.py for centralized hardcoded values (Nepali words, placeholders)
- Simplify number_to_nepali_words function from 157 to 42 lines
- Refactor normalize_address with extracted helper functions
- Add lang_utils.py for consistent language parameter handling
- Reduce JavaScript duplication in address-chaining.js with helper functions
- Remove ~160+ lines of redundant code while preserving all logic
- All 79 tests passing, ruff linting passes
This commit is contained in:
sankalp committed 2026-02-05 20:49:26 +05:45
1 parent 74936ddabd
commit 718cff2951
8 files changed
+509 -309

No files matched your search

@@ -1,6 +1,20 @@
(function() {
'use strict';
// Placeholder text constants
const PLACEHOLDERS = {
province: { ne: 'प्रदेश छान्नुहोस्', en: 'Select Province' },
district: { ne: 'जिल्ला छान्नुहोस्', en: 'Select District' },
municipality: { ne: 'नगरपालिका छान्नुहोस्', en: 'Select Municipality' }
};
/**
* Get placeholder text for a specific location type
*/
function getPlaceholder(type, isNepali) {
return PLACEHOLDERS[type][isNepali ? 'ne' : 'en'];
}
function updateOptions(selectElement, data, placeholder) {
selectElement.innerHTML = '';
if (placeholder) {
@@ -43,7 +57,45 @@
return null;
}
/**
* Fetch data from server endpoint
*/
function fetchData(selectElement, paramName, paramValue, placeholderType, isNepali) {
if (!selectElement.dataset.url) return;
let url = selectElement.dataset.url + '?' + paramName + '=' + encodeURIComponent(paramValue);
if (isNepali) url += '&ne=true';
if (selectElement.dataset.en === 'true') url += '&en=true';
fetch(url)
.then(response => response.json())
.then(data => {
updateOptions(selectElement, data, getPlaceholder(placeholderType, isNepali));
});
}
/**
* Update dependent select element based on parent value
*/
function updateDependentSelect(childSelect, dataType, parentValue, placeholderType, isNepali, paramName) {
if (!childSelect) return;
if (!parentValue) {
updateOptions(childSelect, [], getPlaceholder(placeholderType, isNepali));
return;
}
const localData = getLocalData(dataType, parentValue, isNepali);
if (localData) {
updateOptions(childSelect, localData, getPlaceholder(placeholderType, isNepali));
} else {
fetchData(childSelect, paramName, parentValue, placeholderType, isNepali);
}
}
function init() {
// Initialize province selects
document.querySelectorAll('.nepkit-province-select').forEach(provinceSelect => {
const isNepali = provinceSelect.dataset.ne === 'true';
const container = provinceSelect.closest('form') || document;
@@ -52,14 +104,15 @@
if (!provinceSelect.value) {
if (districtSelect && !districtSelect.value) {
updateOptions(districtSelect, [], isNepali ? 'जिल्ला छान्नुहोस्' : 'Select District');
updateOptions(districtSelect, [], getPlaceholder('district', isNepali));
}
if (municipalitySelect && !municipalitySelect.value) {
updateOptions(municipalitySelect, [], isNepali ? 'नगरपालिका छान्नुहोस्' : 'Select Municipality');
updateOptions(municipalitySelect, [], getPlaceholder('municipality', isNepali));
}
}
});
// Initialize district selects
document.querySelectorAll('.nepkit-district-select').forEach(districtSelect => {
const isNepali = districtSelect.dataset.ne === 'true';
const container = districtSelect.closest('form') || document;
@@ -67,13 +120,14 @@
if (!districtSelect.value) {
if (municipalitySelect && !municipalitySelect.value) {
updateOptions(municipalitySelect, [], isNepali ? 'नगरपालिका छान्नुहोस्' : 'Select Municipality');
updateOptions(municipalitySelect, [], getPlaceholder('municipality', isNepali));
}
}
});
}
document.addEventListener('change', function(e) {
// Handle province select change
if (e.target.matches('.nepkit-province-select')) {
const province = e.target.value;
const isNepali = e.target.dataset.ne === 'true';
@@ -83,63 +137,22 @@
// Always clear municipality if province changes
if (municipalitySelect) {
updateOptions(municipalitySelect, [], isNepali ? 'नगरपालिका छान्नुहोस्' : 'Select Municipality');
updateOptions(municipalitySelect, [], getPlaceholder('municipality', isNepali));
}
if (districtSelect) {
if (!province) {
updateOptions(districtSelect, [], isNepali ? 'जिल्ला छान्नुहोस्' : 'Select District');
return;
}
const localData = getLocalData('districts', province, isNepali);
if (localData) {
updateOptions(districtSelect, localData, isNepali ? 'जिल्ला छान्नुहोस्' : 'Select District');
} else if (districtSelect.dataset.url) {
// Fallback to AJAX
let url = districtSelect.dataset.url + '?province=' + encodeURIComponent(province);
if (isNepali) url += '&ne=true';
if (districtSelect.dataset.en === 'true') url += '&en=true';
fetch(url)
.then(response => response.json())
.then(data => {
updateOptions(districtSelect, data, isNepali ? 'जिल्ला छान्नुहोस्' : 'Select District');
});
}
}
// Update district options
updateDependentSelect(districtSelect, 'districts', province, 'district', isNepali, 'province');
}
// Handle district select change
if (e.target.matches('.nepkit-district-select')) {
const district = e.target.value;
const isNepali = e.target.dataset.ne === 'true';
const container = e.target.closest('form') || document;
const municipalitySelect = getMatchingSelect(container, '.nepkit-municipality-select', isNepali);
if (municipalitySelect) {
if (!district) {
updateOptions(municipalitySelect, [], isNepali ? 'नगरपालिका छान्नुहोस्' : 'Select Municipality');
return;
}
const localData = getLocalData('municipalities', district, isNepali);
if (localData) {
updateOptions(municipalitySelect, localData, isNepali ? 'नगरपालिका छान्नुहोस्' : 'Select Municipality');
} else if (municipalitySelect.dataset.url) {
// Fallback to AJAX
let url = municipalitySelect.dataset.url + '?district=' + encodeURIComponent(district);
if (isNepali) url += '&ne=true';
if (municipalitySelect.dataset.en === 'true') url += '&en=true';
fetch(url)
.then(response => response.json())
.then(data => {
updateOptions(municipalitySelect, data, isNepali ? 'नगरपालिका छान्नुहोस्' : 'Select Municipality');
});
}
}
// Update municipality options
updateDependentSelect(municipalitySelect, 'municipalities', district, 'municipality', isNepali, 'district');
}
});