From 683d99ac1a91264383c07cd488be54ee51e1ab40 Mon Sep 17 00:00:00 2001 From: Horilla Date: Wed, 23 Apr 2025 22:55:32 +0530 Subject: [PATCH] [UPDT] BASE: Updated base app ModelFrom class,Use user's company if present in queryset, otherwise fallback to first available option to the company field --- base/forms.py | 8 ++++++-- base/methods.py | 35 ++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/base/forms.py b/base/forms.py index 267dca5ae..d588ffb11 100644 --- a/base/forms.py +++ b/base/forms.py @@ -245,8 +245,12 @@ class ModelForm(forms.ModelForm): pass try: - self.fields["company_id"].initial = ( - request.user.employee_get.get_company + company_field = self.fields["company_id"] + company = request.user.employee_get.get_company + company_queryset = company_field.queryset + + company_field.initial = ( + company if company in company_queryset else company_queryset.first() ) except: pass diff --git a/base/methods.py b/base/methods.py index 1e4014919..e4522de17 100644 --- a/base/methods.py +++ b/base/methods.py @@ -608,29 +608,30 @@ def export_data(request, model, form_class, filter_class, file_name, perm=None): def reload_queryset(fields): """ - This method is used to reload the querysets in the form + Reloads querysets in the form based on active filters and selected company. """ + request = getattr(_thread_locals, "request", None) + selected_company = request.session.get("selected_company") if request else None + + recruitment_installed = apps.is_installed("recruitment") model_filters = { "Employee": {"is_active": True}, - "Candidate": {"is_active": True} if apps.is_installed("recruitment") else None, + "Candidate": {"is_active": True} if recruitment_installed else None, } - request = getattr(_thread_locals, "request", None) - selected_company = request.session.get("selected_company") if request else None for field in fields.values(): - if isinstance(field, ModelChoiceField): - model_name = field.queryset.model.__name__ - filter_criteria = model_filters.get(model_name) - if filter_criteria is not None: - field.queryset = field.queryset.model.objects.filter(**filter_criteria) - # Future updation for company select field options when select a comapany from navbar - # Comment this line to ensure dynamically update the list of departments - # based on the company selected from the navbar. - # on the /employee-view-update// page - # elif selected_company and not selected_company == 'all': - # field.queryset = field.queryset.model.objects.filter(id=selected_company) - else: - field.queryset = field.queryset.model.objects.all() + if not isinstance(field, ModelChoiceField): + continue + + model = field.queryset.model + model_name = model.__name__ + + if model_name == "Company" and selected_company and selected_company != "all": + field.queryset = model.objects.filter(id=selected_company) + elif (filters := model_filters.get(model_name)) is not None: + field.queryset = model.objects.filter(**filters) + else: + field.queryset = model.objects.all() return fields