doorUpdater: Reuse more code/flow between new and existing members

This commit is contained in:
Adam Goldsmith 2020-03-29 00:10:25 -04:00
parent e832851fc4
commit 952852badb

View File

@ -1,5 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import copy
from common import doors, membershipworks, memberLevels, doorSpecificSchedules from common import doors, membershipworks, memberLevels, doorSpecificSchedules
from lib.hid.Credential import Credential from lib.hid.Credential import Credential
from lib.hid.DoorController import ROOT, E from lib.hid.DoorController import ROOT, E
@ -155,10 +157,27 @@ def update_door(door, members):
# TODO: can I combine requests? # TODO: can I combine requests?
for membershipworksMember in members: for membershipworksMember in members:
member = membershipworksMember.to_DoorMember(door) member = membershipworksMember.to_DoorMember(door)
# cardholder did not exist, so add them
if member.membershipWorksID not in cardholders:
print("- Adding Member {member.forename} {member.surname}:")
print(f" - {member.attribs()}")
resp = door.doXMLRequest(ROOT(
E.Cardholders(
{"action": "AD"},
E.Cardholder(member.attribs()))))
member.cardholderID = resp.find('{*}Cardholders/{*}Cardholder') \
.attrib["cardholderID"]
# create a dummy ch to force an update
# TODO: probably a cleaner way to do this
ch = copy.copy(member)
ch.schedules = []
ch.credentials = set()
# cardholder exists, compare contents # cardholder exists, compare contents
if member.membershipWorksID in cardholders: else:
ch = cardholders.pop(member.membershipWorksID) ch = cardholders.pop(member.membershipWorksID)
member.cardholderID = ch.cardholderID member.cardholderID = ch.cardholderID
if member.attribs() != ch.attribs(): # update cardholder attributes if member.attribs() != ch.attribs(): # update cardholder attributes
print(f"- Updating profile for {member.forename} {member.surname}") print(f"- Updating profile for {member.forename} {member.surname}")
print(f" - Old: {ch.attribs()}") print(f" - Old: {ch.attribs()}")
@ -211,20 +230,7 @@ def update_door(door, members):
print("- Updating schedule for" + print("- Updating schedule for" +
f" {member.forename} {member.surname}:" + f" {member.forename} {member.surname}:" +
f" {ch.schedules} -> {member.schedules}") f" {ch.schedules} -> {member.schedules}")
door.doXMLRequest(ROOT( door.doXMLRequest(ROOT(member.make_schedules(schedulesMap)))
member.make_schedules(schedulesMap)))
else: # cardholder did not exist, so add them
print(f"- Adding Member:")
print(member)
resp = door.doXMLRequest(ROOT(
E.Cardholders(
{"action": "AD"},
E.Cardholder(member.attribs()))))
member.cardholderID = resp.find('{*}Cardholders/{*}Cardholder') \
.attrib["cardholderID"]
door.doXMLRequest(ROOT(
member.make_credentials(member.credentials, cardFormats),
member.make_schedules(schedulesMap)))
# TODO: delete cardholders that are no longer members? # TODO: delete cardholders that are no longer members?