Untitled

 avatar
unknown
python
6 months ago
3.8 kB
3
Indexable
@method_decorator(csrf_exempt, name='dispatch')
class FilteredCompanyListView(LoginRequiredMixin, StaffRequiredMixin, View):
    def post(self, request, *args, **kwargs):
        context = {}
        try:
            # Step 1: Extract data from the POST request
            data = json.loads(request.body)
            selected_companies = data.get("selected_companies", [])
            representative_id = data.get("representative_id")

            if not selected_companies or not representative_id:
                messages.error(
                    request, "Invalid data submitted. Please try again."
                )
                return redirect(request.path)  # Redirect back to the form

            # Step 2: Initiate your task with selected_companies and representative_id
            allocate_customer_representative_to_companies.delay(
                selected_companies, representative_id
            )

            context.update(
                {
                    "message": "Task initiated successfully for the selected companies"
                }
            )
            return render(
                request, "admin/recruiter/allocate_companies.html", context
            )

        except json.JSONDecodeError:
            context.update({"message": "Error invoking the task"})
            return render(
                request, "admin/recruiter/allocate_companies.html", context
            )
        except Exception as e:
            context.update(
                {"message": "An error occurred while invoking the task"}
            )
            return render(
                request, "admin/recruiter/allocate_companies.html", context
            )

    def get(self, request):
        exclude_large_companies = (
            request.GET.get("exclude_large_companies", "false") == "true"
        )
        is_international = (
            request.GET.get("isInternational", "false") == "true"
        )
        is_indian = request.GET.get("isIndian", "false") == "true"
        num_companies = request.GET.get("num_companies", 10)
        representative_id = request.GET.get("representative_id", None)
        context = {"representative_id": representative_id}

        filter_all_companies = is_indian and is_international

        # Base queryset with non-blocked, active and paid companies
        companies = Company.objects.filter(
            is_blocked=False,
            account_type=Company.AccountType.ACTIVE,
            og_country__isnull=False,
            stripe_subscription_id__isnull=False,
            customer_representative__isnull=True,
        )

        # Apply filters based on query parameters
        if exclude_large_companies:
            companies = companies.annotate(recruiter_count=Count("recruiter"))
            companies = companies.exclude(recruiter_count__gt=9)

        if is_international and not filter_all_companies:
            # international if og_country is not india
            companies = companies.exclude(og_country__icontains="india")

            if num_companies:
                companies = companies[: int(num_companies)]

            context.update({"companies": companies})

            return render(
                request, "admin/recruiter/allocate_companies.html", context
            )

        if is_indian and not filter_all_companies:
            companies = companies.filter(og_country__icontains="india")

            if num_companies:
                companies = companies[: int(num_companies)]

            context.update({"companies": companies})
            return render(
                request, "admin/recruiter/allocate_companies.html", context
            )

        context.update({"companies": companies})
        return render(
            request, "admin/recruiter/allocate_companies.html", context
        )
Editor is loading...
Leave a Comment