def get(self, request):
     filter_args = {}
     country_code = self.request.GET.get("country", None)
     supplier_id = self.request.GET.get("supplier", None)
     buyer_id = self.request.GET.get("buyer", None)
     product = self.request.GET.get("product", None)
     if country_code:
         country_code = str(country_code).upper()
         filter_args["country__country_code_alpha_2"] = country_code
     if supplier_id:
         filter_args = add_filter_args("supplier", supplier_id, filter_args)
     if buyer_id:
         filter_args = add_filter_args("buyer", buyer_id, filter_args)
     if product:
         filter_args[
             "goods_services__goods_services_category__id"] = product
     red_flags = RedFlag.objects.filter(implemented=True)
     value = []
     result = {"result": value}
     for red_flag in red_flags:
         count = Tender.objects.filter(
             **filter_args, red_flag__pk=red_flag.id).distinct().count()
         data = {}
         data["red_flag_id"] = red_flag.id
         data["red_flag"] = red_flag.title
         data["tender_count"] = count
         value.append(data)
     return JsonResponse(result, safe=False)
    def get_queryset(self):
        country_code = self.request.GET.get("country", None)
        buyer_id = self.request.GET.get("buyer", None)
        supplier_id = self.request.GET.get("supplier", None)
        product_id = self.request.GET.get("product", None)
        status = self.request.GET.get("status", None)
        procurement_procedure = self.request.GET.get("procurement_procedure",
                                                     None)
        title = self.request.GET.get("title", None)
        date_from = self.request.GET.get("date_from", None)
        date_to = self.request.GET.get("date_to", None)
        contract_value_usd = self.request.GET.get("contract_value_usd", None)
        value_comparison = self.request.GET.get("value_comparison", None)
        red_flag_id = self.request.GET.get("red_flag", None)
        equity_id = self.request.GET.get("equity", None)
        filter_args = {}
        exclude_args = {}
        annotate_args = {}

        if equity_id:
            filter_args = add_filter_args("equity_category", equity_id,
                                          filter_args)
        if red_flag_id:
            filter_args["red_flag__id"] = red_flag_id
        if country_code:
            filter_args["country__country_code_alpha_2"] = country_code.upper()
        if buyer_id:
            filter_args = add_filter_args("buyer", buyer_id, filter_args)
        if supplier_id:
            filter_args = add_filter_args("supplier", supplier_id, filter_args)
        if product_id:
            filter_args["goods_services__goods_services_category"] = product_id
        if title:
            filter_args["contract_title__icontains"] = title
        if date_from and date_to:
            filter_args["contract_date__range"] = [date_from, date_to]
        if contract_value_usd and value_comparison:
            if value_comparison == "gt":
                annotate_args["sum"] = Sum("contract_value_usd")
                filter_args["sum__gte"] = contract_value_usd
            elif value_comparison == "lt":
                annotate_args["sum"] = Sum("contract_value_usd")
                filter_args["sum__lte"] = contract_value_usd
        if status == "others":
            exclude_args["status__in"] = ["active", "canceled", "completed"]
        elif status in ["active", "canceled", "completed"]:
            filter_args["status"] = status
        if procurement_procedure == "others":
            exclude_args["procurement_procedure__in"] = [
                "open", "limited", "direct", "selective"
            ]
        elif procurement_procedure in [
                "open", "limited", "direct", "selective"
        ]:
            filter_args["procurement_procedure"] = procurement_procedure
        return (Tender.objects.prefetch_related(
            "buyer", "supplier", "country").annotate(**annotate_args).filter(
                **filter_args).exclude(**exclude_args))
Exemplo n.º 3
0
    def get(self, request):
        country_code = self.request.GET.get("country", None)
        buyer_id = self.request.GET.get("buyer")
        supplier_id = self.request.GET.get("supplier")
        filter_args = {}
        country_currency = ""

        results = {}

        if buyer_id:
            filter_args = add_filter_args("buyer", buyer_id, filter_args)
            results = procurement_procedure_amount(**filter_args)

        if supplier_id:
            filter_args = add_filter_args("supplier", supplier_id, filter_args)
            results = procurement_procedure_amount(**filter_args)

        if country_code:
            country_code = str(country_code).upper()
            country_obj = Country.objects.get(
                country_code_alpha_2=country_code)
            country_currency = country_obj.currency

            filter_args = add_filter_args("country__country_code_alpha_2",
                                          country_code,
                                          filter_args,
                                          append_only=True)
            results = procurement_procedure_amount(**filter_args)

        if not country_code and not supplier_id and not buyer_id:
            results = procurement_procedure_amount(**filter_args)

        response = []

        for result in results:
            response.append({
                "amount_local":
                result["local"] if result["local"] else 0,
                "amount_usd":
                result["usd"] if result["usd"] else 0,
                "tender_count":
                result["count"],
                "local_currency_code":
                country_currency,
                "status":
                result["procurement_procedure"]
                if result["procurement_procedure"] else "",
            })

        return JsonResponse(response, safe=False)
Exemplo n.º 4
0
 def get(self, request):
     filter_args = {}
     country_code = self.request.GET.get("country", None)
     buyer_id = self.request.GET.get("buyer")
     supplier_id = self.request.GET.get("supplier")
     if country_code:
         country_code = str(country_code).upper()
         filter_args["country__country_code_alpha_2"] = country_code
     if buyer_id:
         filter_args = add_filter_args("buyer", buyer_id, filter_args)
     if supplier_id:
         filter_args = add_filter_args("supplier", supplier_id, filter_args)
     result = []
     try:
         tenders_assigned = (
             Tender.objects.filter(**filter_args)
             .exclude(goods_services__goods_services_category=None)
             .annotate(month=TruncMonth("contract_date"))
             .values(
                 "month",
                 "goods_services__goods_services_category__category_name",
                 "goods_services__goods_services_category__id",
                 "country__currency",
             )
             .annotate(
                 count=Count("id"),
                 local=Sum("contract_value_local"),
                 usd=Sum("contract_value_usd"),
             )
             .order_by("-month")
         )
         for tender in tenders_assigned:
             data = {
                 "amount_local": tender["local"],
                 "amount_usd": tender["usd"],
                 "date": tender["month"],
                 "local_currency_code": tender["country__currency"],
                 "product_id": tender["goods_services__goods_services_category__id"],
                 "product_name": tender["goods_services__goods_services_category__category_name"],
                 "tender_count": tender["count"],
             }
             result.append(data)
         return JsonResponse(result, safe=False)
     except Exception:
         return JsonResponse([{"error": "Invalid country_code"}], safe=False)
    def get(self, request):
        """
        Return a list of all contracts.
        """
        # Calculating total tender
        country_code = self.request.GET.get("country", None)
        buyer_id = self.request.GET.get("buyer")
        supplier_id = self.request.GET.get("supplier")

        total_contract = 0
        bar_chart_list = []
        filter_args = {}
        if country_code:
            country_code = str(country_code).upper()
            filter_args["country__country_code_alpha_2"] = country_code
        if buyer_id:
            filter_args = add_filter_args("buyer", buyer_id, filter_args)
        if supplier_id:
            filter_args = add_filter_args("supplier", supplier_id, filter_args)

        bar_chart = (Tender.objects.filter(
            **filter_args).values("procurement_procedure").annotate(
                count=Count("procurement_procedure"),
                total_contract=Count("id")))
        for i in bar_chart:
            total_contract += i["total_contract"]
            bar_chart_list.append({
                "method": i["procurement_procedure"],
                "value": i["count"]
            })

        line_chart = (Tender.objects.filter(**filter_args).annotate(
            month=TruncMonth("contract_date")).values("month").annotate(
                total=Count("id")).order_by("-month"))
        line_chart_list = [{
            "date": i["month"],
            "value": i["total"]
        } for i in line_chart]
        result = {
            "total": total_contract,
            "line_chart": line_chart_list,
            "bar_chart": bar_chart_list,
        }
        return JsonResponse(result)
    def get(self, request):
        filter_args = {}
        result = list()
        currency_code = ""

        country_code = self.request.GET.get("country", None)
        buyer_id = self.request.GET.get("buyer")

        if country_code:
            country_code = str(country_code).upper()
            filter_args["country__country_code_alpha_2"] = country_code

        if buyer_id:
            filter_args = add_filter_args("buyer", buyer_id, filter_args)

        if country_code:
            try:
                country_res = Country.objects.get(
                    country_code_alpha_2=country_code)
                currency_code = country_res.currency
            except Exception as e:
                return JsonResponse(
                    {"error": str(e)},
                    safe=False,
                    status=status_code.HTTP_500_INTERNAL_SERVER_ERROR)

        status_query_set = (Tender.objects.filter(
            **filter_args).values("status").annotate(
                amount_usd=Sum("contract_value_usd"),
                amount_local=Sum("contract_value_local"),
                tender_count=Count("id"),
            ))

        for status in status_query_set:
            data = {
                "amount_local": status["amount_local"],
                "amount_usd": status["amount_usd"],
                "tender_count": status["tender_count"],
                "local_currency_code": currency_code,
                "status": status["status"],
            }

            result.append(data)

        return JsonResponse(result, safe=False)
Exemplo n.º 7
0
    def get(self, request):
        """
        Returns average bids for contracts
        """
        country_code = self.request.GET.get("country", None)
        buyer_id = self.request.GET.get("buyer")

        filter_args = {}
        filter_args["no_of_bidders__gte"] = 1
        if country_code:
            country_code = str(country_code).upper()
            filter_args["country__country_code_alpha_2"] = country_code
        if buyer_id:
            filter_args = add_filter_args("buyer", buyer_id, filter_args)

        # Month wise average of number of bids for contracts
        monthwise_data = (
            Tender.objects.filter(**filter_args)
            .annotate(month=TruncMonth("contract_date"))
            .values("month")
            .annotate(count=Count("id"), sum=Sum("no_of_bidders"))
            .order_by("-month")
        )
        final_line_chart_data = [
            {
                "date": monthwise_data[i]["month"],
                "value": round(monthwise_data[i]["sum"] / monthwise_data[i]["count"], 1)
                if monthwise_data[i]["sum"]
                else 0,
            }
            for i in range(len(monthwise_data))
        ]

        # Difference percentage calculation

        result = {
            "average": round(sum(item["value"] for item in final_line_chart_data) / len(final_line_chart_data), 1)
            if len(final_line_chart_data) > 0
            else 0,
            "line_chart": final_line_chart_data,
        }
        return JsonResponse(result)
    def get(self, request):
        country_code = self.request.GET.get("country", None)
        buyer_id = self.request.GET.get("buyer", None)

        filter_args = {"supplier__isnull": False}

        if buyer_id:
            filter_args = add_filter_args("buyer", buyer_id, filter_args)

        if country_code:
            country_code = str(country_code).upper()
            filter_args["country__country_code_alpha_2"] = country_code

        try:
            suppliers = (Tender.objects.filter(**filter_args).values(
                "supplier__id", "supplier__supplier_name").distinct())

            result = []

            for supplier in suppliers:
                data = {
                    "id": supplier["supplier__id"],
                    "name": supplier["supplier__supplier_name"]
                }

                if country_code:
                    data["country_code"] = country_code

                result.append(data)

            return JsonResponse(result, safe=False)

        except Exception:
            return JsonResponse([{
                "error": "Country code does not exists"
            }],
                                safe=False)
    def get(self, request):
        """
        Return a list of all contracts.
        """

        # Calculating total tender
        country_code = self.request.GET.get("country", None)
        buyer_id = self.request.GET.get("buyer")
        supplier_id = self.request.GET.get("supplier")

        filter_args = {}
        exclude_args = {"status": "canceled"}
        if country_code:
            country_code = str(country_code).upper()
            filter_args["country__country_code_alpha_2"] = country_code
        if buyer_id:
            filter_args = add_filter_args("buyer", buyer_id, filter_args)
        if supplier_id:
            filter_args = add_filter_args("supplier", supplier_id, filter_args)

        total_country_tender_amount = (
            Tender.objects.filter(**filter_args)
            .exclude(**exclude_args)
            .aggregate(usd=Sum("contract_value_usd"), local=Sum("contract_value_local"))
        )

        bar_chart = (
            Tender.objects.filter(**filter_args)
            .exclude(**exclude_args)
            .values("procurement_procedure")
            .annotate(usd=Sum("contract_value_usd"), local=Sum("contract_value_local"))
        )
        selective_sum_local = 0
        limited_sum_local = 0
        open_sum_local = 0
        direct_sum_local = 0
        limited_total = 0
        open_total = 0
        selective_total = 0
        direct_total = 0
        not_identified_total = 0
        not_identified_sum_local = 0

        for i in bar_chart:
            if i["procurement_procedure"] == "selective":
                selective_total = i["usd"]
                selective_sum_local = i["local"]
            elif i["procurement_procedure"] == "limited":
                limited_total = i["usd"]
                limited_sum_local = i["local"]
            elif i["procurement_procedure"] == "open":
                open_total = i["usd"]
                open_sum_local = i["local"]
            elif i["procurement_procedure"] == "direct":
                direct_total = i["usd"]
                direct_sum_local = i["local"]
            elif i["procurement_procedure"] == "not_identified":
                not_identified_total = i["usd"]
                not_identified_sum_local = i["local"]

        line_chart = (
            Tender.objects.filter(**filter_args)
            .exclude(**exclude_args)
            .annotate(month=TruncMonth("contract_date"))
            .values("month")
            .annotate(usd=Sum("contract_value_usd"), local=Sum("contract_value_local"))
            .order_by("-month")
        )
        line_chart_local_list = [{"date": i["month"], "value": i["local"]} for i in line_chart]
        line_chart_list = [{"date": i["month"], "value": i["usd"]} for i in line_chart]

        result = {
            "usd": {
                "total": total_country_tender_amount["usd"],
                "line_chart": line_chart_list,
                "bar_chart": [
                    {"method": "open", "value": open_total},
                    {"method": "limited", "value": limited_total},
                    {"method": "selective", "value": selective_total},
                    {"method": "direct", "value": direct_total},
                    {"method": "not_identified", "value": not_identified_total},
                ],
            },
            "local": {
                "total": total_country_tender_amount["local"],
                "line_chart": line_chart_local_list,
                "bar_chart": [
                    {"method": "open", "value": open_sum_local},
                    {"method": "limited", "value": limited_sum_local},
                    {"method": "selective", "value": selective_sum_local},
                    {"method": "direct", "value": direct_sum_local},
                    {"method": "not_identified", "value": not_identified_sum_local},
                ],
            },
        }
        return JsonResponse(result)
 def get(self, request):
     filter_args = {}
     country_code = self.request.GET.get("country", None)
     buyer_id = self.request.GET.get("buyer")
     supplier_id = self.request.GET.get("supplier")
     currency = "USD"
     if supplier_id:
         filter_args = add_filter_args("supplier", supplier_id, filter_args)
     if country_code:
         country_code = str(country_code).upper()
         filter_args["country__country_code_alpha_2"] = country_code
         instance = Country.objects.get(country_code_alpha_2=country_code)
         currency = instance.currency
     if buyer_id:
         filter_args = add_filter_args("buyer", buyer_id, filter_args)
     cum_dict = {}
     final_data = []
     categories = GoodsServicesCategory.objects.all()
     tenders = (Tender.objects.exclude(
         goods_services__goods_services_category=None).annotate(
             month=TruncMonth("contract_date")).values("month").annotate(
                 count=Count("id")).order_by("month"))
     for tender in tenders:
         end_date = tender["month"] + dateutil.relativedelta.relativedelta(
             months=1)
         start_date = tender["month"]
         result = {
             "month": str(start_date.year) + "-" + str(start_date.month),
             "details": []
         }
         for category in categories:
             data = {}
             if country_code:
                 good_services = (GoodsServices.objects.filter(
                     contract__country__country_code_alpha_2=country_code,
                     goods_services_category=category,
                     contract__contract_date__gte=start_date,
                     contract__contract_date__lte=end_date,
                 ).values("goods_services_category__category_name",
                          "goods_services_category__id").annotate(
                              count=Count("id"),
                              local=Sum("contract_value_local"),
                              usd=Sum("contract_value_usd")))
             else:
                 good_services = (GoodsServices.objects.filter(
                     goods_services_category=category,
                     contract__contract_date__gte=start_date,
                     contract__contract_date__lte=end_date,
                 ).values("goods_services_category__category_name",
                          "goods_services_category__id").annotate(
                              count=Count("id"),
                              local=Sum("contract_value_local"),
                              usd=Sum("contract_value_usd")))
             tender_count = Tender.objects.filter(
                 contract_date__gte=start_date,
                 contract_date__lte=end_date,
                 goods_services__goods_services_category=category,
             ).count()
             data["product_name"] = category.category_name
             data["product_id"] = category.id
             local_value = [i["local"] for i in good_services]
             usd_value = [i["usd"] for i in good_services]
             if category.category_name in cum_dict.keys():
                 if "local" in cum_dict[category.category_name].keys():
                     cum_dict[category.category_name]["local"] = cum_dict[
                         category.category_name]["local"] + (
                             local_value[0] if local_value else 0)
                 if "usd" in cum_dict[category.category_name].keys():
                     cum_dict[category.category_name]["usd"] = cum_dict[
                         category.category_name]["usd"] + (usd_value[0] if
                                                           usd_value else 0)
                 if "count" in cum_dict[category.category_name].keys():
                     cum_dict[category.category_name]["count"] = (
                         cum_dict[category.category_name]["count"] +
                         tender_count)
             else:
                 cum_dict[category.category_name] = {
                     "local": 0,
                     "usd": 0,
                     "count": 0
                 }
                 cum_dict[category.category_name]["local"] = cum_dict[
                     category.category_name]["local"] + (local_value[0] if
                                                         local_value else 0)
                 cum_dict[category.category_name]["usd"] = cum_dict[
                     category.category_name]["usd"] + (usd_value[0]
                                                       if usd_value else 0)
                 cum_dict[category.category_name]["count"] = (
                     cum_dict[category.category_name]["count"] +
                     tender_count)
             data["amount_local"] = cum_dict[
                 category.category_name]["local"]
             data["amount_usd"] = cum_dict[category.category_name]["usd"]
             data["currency"] = currency
             data["tender_count"] = cum_dict[
                 category.category_name]["count"]
             result["details"].append(data)
         final_data.append(result)
     return JsonResponse(final_data, safe=False)
Exemplo n.º 11
0
 def get(self, request):
     filter_args = {}
     country_code = self.request.GET.get("country", None)
     buyer_id = self.request.GET.get("buyer")
     if country_code:
         country_code = str(country_code).upper()
         filter_args["country__country_code_alpha_2"] = country_code
     if buyer_id:
         filter_args = add_filter_args("buyer", buyer_id, filter_args)
     if country_code:
         try:
             country_instance = Country.objects.get(
                 country_code_alpha_2=country_code)
             filter_args["country"] = country_instance
             tenders_assigned = (Tender.objects.filter(
                 **filter_args).exclude(equity_category=None).aggregate(
                     total_usd=Sum("contract_value_usd"),
                     total_local=Sum("contract_value_local"),
                 ))
             assigned_count = Tender.objects.filter(**filter_args).exclude(
                 equity_category=None).count()
             filter_args["equity_category"] = None
             tenders_unassigned = Tender.objects.filter(
                 **filter_args).aggregate(
                     total_usd=Sum("contract_value_usd"),
                     total_local=Sum("contract_value_local"),
                 )
             unassigned_count = Tender.objects.filter(**filter_args).count()
             data = [
                 {
                     "amount_local": tenders_assigned["total_local"],
                     "amount_usd": tenders_assigned["total_usd"],
                     "tender_count": assigned_count,
                     "local_currency_code": country_instance.currency,
                     "type": "assigned",
                 },
                 {
                     "amount_local": tenders_unassigned["total_local"],
                     "amount_usd": tenders_unassigned["total_usd"],
                     "tender_count": unassigned_count,
                     "local_currency_code": country_instance.currency,
                     "type": "unassigned",
                 },
             ]
             return JsonResponse(data, safe=False)
         except Exception:
             return JsonResponse([{
                 "error": "Invalid country_code"
             }],
                                 safe=False)
     else:
         tenders_assigned = (Tender.objects.filter(**filter_args).exclude(
             equity_category=None).aggregate(
                 total_usd=Sum("contract_value_usd"),
                 total_local=Sum("contract_value_local"),
             ))
         assigned_count = Tender.objects.filter(**filter_args).exclude(
             equity_category=None).count()
         filter_args["equity_category"] = None
         tenders_unassigned = Tender.objects.filter(
             **filter_args).aggregate(
                 total_usd=Sum("contract_value_usd"),
                 total_local=Sum("contract_value_local"),
             )
         unassigned_count = Tender.objects.filter(**filter_args).count()
         data = [
             {
                 "amount_local": tenders_assigned["total_local"],
                 "amount_usd": tenders_assigned["total_usd"],
                 "tender_count": assigned_count,
                 "local_currency_code": "USD",
                 "type": "assigned",
             },
             {
                 "amount_local": tenders_unassigned["total_local"],
                 "amount_usd": tenders_unassigned["total_usd"],
                 "tender_count": unassigned_count,
                 "local_currency_code": "USD",
                 "type": "unassigned",
             },
         ]
         return JsonResponse(data, safe=False)
Exemplo n.º 12
0
    def get(self, request):
        """
        Return a list of all contracts.
        """

        # Calculating total tender
        country_code = self.request.GET.get("country", None)
        buyer_id = self.request.GET.get("buyer")
        supplier_id = self.request.GET.get("supplier")

        filter_args = {}
        if country_code:
            country_code = str(country_code).upper()
            filter_args["country__country_code_alpha_2"] = country_code
        if buyer_id:
            filter_args = add_filter_args("buyer", buyer_id, filter_args)
        if supplier_id:
            filter_args = add_filter_args("supplier", supplier_id, filter_args)

        bar_chart = (Tender.objects.filter(
            **filter_args).values("procurement_procedure").annotate(
                usd=Sum("contract_value_usd"),
                local=Sum("contract_value_local")))

        total_country_usd_sum = 0
        total_country_local_sum = 0
        bar_chart_usd = []
        bar_chart_local = []

        for i in bar_chart:
            total_country_usd_sum += i["usd"] if i["usd"] else 0
            total_country_local_sum += i["local"] if i["local"] else 0
            bar_chart_usd.append({
                "method": i["procurement_procedure"],
                "value": i["usd"]
            })

            bar_chart_local.append({
                "method": i["procurement_procedure"],
                "value": i["local"]
            })

        line_chart = (Tender.objects.filter(**filter_args).annotate(
            month=TruncMonth("contract_date")).values("month").annotate(
                usd=Sum("contract_value_usd"),
                local=Sum("contract_value_local")).order_by("-month"))
        line_chart_local_list = [{
            "date": i["month"],
            "value": i["local"]
        } for i in line_chart]
        line_chart_list = [{
            "date": i["month"],
            "value": i["usd"]
        } for i in line_chart]

        result = {
            "usd": {
                "total": total_country_usd_sum,
                "line_chart": line_chart_list,
                "bar_chart": bar_chart_usd,
            },
            "local": {
                "total": total_country_local_sum,
                "line_chart": line_chart_local_list,
                "bar_chart": bar_chart_local,
            },
        }
        return JsonResponse(result)
 def test_add_filter_args_append_only(self):
     result = add_filter_args("foo", "bar", {}, append_only=True)
     self.assertEquals(result, {"foo": "bar"})
 def test_add_filter_args_notnull(self):
     result = add_filter_args("foo", "notnull", {})
     self.assertEquals(result, {"foo__isnull": False})
 def test_add_filter_args(self):
     result = add_filter_args("foo", "bar", {})
     self.assertEquals(result, {"foo__id": "bar"})