From 85a2ab977ae4facda113529caede136f57769559 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 18 May 2020 20:25:35 -0400 Subject: [PATCH] sqlExport: export all non-folder flags, instead of just labels --- memberPlumbing/mw_models.py | 25 ++++++++++++------------- memberPlumbing/sqlExport.py | 36 +++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/memberPlumbing/mw_models.py b/memberPlumbing/mw_models.py index ed0e1c5..f2a0005 100644 --- a/memberPlumbing/mw_models.py +++ b/memberPlumbing/mw_models.py @@ -6,6 +6,7 @@ from peewee import ( DateField, DateTimeField, DecimalField, + CharField, FixedCharField, ForeignKeyField, Model, @@ -65,14 +66,7 @@ class BaseModel(Model): database = database -class Label(BaseModel): - label_id = FixedCharField(24, primary_key=True) - label = TextField(null=True) - - class Meta: - table_name = "labels" - - +# TODO: is this still a temporal table? class Member(BaseModel): uid = FixedCharField(24, primary_key=True) year_of_birth = TextField(column_name="Year of Birth", null=True) @@ -199,13 +193,18 @@ class Member(BaseModel): table_name = "members" -class MemberLabel(BaseModel): - uid = ForeignKeyField(Member, column_name="uid", backref="labels") - label_id = ForeignKeyField(Label, backref="members") +class Flag(BaseModel): + id = FixedCharField(24, primary_key=True) + name = TextField(null=True) + type = CharField(6) + + +class MemberFlag(BaseModel): + uid = ForeignKeyField(Member, column_name="uid", backref="flags") + flag_id = ForeignKeyField(Flag, backref="members") class Meta: - table_name = "member_labels" - primary_key = CompositeKey("label_id", "uid") + primary_key = CompositeKey("flag_id", "uid") class Transaction(BaseModel): diff --git a/memberPlumbing/sqlExport.py b/memberPlumbing/sqlExport.py index 46d6ee2..7978877 100755 --- a/memberPlumbing/sqlExport.py +++ b/memberPlumbing/sqlExport.py @@ -3,20 +3,24 @@ from datetime import datetime from .config import Config -from .mw_models import Label, Member, MemberLabel, Transaction, database +from .mw_models import Member, Flag, MemberFlag, Transaction, database @database.atomic() def do_import(config): membershipworks = config.membershipworks print("Creating tables") - database.create_tables([Label, Member, MemberLabel, Transaction]) + database.create_tables([Member, Flag, MemberFlag, 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("Updating flags (labels, levels, and addons)") + flags = membershipworks._parse_flags() + Flag.insert_many( + [ + {"id": v, "name": k, "type": typ[:-1]} + for typ, flags_of_type in flags.items() + for k, v in flags_of_type.items() + ] + ).on_conflict(action="update", preserve=[Flag.name, Flag.type]).execute() print("Getting/Updating members...") members = membershipworks.get_all_members() @@ -35,17 +39,19 @@ def do_import(config): # 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() + # update member's flags + for type, flags in membershipworks._parse_flags().items(): + if type != "folders": # currently no way to retrieve this info + for flag, id in flags.items(): + ml = MemberFlag(uid=member["Account ID"], flag_id=id) + if member[flag]: + ml.magic_save() + else: + ml.delete_instance() print("Getting/Updating transactions...") # Deduping these is hard, so just recreate the data every time - Transaction.truncate_table(); + Transaction.truncate_table() now = datetime.now() start_date = datetime(2010, 1, 1) transactions_csv = membershipworks.get_transactions(start_date, now)