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

Commit

Permalink
[x] Removed annotate and red_flag details on BUyer Supplier APi
Browse files Browse the repository at this point in the history
[x] Supplier issue solved
[x] Added files in requirement.txt file
[x] Commented out the migrations and data import issue solved
  • Loading branch information
Suyoj Man Tamrakar authored and KushalRaj committed Apr 1, 2021
1 parent c354eca commit 30e03b2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 62 deletions.
5 changes: 2 additions & 3 deletions country/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ def validate(self):
)
else:
return format_html(
f"""<a class="button" onClick="this.disabled = true;" href="/data_validate?
data_import_id={data_import_id}">
Validate</a>&nbsp;"""
f"""<a class="button" onClick="this.disabled = true;"
href="/data_validate?data_import_id={data_import_id}">Validate</a>&nbsp;"""
)

validate.short_description = "Validate"
Expand Down
33 changes: 16 additions & 17 deletions country/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.contrib.postgres.fields import ArrayField
from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
from django.db import models
from django.db.models import Count, F, Q, Sum
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -158,13 +157,13 @@ def get_queryset(self):
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),
# 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),
)
)

Expand All @@ -175,15 +174,15 @@ def get_queryset(self):
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
),
# 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
# ),
)
)

Expand Down
86 changes: 47 additions & 39 deletions country/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db.models import Sum
from rest_framework import serializers

from rest_framework_serializer_extensions.serializers import SerializerExtensionsMixin
from .models import Buyer, Country, Language, OverallSummary, RedFlag, Supplier, Tender


Expand All @@ -21,7 +21,7 @@ def to_internal_value(self, data):
self.fail("invalid_choice", input=data)


class CountrySerializer(serializers.HyperlinkedModelSerializer):
class CountrySerializer(serializers.HyperlinkedModelSerializer, SerializerExtensionsMixin):
id = serializers.IntegerField(read_only=True)
continent = ChoiceField(choices=Country.CONTINENT_CHOICES)

Expand All @@ -40,7 +40,7 @@ class Meta:
)


class LanguageSerializer(serializers.ModelSerializer):
class LanguageSerializer(serializers.ModelSerializer, SerializerExtensionsMixin):
class Meta:
model = Language
fields = (
Expand All @@ -50,11 +50,11 @@ class Meta:
)


class SupplierSerializer(serializers.ModelSerializer):
class SupplierSerializer(serializers.ModelSerializer, SerializerExtensionsMixin):
amount_local = serializers.SerializerMethodField()
amount_usd = serializers.SerializerMethodField()
red_flag_tender_count = serializers.SerializerMethodField()
red_flag_tender_percentage = serializers.SerializerMethodField()
# red_flag_tender_count = serializers.SerializerMethodField()
# red_flag_tender_percentage = serializers.SerializerMethodField()
country_code = serializers.SerializerMethodField()
country_name = serializers.SerializerMethodField()
product_category_count = serializers.SerializerMethodField()
Expand All @@ -67,7 +67,7 @@ class Meta:
fields = (
"amount_local",
"amount_usd",
"red_flag_tender_count",
# "red_flag_tender_count",
"supplier_id",
"supplier_name",
"supplier_address",
Expand All @@ -76,7 +76,7 @@ class Meta:
"product_category_count",
"buyer_count",
"tender_count",
"red_flag_tender_percentage",
# "red_flag_tender_percentage",
)

def get_amount_usd(self, obj):
Expand All @@ -97,16 +97,16 @@ def get_amount_local(self, obj):
except:
return obj.tenders.all().aggregate(sum_local=Sum("goods_services__contract_value_local"))["sum_local"]

def get_red_flag_tender_count(self, obj):
return obj.red_flag_count

def get_red_flag_tender_percentage(self, obj):
red_flags = obj.red_flag_count
total = obj.tender_count
try:
return red_flags / total
except:
return 0
# def get_red_flag_tender_count(self, obj):
# return obj.red_flag_count
#
# def get_red_flag_tender_percentage(self, obj):
# red_flags = obj.red_flag_count
# total = obj.tender_count
# try:
# return red_flags / total
# except:
# return 0

def get_country_code(self, obj):
tender_obj = obj.tenders.first()
Expand Down Expand Up @@ -158,11 +158,11 @@ def get_product_category_count(self, obj):
return product_category_count


class BuyerSerializer(serializers.ModelSerializer):
class BuyerSerializer(serializers.ModelSerializer, SerializerExtensionsMixin):
amount_local = serializers.SerializerMethodField()
amount_usd = serializers.SerializerMethodField()
red_flag_tender_count = serializers.SerializerMethodField()
red_flag_tender_percentage = serializers.SerializerMethodField()
# red_flag_tender_count = serializers.SerializerMethodField()
# red_flag_tender_percentage = serializers.SerializerMethodField()
country_code = serializers.SerializerMethodField()
country_name = serializers.SerializerMethodField()
product_category_count = serializers.SerializerMethodField()
Expand All @@ -175,7 +175,7 @@ class Meta:
fields = (
"amount_local",
"amount_usd",
"red_flag_tender_count",
# "red_flag_tender_count",
"buyer_id",
"buyer_name",
"buyer_address",
Expand All @@ -184,7 +184,7 @@ class Meta:
"product_category_count",
"supplier_count",
"tender_count",
"red_flag_tender_percentage",
# "red_flag_tender_percentage",
)

def get_amount_usd(self, obj):
Expand All @@ -194,7 +194,11 @@ def get_amount_usd(self, obj):
else:
return None
except:
return obj.tenders.all().aggregate(sum_usd=Sum("goods_services__contract_value_usd"))["sum_usd"]
return (
obj.tenders.select_related("goods_services")
.all()
.aggregate(sum_usd=Sum("goods_services__contract_value_usd"))["sum_usd"]
)

def get_amount_local(self, obj):
try:
Expand All @@ -203,18 +207,22 @@ def get_amount_local(self, obj):
else:
return None
except:
return obj.tenders.all().aggregate(sum_local=Sum("goods_services__contract_value_local"))["sum_local"]

def get_red_flag_tender_count(self, obj):
return obj.red_flag_count

def get_red_flag_tender_percentage(self, obj):
red_flags = obj.red_flag_count
total = obj.tender_count
try:
return red_flags / total
except:
return 0
return (
obj.tenders.select_related("goods_services")
.all()
.aggregate(sum_local=Sum("goods_services__contract_value_local"))["sum_local"]
)

# def get_red_flag_tender_count(self, obj):
# return obj.red_flag_count
#
# def get_red_flag_tender_percentage(self, obj):
# red_flags = obj.red_flag_count
# total = obj.tender_count
# try:
# return red_flags / total
# except:
# return 0

def get_country_code(self, obj):
tender_obj = obj.tenders.first()
Expand Down Expand Up @@ -266,13 +274,13 @@ def get_buyer_id(self, obj):
return obj.id


class RedFlagSerializer(serializers.ModelSerializer):
class RedFlagSerializer(serializers.ModelSerializer, SerializerExtensionsMixin):
class Meta:
model = RedFlag
fields = "__all__"


class TenderSerializer(serializers.ModelSerializer):
class TenderSerializer(serializers.ModelSerializer, SerializerExtensionsMixin):
country_name = serializers.CharField(source="country.name", read_only=True)
country_alpha_code = serializers.CharField(source="country.country_code_alpha_2", read_only=True)
contract_currency_local = serializers.CharField(source="country.currency", read_only=True)
Expand Down Expand Up @@ -435,7 +443,7 @@ def get_goods_services(self, obj):
return list(a)


class OverallStatSummarySerializer(serializers.ModelSerializer):
class OverallStatSummarySerializer(serializers.ModelSerializer, SerializerExtensionsMixin):
country = CountrySerializer(many=False, read_only=True)

class Meta:
Expand Down
8 changes: 5 additions & 3 deletions country/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def get_queryset(self):
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.annotate(**annotate_args).filter(**filter_args).exclude(**exclude_args)
return Tender.objects.prefetch_related('buyer', 'supplier', 'country').annotate(**annotate_args).filter(
**filter_args).exclude(**exclude_args)


class BuyerView(viewsets.ModelViewSet):
Expand Down Expand Up @@ -160,7 +161,7 @@ def get_queryset(self):
if country:
filter_args["tenders__country__country_code_alpha_2"] = country
if buyer_name:
filter_args["buyer_name__icontains"] = buyer_name
filter_args["buyer_name__contains"] = buyer_name
if product_id:
filter_args["tenders__goods_services__goods_services_category"] = product_id
if contract_value_usd and value_comparison:
Expand All @@ -170,7 +171,8 @@ def get_queryset(self):
elif value_comparison == "lt":
annotate_args["sum"] = Sum("tenders__goods_services__contract_value_usd")
filter_args["sum__lte"] = contract_value_usd
return Buyer.objects.annotate(**annotate_args).filter(**filter_args).distinct()
return Buyer.objects.prefetch_related('tenders').annotate(**annotate_args).filter(
**filter_args).distinct()


class SupplierView(viewsets.ModelViewSet):
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ requests
sentry_sdk
xlsxwriter
wagtail
djangorestframework-serializer-extensions
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ xlwt==1.3.0
# via tablib
zipp==3.4.0
# via importlib-metadata
djangorestframework-serializer-extensions==2.0.1

# The following packages are considered to be unsafe in a requirements file:
# setuptools

0 comments on commit 30e03b2

Please sign in to comment.