Skip to content

Commit

Permalink
create csv file for all funders
Browse files Browse the repository at this point in the history
  • Loading branch information
PumpkingWok committed Sep 3, 2019
1 parent bc2c8db commit 2eeaddd
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions app/marketing/management/commands/send_tax_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
'''
Copyright (C) 2019 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import time
import warnings
import csv
import os

from django.core.management.base import BaseCommand

from dashboard.models import Bounty, Profile, Tip

# Constants
DATETIME = 'datetime'
COUNTER_PARTY_NAME = 'counter_party_name'
COUNTER_PARTY_LOCATION = 'counter_party_location'
TOKEN_AMOUNT = 'token_amount'
TOKEN_NAME = 'token_name'
USD_VALUE = 'usd_value'
WORKER_TYPE = 'worker_type'
COUNTRY_CODE = 'country_code'

WEB3_NETWORK = 'rinkeby'
BOUNTY = 'bounty'
TIP = 'tip'
GRANT = 'grant'

FUNDERS_DIR = 'funders_csv'
TAX_YEAR = '2019'

warnings.filterwarnings("ignore", category=DeprecationWarning)


def create_csv_record(wt_obj, worker_type):
record = {DATETIME: wt_obj.web3_created}

if worker_type == BOUNTY:
counter_party_name = []
counter_party_location = []
for fulfiller in wt_obj.fulfillments.filter(accepted=True):
counter_party_name.append(fulfiller.fulfiller_github_username)
profiles = Profile.objects.filter(handle__iexact=fulfiller.fulfiller_github_username)
if profiles.exists():
profile = profiles.first()
if profile.location:
counter_party_location.append(profile.location)
elif profile.locations:
counter_party_location.append(profile.locations[-1][COUNTRY_CODE])
else:
counter_party_location.append('No location')
# No profile found on DB
else:
counter_party_location.append('No profile')
record[USD_VALUE] = wt_obj._val_usd_db

elif worker_type == TIP:
counter_party_name = wt_obj.username
counter_party_location = 'No location'
profiles = Profile.objects.filter(handle__iexact=wt_obj.username)
if profiles.exists():
profile = profiles.first()
if profile.location:
counter_party_location = profile.location
elif profile.locations:
counter_party_location = profiles.location[-1][COUNTRY_CODE]
record[USD_VALUE] = wt_obj._value_in_usdt_now

elif worker_type == GRANT:
# TODO
print('grant')

record[COUNTER_PARTY_NAME] = counter_party_name
record[COUNTER_PARTY_LOCATION] = counter_party_location
record[TOKEN_AMOUNT] = wt_obj.value_in_token
record[TOKEN_NAME] = wt_obj.token_name
record[USD_VALUE] = wt_obj._val_usd_db
record[WORKER_TYPE] = worker_type

return record


class Command(BaseCommand):
help = 'the tax report for last year'

def handle(self, *args, **options):
profiles = Profile.objects.all()
us_workers = {}
funders = {}

for p in profiles:
for b in p.get_sent_bounties:
if not b._val_usd_db or b.status != 'done' or b.is_open is True or b.network != WEB3_NETWORK or p.username != b.bounty_owner_github_username or b.fulfillment_accepted_on.date().year != TAX_YEAR:
continue
else:
print(b.fulfillment_accepted_on.date().year)
if p.username not in funders.keys():
funders[p.username] = []
funders[p.username].append(create_csv_record(b, BOUNTY))
for t in p.get_sent_tips:
if not t.value_in_usdt_now or t.network != WEB3_NETWORK or p.username != t.from_username:
continue
else:
if p.username not in funders.keys():
funders[p.username] = []
funders[p.username].append(create_csv_record(t, TIP))
for g in p.get_my_grants:
if g.network != WEB3_NETWORK:
continue
else:
if p.username not in funders.keys():
funders[p.username] = []
funders[p.username].append(create_csv_record(g, GRANT))

csv_columns = [DATETIME, COUNTER_PARTY_NAME, COUNTER_PARTY_LOCATION, TOKEN_AMOUNT, TOKEN_NAME, USD_VALUE, WORKER_TYPE]
if not os.path.isdir(os.path.join(os.getcwd(), FUNDERS_DIR)):
os.makedirs(os.path.join(os.getcwd(), FUNDERS_DIR))
for funder in funders:
try:
with open(os.path.join(os.getcwd(), FUNDERS_DIR, funder + '.csv'), 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for row in funders[funder]:
writer.writerow(row)
except IOError:
print("I/O error")

0 comments on commit 2eeaddd

Please sign in to comment.