membershipworks: Scrape Transactions

This commit is contained in:
Adam Goldsmith 2023-12-19 23:43:49 -05:00
parent 0a92c28efc
commit cd63a169aa
2 changed files with 41 additions and 2 deletions

View File

@ -1,8 +1,10 @@
from datetime import datetime
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import transaction
from membershipworks.models import Member, Flag
from membershipworks.models import Member, Flag, Transaction
from membershipworks import MembershipWorks
@ -56,3 +58,23 @@ class Command(BaseCommand):
member.clean_fields()
member.save()
member.flags.set(self.flags_for_member(csv_member, flags, folders))
print("Getting/Updating transactions...")
# Deduping these is hard, so just recreate the data every time
Transaction.objects.all().delete()
now = datetime.now()
start_date = datetime(2010, 1, 1)
transactions_csv = membershipworks.get_transactions(start_date, now)
transactions_json = membershipworks.get_transactions(start_date, now, json=True)
# this is terrible, but as long as the dates are the same, should be fiiiine
transactions = [{**j, **v} for j, v in zip(transactions_csv, transactions_json)]
assert all(
[
t["Account ID"] == t.get("uid", "")
and t["Payment ID"] == t.get("sid", "")
for t in transactions
]
)
for csv_transaction in transactions:
Transaction.from_csv_dict(csv_transaction).save()

View File

@ -5,6 +5,7 @@ import django.core.mail.message
from django.conf import settings
from django.db import models
from django.db.models import Exists, OuterRef
from django.utils import timezone
class BaseModel(models.Model):
@ -277,7 +278,7 @@ class MemberFlag(BaseModel):
]
class Transaction(models.Model):
class Transaction(BaseModel):
sid = models.CharField(max_length=27, null=True, blank=True)
member = models.ForeignKey(
Member,
@ -307,6 +308,22 @@ class Transaction(models.Model):
phone = models.TextField(db_column="Phone", null=True, blank=True)
email = models.TextField(db_column="Email", null=True, blank=True)
@classmethod
def from_csv_dict(cls, data):
txn = data.copy()
# can't use '%s' format string, have to use the special function
txn["_dp"] = datetime.fromtimestamp(
txn["_dp"], tz=timezone.get_current_timezone()
)
return super().from_csv_dict(txn)
_csv_headers_override = {
"uid": "member_id",
"_dp": "timestamp",
"Transaction Type": "type",
"Event/Form Name": "for_what",
}
def __str__(self):
return f"{self.type} [{self.member if self.member else self.name}] {self.timestamp}"