sqlExport: export all non-folder flags, instead of just labels
This commit is contained in:
parent
7ca2b30d4a
commit
85a2ab977a
@ -6,6 +6,7 @@ from peewee import (
|
|||||||
DateField,
|
DateField,
|
||||||
DateTimeField,
|
DateTimeField,
|
||||||
DecimalField,
|
DecimalField,
|
||||||
|
CharField,
|
||||||
FixedCharField,
|
FixedCharField,
|
||||||
ForeignKeyField,
|
ForeignKeyField,
|
||||||
Model,
|
Model,
|
||||||
@ -65,14 +66,7 @@ class BaseModel(Model):
|
|||||||
database = database
|
database = database
|
||||||
|
|
||||||
|
|
||||||
class Label(BaseModel):
|
# TODO: is this still a temporal table?
|
||||||
label_id = FixedCharField(24, primary_key=True)
|
|
||||||
label = TextField(null=True)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
table_name = "labels"
|
|
||||||
|
|
||||||
|
|
||||||
class Member(BaseModel):
|
class Member(BaseModel):
|
||||||
uid = FixedCharField(24, primary_key=True)
|
uid = FixedCharField(24, primary_key=True)
|
||||||
year_of_birth = TextField(column_name="Year of Birth", null=True)
|
year_of_birth = TextField(column_name="Year of Birth", null=True)
|
||||||
@ -199,13 +193,18 @@ class Member(BaseModel):
|
|||||||
table_name = "members"
|
table_name = "members"
|
||||||
|
|
||||||
|
|
||||||
class MemberLabel(BaseModel):
|
class Flag(BaseModel):
|
||||||
uid = ForeignKeyField(Member, column_name="uid", backref="labels")
|
id = FixedCharField(24, primary_key=True)
|
||||||
label_id = ForeignKeyField(Label, backref="members")
|
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:
|
class Meta:
|
||||||
table_name = "member_labels"
|
primary_key = CompositeKey("flag_id", "uid")
|
||||||
primary_key = CompositeKey("label_id", "uid")
|
|
||||||
|
|
||||||
|
|
||||||
class Transaction(BaseModel):
|
class Transaction(BaseModel):
|
||||||
|
@ -3,20 +3,24 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from .config import Config
|
from .config import Config
|
||||||
from .mw_models import Label, Member, MemberLabel, Transaction, database
|
from .mw_models import Member, Flag, MemberFlag, Transaction, database
|
||||||
|
|
||||||
|
|
||||||
@database.atomic()
|
@database.atomic()
|
||||||
def do_import(config):
|
def do_import(config):
|
||||||
membershipworks = config.membershipworks
|
membershipworks = config.membershipworks
|
||||||
print("Creating tables")
|
print("Creating tables")
|
||||||
database.create_tables([Label, Member, MemberLabel, Transaction])
|
database.create_tables([Member, Flag, MemberFlag, Transaction])
|
||||||
|
|
||||||
print("Updating labels")
|
print("Updating flags (labels, levels, and addons)")
|
||||||
labels = membershipworks._parse_flags()["labels"]
|
flags = membershipworks._parse_flags()
|
||||||
Label.insert_many(
|
Flag.insert_many(
|
||||||
[{"label_id": v, "label": k} for k, v in labels.items()]
|
[
|
||||||
).on_conflict(action="update", preserve=[Label.label]).execute()
|
{"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...")
|
print("Getting/Updating members...")
|
||||||
members = membershipworks.get_all_members()
|
members = membershipworks.get_all_members()
|
||||||
@ -35,17 +39,19 @@ def do_import(config):
|
|||||||
# create/update member
|
# create/update member
|
||||||
Member.from_csv_dict(member).magic_save()
|
Member.from_csv_dict(member).magic_save()
|
||||||
|
|
||||||
# update member's labels
|
# update member's flags
|
||||||
for label, label_id in membershipworks._parse_flags()["labels"].items():
|
for type, flags in membershipworks._parse_flags().items():
|
||||||
ml = MemberLabel(uid=member["Account ID"], label_id=label_id)
|
if type != "folders": # currently no way to retrieve this info
|
||||||
if member[label]:
|
for flag, id in flags.items():
|
||||||
ml.magic_save()
|
ml = MemberFlag(uid=member["Account ID"], flag_id=id)
|
||||||
else:
|
if member[flag]:
|
||||||
ml.delete_instance()
|
ml.magic_save()
|
||||||
|
else:
|
||||||
|
ml.delete_instance()
|
||||||
|
|
||||||
print("Getting/Updating transactions...")
|
print("Getting/Updating transactions...")
|
||||||
# Deduping these is hard, so just recreate the data every time
|
# Deduping these is hard, so just recreate the data every time
|
||||||
Transaction.truncate_table();
|
Transaction.truncate_table()
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
start_date = datetime(2010, 1, 1)
|
start_date = datetime(2010, 1, 1)
|
||||||
transactions_csv = membershipworks.get_transactions(start_date, now)
|
transactions_csv = membershipworks.get_transactions(start_date, now)
|
||||||
|
Reference in New Issue
Block a user