This repository has been archived on 2024-02-23. You can view files and clone it, but cannot push or open issues or pull requests.
memberPlumbing/sqlExport.py

63 lines
2.2 KiB
Python
Executable File

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