sqlExport: export all non-folder flags, instead of just labels

This commit is contained in:
Adam Goldsmith 2020-05-18 20:25:35 -04:00
parent 7ca2b30d4a
commit 85a2ab977a
2 changed files with 33 additions and 28 deletions

View File

@ -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):

View File

@ -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)