Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
CPA-295 Set Buyer / Supplier summary
Browse files Browse the repository at this point in the history
- [x] Create buyer and supplier summary commands/tasks like `summarize_buyer` and `summarize_supplier`
- [x] Command will take buyer id or supplier id as parameter
- [x] Add a `summary` JSON column in `country_buyer` and `country_supplier` tables
- [x] Calculate summary information of buyer and supplier like:
- [x] number of contracts `tender_count`
- [x] total `amount_usd`
- [x] total `amount_local`
- [x] number of unique suppliers `supplier_count` OR buyers `buyer_count`
- [x] number of Product categories `product_count`
- [x] total of contracts with red flag `red_flag_tender_count`
- [x] percentage of contracts with red flag `red_flag_tender_percentage`
- [x] Added required line to solve error in tender edit
  • Loading branch information
Suyoj Man Tamrakar committed May 6, 2021
1 parent c082d5c commit 8666a4f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 43 deletions.
8 changes: 4 additions & 4 deletions country/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ class Meta:
"buyer",
)

# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
# self.fields["buyer"].queryset = Buyer.objects.filter(tenders__country=self.instance.country)
# self.fields["supplier"].queryset = Supplier.objects.filter(tenders__country=self.instance.country)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["buyer"].queryset = Buyer.objects.filter(country=self.instance.country)
self.fields["supplier"].queryset = Supplier.objects.filter(country=self.instance.country)


class TenderAdmin(admin.ModelAdmin):
Expand Down
20 changes: 20 additions & 0 deletions country/management/commands/summarize_buyer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.core.management.base import BaseCommand

from country.models import Buyer, Country
from country.tasks import summarize_buyer


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("country", type=str)

def handle(self, *args, **kwargs):
country_alpha_code = kwargs["country"]
try:
country = Country.objects.get(country_code_alpha_2=country_alpha_code)
except Exception:
return self.stdout.write("Country alpha code doesnt exist")
buyers = Buyer.objects.filter(tenders__country=country)
for buyer in buyers:
self.stdout.write("Created tasks for buyer_id" + str(buyer.id))
summarize_buyer.apply_async(args=(buyer.id,), queue="covid19")
20 changes: 20 additions & 0 deletions country/management/commands/summarize_supplier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.core.management.base import BaseCommand

from country.models import Country, Supplier
from country.tasks import summarize_supplier


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("country", type=str)

def handle(self, *args, **kwargs):
country_alpha_code = kwargs["country"]
try:
country = Country.objects.get(country_code_alpha_2=country_alpha_code)
except Exception:
return self.stdout.write("Country alpha code doesnt exist")
suppliers = Supplier.objects.filter(tenders__country=country)
for supplier in suppliers:
self.stdout.write("Created tasks for supplier_id" + str(supplier.id))
summarize_supplier.apply_async(args=(supplier.id,), queue="covid19")
38 changes: 0 additions & 38 deletions country/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,42 +96,6 @@ class CurrencyConversionCache(models.Model):
conversion_rate = models.FloatField(verbose_name=_("Conversion rate"), null=True)


class SupplierManager(models.Manager):
def get_queryset(self):
return (
super()
.get_queryset()
.annotate(
# amount_local=Sum("tenders__goods_services__contract_value_local"),
# amount_usd=Sum("tenders__goods_services__contract_value_usd"),
# country_name=F("tenders__country__name"),
# red_flag_count=Count("tenders__red_flag", distinct=True),
# product_category_count=Count("tenders__goods_services__goods_services_category", distinct=True),
# tender_count=Count("tenders__id", distinct=True),
# buyer_count=Count("tenders__buyer_id", filter=Q(tenders__buyer_id__isnull=False), distinct=True),
)
)


class BuyerManager(models.Manager):
def get_queryset(self):
return (
super()
.get_queryset()
.annotate(
# amount_usd=Sum("tenders__goods_services__contract_value_usd"),
# amount_local=Sum("tenders__goods_services__contract_value_local"),
# country_name=F("tenders__country__name"),
# red_flag_count=Count("tenders__red_flag", distinct=True),
# # product_category_count=Count("tenders__goods_services__goods_services_category", distinct=True),
# tender_count=Count("tenders__id", distinct=True),
# supplier_count=Count(
# "tenders__supplier_id", filter=Q(tenders__supplier_id__isnull=False), distinct=True
# ),
)
)


class Supplier(models.Model):
supplier_id = models.CharField(verbose_name=_("Supplier ID"), max_length=50, null=True)
supplier_name = models.CharField(
Expand All @@ -140,7 +104,6 @@ class Supplier(models.Model):
supplier_address = models.CharField(verbose_name=_("Supplier address"), max_length=250, null=True, blank=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE, related_name="suppliers", null=True)
summary = models.JSONField(null=True)
objects = SupplierManager()

def __str__(self):
return f"{self.supplier_id} - {self.supplier_name}"
Expand All @@ -152,7 +115,6 @@ class Buyer(models.Model):
buyer_address = models.CharField(verbose_name=_("Buyer address"), max_length=250, null=True, blank=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE, related_name="buyers", null=True)
summary = models.JSONField(null=True)
objects = BuyerManager()

def __str__(self):
return f"{self.buyer_id} - {self.buyer_name}"
Expand Down
77 changes: 76 additions & 1 deletion country/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import xlsxwriter
from celery import Celery
from django.conf import settings
from django.db.models import Sum
from django.db.models import Count, Q, Sum
from requests.exceptions import Timeout

from content.models import DataImport
Expand Down Expand Up @@ -1020,3 +1020,78 @@ def fill_contract_values(tender_id):
tender_instance.award_value_usd = award_value_usd
tender_instance.save()
print("Done for tender id : " + str(tender_id))


@app.task(name="summarize_buyer")
def summarize_buyer(buyer_id):
buyer_instance = (
Buyer.objects.filter(id=buyer_id)
.values("tenders__country__country_code_alpha_2", "tenders__country__name")
.annotate(
amount_usd=Sum("tenders__contract_value_usd"),
amount_local=Sum("tenders__contract_value_local"),
tender_count=Count("tenders__id", distinct=True),
supplier_count=Count("tenders__supplier_id", filter=Q(tenders__supplier_id__isnull=False), distinct=True),
product_count=Count("tenders__goods_services__goods_services_category", distinct=True),
red_flag_count=Count("tenders__red_flag", distinct=True),
)
.first()
)
summary = {
"amount_local": buyer_instance["amount_local"],
"amount_usd": buyer_instance["amount_usd"],
"tender_count": buyer_instance["tender_count"],
"supplier_count": buyer_instance["supplier_count"],
"product_count": buyer_instance["product_count"],
"country_code": buyer_instance["tenders__country__country_code_alpha_2"],
"country_name": buyer_instance["tenders__country__name"],
"red_flag_tender_count": buyer_instance["red_flag_count"],
"red_flag_tender_percentage": float(buyer_instance["red_flag_count"] / buyer_instance["tender_count"]),
}
try:
country = Country.objects.get(country_code_alpha_2=buyer_instance["tenders__country__country_code_alpha_2"])
buyer = Buyer.objects.get(id=buyer_id)
buyer.country = country
buyer.summary = summary
buyer.save()
except Exception:
return "Buyer id doesnt exists!"
return "Completed"


@app.task(name="summarize_supplier")
def summarize_supplier(supplier_id):
supplier_instance = (
Supplier.objects.filter(id=supplier_id)
.values("tenders__country__country_code_alpha_2", "tenders__country__name")
.annotate(
amount_usd=Sum("tenders__contract_value_usd"),
amount_local=Sum("tenders__contract_value_local"),
tender_count=Count("tenders__id", distinct=True),
buyer_count=Count("tenders__buyer_id", filter=Q(tenders__buyer_id__isnull=False), distinct=True),
product_count=Count("tenders__goods_services__goods_services_category", distinct=True),
red_flag_count=Count("tenders__red_flag", distinct=True),
)
.first()
)
summary = {
"amount_local": supplier_instance["amount_local"],
"amount_usd": supplier_instance["amount_usd"],
"tender_count": supplier_instance["tender_count"],
"buyer_count": supplier_instance["buyer_count"],
"product_count": supplier_instance["product_count"],
"country_code": supplier_instance["tenders__country__country_code_alpha_2"],
"country_name": supplier_instance["tenders__country__name"],
"red_flag_tender_count": supplier_instance["red_flag_count"],
"red_flag_tender_percentage": float(supplier_instance["red_flag_count"] / supplier_instance["tender_count"]),
}

try:
country = Country.objects.get(country_code_alpha_2=supplier_instance["tenders__country__country_code_alpha_2"])
supplier = Supplier.objects.get(id=supplier_id)
supplier.country = country
supplier.summary = summary
supplier.save()
except Exception as e:
return e
return "Completed"

0 comments on commit 8666a4f

Please sign in to comment.