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

Commit

Permalink
CPA-324 Refactoring management commands
Browse files Browse the repository at this point in the history
- [x] evaluate_contract_equity
- [x] evaluate_contract_red_flag
- [x] export_country_contracts
- [x] export_summary_report
- [x] fetch_covid_active_cases
- [x] import_tender_from_id
- [x] process_currency_conversion
- [x] summarize_country_buyer
- [x] summarize_country_supplier

Split into files
- [x] Tasks
- [x] Models
- [x] Serializers
  • Loading branch information
Kushal Raj Shrestha committed Jul 18, 2021
1 parent 7e3cd29 commit de6f9ee
Show file tree
Hide file tree
Showing 68 changed files with 2,354 additions and 2,018 deletions.
4 changes: 3 additions & 1 deletion content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.search import index

from country.models import Country, Language, Topic
from country.models.country import Country
from country.models.language import Language
from country.models.topic import Topic

from .validators import validate_file_extension

Expand Down
23 changes: 23 additions & 0 deletions country/management/commands/evaluate_contract_equity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.core.management.base import BaseCommand

from country.models import Country
from country.tasks import evaluate_contract_equity


class Command(BaseCommand):
help = "Evaluate country contracts for equity indicators"

def add_arguments(self, parser):
parser.add_argument("country_code", type=str, help="Country code")

def handle(self, *args, **kwargs):
country_code = kwargs["country_code"].upper()

try:
Country.objects.get(country_code_alpha_2=country_code)
except Exception:
return self.stdout.write("Invalid country code provided")

evaluate_contract_equity.apply_async(args=(country_code,), queue="covid19")

return "Done"
15 changes: 9 additions & 6 deletions country/management/commands/evaluate_contract_red_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@


class Command(BaseCommand):
help = "Compute red flag for specific country"
help = "Evaluate country contracts for red flags"

def add_arguments(self, parser):
parser.add_argument("country", type=str)
parser.add_argument("country_code", type=str, help="Country code")

def handle(self, *args, **kwargs):
country_alpha_code = kwargs["country"].upper()
country_code = kwargs["country_code"].upper()

try:
Country.objects.get(country_code_alpha_2=country_alpha_code)
Country.objects.get(country_code_alpha_2=country_code)
except Exception:
return self.stdout.write("Country alpha code doesnt exist")
return self.stdout.write("Invalid country code provided")

evaluate_contract_red_flag.apply_async(
args=(country_alpha_code,),
args=(country_code,),
queue="covid19",
)

return "Done"
21 changes: 21 additions & 0 deletions country/management/commands/export_country_contracts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.core.management.base import BaseCommand

from country.models import Country
from country.tasks import country_contract_excel


class Command(BaseCommand):
help = "Export country contracts into excel file."

def add_arguments(self, parser):
parser.add_argument("country_code", type=str, help="Country code")

def handle(self, *args, **kwargs):
country_code = kwargs["country_code"].upper()

try:
Country.objects.get(country_code_alpha_2=country_code)
except Exception:
return self.stdout.write("Invalid country code provided")

country_contract_excel.apply_async(args=(country_code,), queue="covid19")
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@


class Command(BaseCommand):
help = "Generate Excel Summary"
help = "Export overall summary report"

def add_arguments(self, parser):
parser.add_argument("country", type=str, nargs="?", default=False)
parser.add_argument("country_code", type=str, nargs="?", default=False, help="Country code")

def handle(self, *args, **kwargs):
self.stdout.write("Exporting")
country_alpha_code = kwargs["country"] or None
if country_alpha_code:
country_alpha_code = country_alpha_code.upper()
country = Country.objects.filter(country_code_alpha_2=country_alpha_code).first()
country_code = kwargs["country_code"] or None

if country_code:
country_code = country_code.upper()
country = Country.objects.filter(country_code_alpha_2=country_code).first()
data = get_statistics(country)
OverallSummary.objects.filter(country=country).delete()
OverallSummary.objects.create(statistic=data, country=country)

return "Done"
os.makedirs(os.path.join("media", "export"), exist_ok=True)
workbook = xlsxwriter.Workbook("media/export/Overall Country Summary.xlsx")
Expand Down Expand Up @@ -56,11 +58,13 @@ def handle(self, *args, **kwargs):
"Percentage of GDP to healthcare budget",
"Country Data Download",
]

for item in column_names:
worksheet.write(row, column, item)
column += 1

countries = Country.objects.all().exclude(country_code_alpha_2="gl")

for country in countries:
columns = 0
row += 1
Expand All @@ -75,8 +79,10 @@ def handle(self, *args, **kwargs):
OverallSummary.objects.create(statistic=data, country=country)
except Exception as e:
self.stderr.write(str(e))

workbook.close()
return "Finished"

return "Done"


def get_statistics(country):
Expand Down Expand Up @@ -116,6 +122,7 @@ def get_statistics(country):
total_value_of_equity_contracts=Sum("contract_value_usd", exclude=Q(equity_category=None)),
)
)

if report["time_span_max"] and report["time_span_min"] is not None:
timespan = report["time_span_max"] - report["time_span_min"]
else:
Expand Down Expand Up @@ -159,4 +166,5 @@ def get_statistics(country):
data["country_data_download"] = (
"https://" + socket.gethostbyname(socket.gethostname()) + "/media/export/" + country.name + "_summary.xlsx"
)

return data
7 changes: 5 additions & 2 deletions country/management/commands/fetch_covid_active_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class Command(BaseCommand):
help = "Import COVID statistics from covid-api.com"
help = "Fetch Covid-19 case statistics from covid-api.com"

def handle(self, *args, **kwargs):
today = datetime.date.today()
Expand All @@ -30,8 +30,8 @@ def handle(self, *args, **kwargs):
.annotate(date=ArrayAgg("covid_monthly_active_cases__covid_data_date"))
.values_list("country_code", "date")
)

countries = Country.objects.all().exclude(country_code_alpha_2="gl").order_by("country_code")

for country in countries:
country_code = country.country_code
country_dates = set(dates_with_data[country_code])
Expand All @@ -46,17 +46,20 @@ def handle(self, *args, **kwargs):
url = f"https://covid-api.com/api/reports?iso={country_code}&date={date}"

response = requests.get(url)

if not response.ok:
self.stderr.write(f"Fetching {url}... {response.status_code}")
continue

data = response.json()["data"]

if not data:
self.stdout.write(f"Fetching {url}... NO DATA")
continue

active_cases_count = 0
death_count = 0

for item in data:
active_cases_count += item["active"]
death_count += item["deaths"]
Expand Down
14 changes: 0 additions & 14 deletions country/management/commands/generate_country_contract_excel.py

This file was deleted.

2 changes: 2 additions & 0 deletions country/management/commands/import_tender_from_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ def handle(self, *args, **kwargs):
self.stdout.write("Created task: import_tender_from_batch_id")
except Exception as e:
self.stderr.write(e)

return "Done"
9 changes: 6 additions & 3 deletions country/management/commands/process_currency_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@


class Command(BaseCommand):
help = "Convert contract local amount values into USD"

def handle(self, *args, **kwargs):
self.stdout.write("Processing currency conversion")
unconverted_tender = GoodsServices.objects.filter(
Q(tender_value_usd__isnull=True) | Q(award_value_usd__isnull=True) | Q(contract_value_usd__isnull=True)
)

for tender in unconverted_tender:
tender_value_local = tender.tender_value_local
tender_date = tender.contract.contract_date
currency = tender.country.currency
id = tender.id
tender_date = tender.contract.contract_date
tender_value_local = tender.tender_value_local
contract_value_local = tender.contract_value_local
award_value_local = tender.award_value_local
currency = tender.country.currency
process_currency_conversion.apply_async(
args=(tender_value_local, award_value_local, contract_value_local, tender_date, currency, id),
queue="covid19",
Expand Down
23 changes: 0 additions & 23 deletions country/management/commands/save_database_equity.py

This file was deleted.

14 changes: 10 additions & 4 deletions country/management/commands/summarize_country_buyer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@


class Command(BaseCommand):
help = "Summarize country buyer's contract information"

def add_arguments(self, parser):
parser.add_argument("country", type=str)
parser.add_argument("country_code", type=str, help="Country code")

def handle(self, *args, **kwargs):
country_alpha_code = kwargs["country"].upper()
country_code = kwargs["country_code"].upper()
try:
country = Country.objects.get(country_code_alpha_2=country_alpha_code)
country = Country.objects.get(country_code_alpha_2=country_code)
except Exception:
return self.stdout.write("Country alpha code doesnt exist")
return self.stdout.write("Invalid country code provided")

buyers = Buyer.objects.filter(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")

return "Done"
14 changes: 10 additions & 4 deletions country/management/commands/summarize_country_supplier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@


class Command(BaseCommand):
help = "Summarize country supplier's contract information"

def add_arguments(self, parser):
parser.add_argument("country", type=str)
parser.add_argument("country_code", type=str, help="Country code")

def handle(self, *args, **kwargs):
country_alpha_code = kwargs["country"].upper()
country_code = kwargs["country_code"].upper()
try:
country = Country.objects.get(country_code_alpha_2=country_alpha_code)
country = Country.objects.get(country_code_alpha_2=country_code)
except Exception:
return self.stdout.write("Country alpha code doesnt exist")
return self.stdout.write("Invalid country code provided")

suppliers = Supplier.objects.filter(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")

return "Done"
Loading

0 comments on commit de6f9ee

Please sign in to comment.