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

Commit

Permalink
CPA-280 POST edit tasks
Browse files Browse the repository at this point in the history
- [x] All the post edit task are done when edit in contract
  • Loading branch information
Suyoj Man Tamrakar committed May 4, 2021
1 parent f659594 commit 81053d3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
18 changes: 18 additions & 0 deletions country/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django import forms
from django.contrib import admin
from django.core import management
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.forms import TextInput
from django.utils.html import format_html

Expand All @@ -20,6 +23,7 @@
Tender,
Topic,
)
from .tasks import fix_contract_name_value


class EquityInline(admin.TabularInline):
Expand Down Expand Up @@ -308,10 +312,24 @@ class TenderAdmin(admin.ModelAdmin):
GoodsAndServicesInline,
]

def save_model(self, request, obj, form, change):
obj.from_admin_site = True # here we setting instance attribute which we check in `post_save`
super().save_model(request, obj, form, change)


admin.site.register(Tender, TenderAdmin)


@receiver(post_save, sender=Tender)
def save_model(sender, instance, **kwargs):
if getattr(instance, "from_admin_site", False):
tender_id = instance.id
tender_country = instance.country
fix_contract_name_value(tender_id, tender_country)
management.call_command("generate_country_contract_excel", tender_country)
management.call_command("generate_excel_summary")


@admin.register(RedFlag)
class RedFlagAdmin(admin.ModelAdmin):
list_display = ("title", "implemented")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Command(BaseCommand):
help = "Generate Country Contract Excel"

def add_arguments(self, parser):
parser.add_argument("--country", type=str)
parser.add_argument("country", type=str)

def handle(self, *args, **kwargs):
country = kwargs["country"]
Expand Down
31 changes: 14 additions & 17 deletions country/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,6 @@ def import_tender_from_batch_id(batch_id, country, currency):
ppu_including_vat=ppu_including_vat or None,
)
goods_services_obj.save()

print(tender_obj.id, goods_services_obj.id)

# Execute local currency to USD conversion celery tasks
conversion_date = contract_date
source_currency = country_obj.currency
Expand Down Expand Up @@ -723,12 +720,12 @@ def country_contract_excel(country):
"goods_services__goods_services_category__category_name",
)
.annotate(
contract_usd=Sum("goods_services__contract_value_usd"),
contract_local=Sum("goods_services__contract_value_local"),
award_local=Sum("goods_services__award_value_local"),
award_usd=Sum("goods_services__award_value_usd"),
tender_local=Sum("goods_services__tender_value_local"),
tender_usd=Sum("goods_services__tender_value_usd"),
contract_usd=Sum("contract_value_usd"),
contract_local=Sum("contract_value_local"),
award_local=Sum("award_value_local"),
award_usd=Sum("award_value_usd"),
tender_local=Sum("tender_value_local"),
tender_usd=Sum("tender_value_usd"),
)
)
if reports:
Expand Down Expand Up @@ -814,7 +811,7 @@ def country_contract_excel(country):
print("............")

else:
country_name = Country.objects.filter(country_code_alpha_2=country).first().name
country_name = Country.objects.filter(name=country).first().name
file_path = f"media/export/{country_name}_summary.xlsx"
if not os.path.exists(file_path):
Path(file_path).touch()
Expand Down Expand Up @@ -858,7 +855,7 @@ def country_contract_excel(country):

data = {}
reports = (
Tender.objects.filter(country__country_code_alpha_2=country)
Tender.objects.filter(country__name=country)
.values(
"id",
"contract_id",
Expand All @@ -876,12 +873,12 @@ def country_contract_excel(country):
"goods_services__goods_services_category__category_name",
)
.annotate(
contract_usd=Sum("goods_services__contract_value_usd"),
contract_local=Sum("goods_services__contract_value_local"),
award_local=Sum("goods_services__award_value_local"),
award_usd=Sum("goods_services__award_value_usd"),
tender_local=Sum("goods_services__tender_value_local"),
tender_usd=Sum("goods_services__tender_value_usd"),
contract_usd=Sum("contract_value_usd"),
contract_local=Sum("contract_value_local"),
award_local=Sum("award_value_local"),
award_usd=Sum("award_value_usd"),
tender_local=Sum("tender_value_local"),
tender_usd=Sum("tender_value_usd"),
)
)

Expand Down

0 comments on commit 81053d3

Please sign in to comment.