From a3d13b11d148738c6203c986461832d9d154577c Mon Sep 17 00:00:00 2001 From: Ernst Odolphi Date: Wed, 24 Aug 2022 12:13:04 +0200 Subject: [PATCH 1/2] Add script to rank tne activities on when they reached a specific target --- scripts/tne_campaign_ranking.py | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 scripts/tne_campaign_ranking.py diff --git a/scripts/tne_campaign_ranking.py b/scripts/tne_campaign_ranking.py new file mode 100644 index 0000000000..091c49bec8 --- /dev/null +++ b/scripts/tne_campaign_ranking.py @@ -0,0 +1,63 @@ +from datetime import date +import xlsxwriter + +from bluebottle.clients.models import Client +from bluebottle.clients.utils import LocalTenant + +from bluebottle.funding.models import Funding, Donor +from bluebottle.geo.models import Location + + +OFFICE_NAME = 'Segou' +TARGET = 328000 +DEADLINE = date(2022, 8, 13) + +def run(*args): + tne = Client.objects.get(client_name='nexteconomy') + + with LocalTenant(tne, clear_tenant=True): + result = [] + + location = Location.objects.get(name=OFFICE_NAME) + + campaigns = Funding.objects.filter( + initiative__location__name=OFFICE_NAME, + deadline__date=DEADLINE, + status__in=('succeeded', 'partially_funded') + ) + + for campaign in campaigns: + donors = campaign.contributors.instance_of( + Donor + ).filter( + status='succeeded' + ).order_by( + 'created' + ) + + total = 0 + for donor in donors: + total += donor.amount.amount + + if total > TARGET: + result.append({ + 'id': campaign.id, + 'title': campaign.title, + 'status': campaign.status, + 'target reached': str(donor.created), + }) + break + + + workbook = xlsxwriter.Workbook(f'TNE-{location.name}-{DEADLINE}.xlsx', {'remove_timezone': True}) + worksheet = workbook.add_worksheet() + + worksheet.write_row(0, 0, result[0].keys()) + + for (index, row) in enumerate(result): + worksheet.write_row(index + 1, 0, row.values()) + + workbook.close() + + + From 3ce6f25a08870674ee7593385332e161def4e2df Mon Sep 17 00:00:00 2001 From: Ernst Odolphi Date: Tue, 30 Aug 2022 09:43:20 +0200 Subject: [PATCH 2/2] Update script a little. Filter on multiple dates --- scripts/tne_campaign_ranking.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/scripts/tne_campaign_ranking.py b/scripts/tne_campaign_ranking.py index 091c49bec8..8b93da3434 100644 --- a/scripts/tne_campaign_ranking.py +++ b/scripts/tne_campaign_ranking.py @@ -8,9 +8,10 @@ from bluebottle.geo.models import Location -OFFICE_NAME = 'Segou' -TARGET = 328000 -DEADLINE = date(2022, 8, 13) +OFFICE_NAME = 'Mogadishu' +TARGET = 500 +DEADLINES = [date(2022, 8, 20), date(2022, 8, 21)] + def run(*args): tne = Client.objects.get(client_name='nexteconomy') @@ -22,9 +23,13 @@ def run(*args): campaigns = Funding.objects.filter( initiative__location__name=OFFICE_NAME, - deadline__date=DEADLINE, + deadline__date__in=DEADLINES, status__in=('succeeded', 'partially_funded') ) + print(len(campaigns)) + + for activity in campaigns: + print(activity.title, activity.amount_raised, activity.status) for campaign in campaigns: donors = campaign.contributors.instance_of( @@ -38,18 +43,17 @@ def run(*args): total = 0 for donor in donors: total += donor.amount.amount - - if total > TARGET: + + if total >= TARGET: result.append({ - 'id': campaign.id, - 'title': campaign.title, - 'status': campaign.status, - 'target reached': str(donor.created), + 'id': campaign.id, + 'title': campaign.title, + 'status': campaign.status, + 'target reached': str(donor.created), }) break - - workbook = xlsxwriter.Workbook(f'TNE-{location.name}-{DEADLINE}.xlsx', {'remove_timezone': True}) + workbook = xlsxwriter.Workbook(f'TNE-{location.name}-{DEADLINES[0]}.xlsx', {'remove_timezone': True}) worksheet = workbook.add_worksheet() worksheet.write_row(0, 0, result[0].keys()) @@ -58,6 +62,3 @@ def run(*args): worksheet.write_row(index + 1, 0, row.values()) workbook.close() - - -