2020-04-02 15:56:19 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from common import membershipworks
|
2020-03-30 14:01:39 -04:00
|
|
|
from lib.mw_models import Label, Member, MemberLabel, Transaction, database
|
|
|
|
|
2020-04-02 15:56:19 -04:00
|
|
|
|
|
|
|
@database.atomic()
|
|
|
|
def main():
|
|
|
|
print("Creating tables")
|
|
|
|
database.create_tables([Label, Member, MemberLabel, Transaction])
|
|
|
|
|
|
|
|
print("Updating labels")
|
2020-03-30 14:01:39 -04:00
|
|
|
labels = membershipworks._parse_flags()["labels"]
|
|
|
|
Label.insert_many(
|
|
|
|
[{"label_id": v, "label": k} for k, v in labels.items()]
|
|
|
|
).on_conflict(action="update", preserve=[Label.label]).execute()
|
2020-04-02 15:56:19 -04:00
|
|
|
|
|
|
|
print("Getting/Updating members...")
|
|
|
|
members = membershipworks.get_all_members()
|
|
|
|
for m in members:
|
|
|
|
# replace flags by booleans
|
2020-03-30 14:01:39 -04:00
|
|
|
for flag in [dek["lbl"] for dek in membershipworks.org_info["dek"]]:
|
2020-04-02 15:56:19 -04:00
|
|
|
if flag in m:
|
|
|
|
m[flag] = m[flag] == flag
|
|
|
|
|
|
|
|
for field_id, field in membershipworks._all_fields().items():
|
|
|
|
# convert checkboxes to real booleans
|
2020-03-30 14:01:39 -04:00
|
|
|
if field.get("typ") == 8 and field["lbl"] in m: # check box
|
|
|
|
m[field["lbl"]] = True if m[field["lbl"]] == "Y" else False
|
2020-04-02 15:56:19 -04:00
|
|
|
|
|
|
|
for member in members:
|
|
|
|
# create/update member
|
|
|
|
Member.from_csv_dict(member).magic_save()
|
|
|
|
|
|
|
|
# update member's labels
|
2020-03-30 14:01:39 -04:00
|
|
|
for label, label_id in membershipworks._parse_flags()["labels"].items():
|
|
|
|
ml = MemberLabel(uid=member["Account ID"], label_id=label_id)
|
2020-04-02 15:56:19 -04:00
|
|
|
if member[label]:
|
|
|
|
ml.magic_save()
|
|
|
|
else:
|
|
|
|
ml.delete_instance()
|
|
|
|
|
|
|
|
print("Getting/Updating transactions...")
|
|
|
|
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)]
|
2020-03-30 14:01:39 -04:00
|
|
|
assert all(
|
|
|
|
[
|
|
|
|
t["Account ID"] == t.get("uid", "") and t["Payment ID"] == t.get("sid", "")
|
|
|
|
for t in transactions
|
|
|
|
]
|
|
|
|
)
|
2020-04-02 15:56:19 -04:00
|
|
|
|
|
|
|
for transaction in transactions:
|
|
|
|
Transaction.from_csv_dict(transaction).magic_save()
|
|
|
|
|
|
|
|
# TODO: folders, levels, addons
|
|
|
|
|
2020-03-30 14:01:39 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-04-02 15:56:19 -04:00
|
|
|
main()
|