Skip to content

Commit

Permalink
Control transaction type vocabulary
Browse files Browse the repository at this point in the history
  • Loading branch information
hancush committed May 10, 2024
1 parent 32b45f0 commit 27a86ba
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
46 changes: 39 additions & 7 deletions camp_fin/management/commands/aggregate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,38 @@
class Command(BaseCommand):
help = "Import New Mexico Campaign Finance data"

def handle(self, *args, **options):
def add_arguments(self, parser):
parser.add_argument(
"--recreate-views",
dest="recreate_views",
action="store_true",
help="Drop and recreate materialized views. Helpful when the underlying query changes.",
)

self.makeLoanBalanceView()
self.makeTransactionAggregates()
def handle(self, *args, **options):
self.makeLoanBalanceView(options["recreate_views"])
self.makeTransactionAggregates(options["recreate_views"])
self.stdout.write(self.style.SUCCESS("Aggregates complete!"))

def makeTransactionAggregates(self):
def makeTransactionAggregates(self, recreate_views):
for interval in ["day", "week", "month"]:
if recreate_views:
self.executeTransaction(
"""
DROP MATERIALIZED VIEW IF EXISTS contributions_by_{}
""".format(
interval
)
)

self.executeTransaction(
"""
DROP MATERIALIZED VIEW IF EXISTS expenditures_by_{}
""".format(
interval
)
)

try:
self.executeTransaction(
"""
Expand All @@ -40,8 +64,10 @@ def makeTransactionAggregates(self):
JOIN camp_fin_filing AS f
ON t.filing_id = f.id
WHERE tt.contribution = TRUE
AND (tt.description = 'Monetary contribution' or
tt.description = 'Anonymous Contribution')
AND tt.description in (
'Monetary Contribution',
'Anonymous Contribution'
)
GROUP BY f.entity_id, date_trunc('{0}', t.received_date)
UNION
SELECT
Expand Down Expand Up @@ -117,7 +143,13 @@ def makeTransactionAggregates(self):

self.executeTransaction(view)

def makeLoanBalanceView(self):
def makeLoanBalanceView(self, recreate_views):
if recreate_views:
self.executeTransaction(
"""
DROP MATERIALIZED VIEW IF EXISTS current_loan_status
"""
)

try:
self.executeTransaction(
Expand Down
13 changes: 11 additions & 2 deletions camp_fin/management/commands/import_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,19 @@ def make_contribution(self, record, contributor, filing):
)

elif "Contribution" in record["Contribution Type"]:
if "in-kind" in record["Contribution Type"].lower():
description = "In-Kind Contribution"
elif "return" in record["Contribution Type"].lower():
description = "Return Contribution"
elif "anonymous" in record["Contribution Type"].lower():
description = "Anonymous Contribution"
else:
description = "Monetary Contribution"

transaction_type, _ = models.TransactionType.objects.get_or_create(
description=record["Contribution Type"],
description=description,
contribution=True,
anonymous=False,
anonymous="anonymous" in record["Contribution Type"].lower(),
)

contribution = models.Transaction(
Expand Down
6 changes: 3 additions & 3 deletions camp_fin/templates/base-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ <h1><i class='fa fa-spin fa-circle-o-notch'></i></h1>
<script>
function transaction_verb(transaction_type){
var verbs = {
'Monetary contribution': 'donated',
'In Kind contribution': 'donated in-kind',
'Monetary Contribution': 'donated',
'In-Kind Contribution': 'donated in-kind',
'Anonymous Contribution': 'anonymously donated',
'Refund monetary (NOT BEING USED)': 'was refunded',
'Return Contribution': 'was refunded',
'Monetary Expenditure': 'received'
}
return verbs[transaction_type];
Expand Down
6 changes: 3 additions & 3 deletions camp_fin/templatetags/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def get_sort_icon(s):
@register.filter
def get_transaction_verb(s):
verbs = {
"Monetary contribution": "donated",
"In Kind contribution": "donated in-kind",
"Monetary Contribution": "donated",
"In-Kind Contribution": "donated in-kind",
"Anonymous Contribution": "anonymously donated",
"Refund monetary (NOT BEING USED)": "was refunded",
"Return Contribution": "was refunded",
"Monetary Expenditure": "spent",
}
return verbs.get(s, "")
Expand Down
3 changes: 1 addition & 2 deletions camp_fin/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class FakeTestData(object):

@classmethod
def races(cls):

cls.first_entity = Entity.objects.create(user_id=1)
cls.second_entity = Entity.objects.create(user_id=2)
cls.third_entity = Entity.objects.create(user_id=3)
Expand Down Expand Up @@ -176,7 +175,7 @@ def races(cls):
)

contribution = TransactionType.objects.create(
description="Monetary contribution", contribution=True, anonymous=False
description="Monetary Contribution", contribution=True, anonymous=False
)

expenditure = TransactionType.objects.create(
Expand Down

0 comments on commit 27a86ba

Please sign in to comment.