2022-02-28 16:06:10 -05:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.db import transaction
|
|
|
|
|
|
|
|
from membershipworks.models import Member, Flag
|
|
|
|
from membershipworks import MembershipWorks
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2022-02-28 17:16:32 -05:00
|
|
|
def flags_for_member(self, csv_member, all_flags, folders):
|
2022-02-28 16:06:10 -05:00
|
|
|
for flag in all_flags:
|
2022-02-28 17:16:32 -05:00
|
|
|
if flag.type == "folder":
|
|
|
|
if csv_member["Account ID"] in folders[flag.id]:
|
|
|
|
yield flag
|
|
|
|
else:
|
2022-02-28 16:06:10 -05:00
|
|
|
if csv_member[flag.name] == flag.name:
|
|
|
|
yield flag
|
|
|
|
|
|
|
|
def update_flags(self, mw_flags) -> list[Flag]:
|
|
|
|
for typ, flags_of_type in mw_flags.items():
|
|
|
|
for name, id in flags_of_type.items():
|
|
|
|
flag = Flag(id=id, name=name, type=typ[:-1])
|
|
|
|
flag.save()
|
|
|
|
yield flag
|
|
|
|
|
|
|
|
@transaction.atomic()
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
membershipworks = MembershipWorks()
|
|
|
|
membershipworks.login(
|
|
|
|
settings.MEMBERSHIPWORKS_USERNAME, settings.MEMBERSHIPWORKS_PASSWORD
|
|
|
|
)
|
|
|
|
|
|
|
|
print("Updating flags (labels, levels, and addons)")
|
|
|
|
flags = list(self.update_flags(membershipworks._parse_flags()))
|
|
|
|
|
2022-02-28 17:16:32 -05:00
|
|
|
print("Getting folder membership")
|
|
|
|
folders = {
|
|
|
|
folder_id: membershipworks.get_member_ids([folder_name])
|
|
|
|
for folder_name, folder_id in membershipworks._parse_flags()[
|
|
|
|
"folders"
|
|
|
|
].items()
|
|
|
|
}
|
|
|
|
|
2022-02-28 16:06:10 -05:00
|
|
|
print("Getting/Updating members...")
|
|
|
|
members = membershipworks.get_all_members()
|
|
|
|
for csv_member in members:
|
|
|
|
for field_id, field in membershipworks._all_fields().items():
|
|
|
|
# convert checkboxes to real booleans
|
|
|
|
if field.get("typ") == 8 and field["lbl"] in csv_member:
|
|
|
|
csv_member[field["lbl"]] = (
|
|
|
|
True if csv_member[field["lbl"]] == "Y" else False
|
|
|
|
)
|
|
|
|
|
|
|
|
# create/update member
|
|
|
|
member = Member.from_csv_dict(csv_member)
|
|
|
|
member.clean_fields()
|
|
|
|
member.save()
|
2022-02-28 17:16:32 -05:00
|
|
|
member.flags.set(self.flags_for_member(csv_member, flags, folders))
|