diff --git a/content/migrations/0045_auto_20210331_1526.py b/content/migrations/0045_auto_20210331_1526.py
index 2b5c9cb..0a85246 100644
--- a/content/migrations/0045_auto_20210331_1526.py
+++ b/content/migrations/0045_auto_20210331_1526.py
@@ -35,14 +35,14 @@ class Migration(migrations.Migration):
name='staticpage',
options={'verbose_name': 'Static Page', 'verbose_name_plural': 'Static Pages'},
),
- migrations.AddField(
- model_name='dataimport',
- name='validated',
- field=models.BooleanField(blank=True, default=False),
- ),
- migrations.AlterField(
- model_name='dataimport',
- name='import_file',
- field=models.FileField(null=True, upload_to='documents', validators=[content.validators.validate_file_extension]),
- ),
+ # migrations.AddField(
+ # model_name='dataimport',
+ # name='validated',
+ # field=models.BooleanField(blank=True, default=False),
+ # ),
+ # migrations.AlterField(
+ # model_name='dataimport',
+ # name='import_file',
+ # field=models.FileField(null=True, upload_to='documents', validators=[content.validators.validate_file_extension]),
+ # ),
]
diff --git a/country/admin.py b/country/admin.py
index 61a5b46..ee46a96 100644
--- a/country/admin.py
+++ b/country/admin.py
@@ -96,9 +96,8 @@ def validate(self):
)
else:
return format_html(
- f"""
- Validate """
+ f"""Validate """
)
validate.short_description = "Validate"
diff --git a/country/models.py b/country/models.py
index 1001f05..0f28c4d 100644
--- a/country/models.py
+++ b/country/models.py
@@ -158,13 +158,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),
)
)
@@ -175,15 +175,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
+ # ),
)
)
diff --git a/country/serializers.py b/country/serializers.py
index cfa26bb..be5b660 100644
--- a/country/serializers.py
+++ b/country/serializers.py
@@ -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
@@ -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)
@@ -40,7 +40,7 @@ class Meta:
)
-class LanguageSerializer(serializers.ModelSerializer):
+class LanguageSerializer(serializers.ModelSerializer,SerializerExtensionsMixin):
class Meta:
model = Language
fields = (
@@ -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()
@@ -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",
@@ -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):
@@ -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()
@@ -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()
@@ -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",
@@ -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):
@@ -194,7 +194,7 @@ 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:
@@ -203,18 +203,18 @@ 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()
@@ -266,13 +266,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)
@@ -435,7 +435,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:
diff --git a/country/views.py b/country/views.py
index a1e04b6..27a080f 100644
--- a/country/views.py
+++ b/country/views.py
@@ -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):
@@ -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:
@@ -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):
diff --git a/requirements.in b/requirements.in
index 07014a8..b700933 100644
--- a/requirements.in
+++ b/requirements.in
@@ -15,3 +15,4 @@ requests
sentry_sdk
xlsxwriter
wagtail
+djangorestframework-serializer-extensions
diff --git a/requirements.txt b/requirements.txt
index 03b6f39..c646da4 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -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