Make executable, clean up style, and rename new door updater

This commit is contained in:
Adam Goldsmith 2019-11-08 16:07:26 -05:00
parent 21a9aa5b5c
commit 2b894d3cc9

21
new_xml_based.py → doorUpdater.py Normal file → Executable file
View File

@ -1,11 +1,10 @@
#!/usr/bin/env python3
import requests
from lxml import etree
from common import doors, getMembershipworksData, memberLevels
from hid.DoorController import E, ROOT
from hid.Credential import Credential
class Member():
def __init__(self, forename, surname, membershipWorksID,
middleName="", email="", phone="",
@ -129,9 +128,10 @@ def update_door(door, members):
allCredentials = set(Credential(hex=c.attrib['rawCardNumber'])
for c in door.get_credentials())
for member in members:
# TODO: can I combine requests?
if member.membershipWorksID in cardholders: # cardholder exists, compare contents
for member in members:
# cardholder exists, compare contents
if member.membershipWorksID in cardholders:
ch = cardholders.pop(member.membershipWorksID)
member.cardholderID = ch.cardholderID
if member.attribs() != ch.attribs(): # update cardholder attributes
@ -179,25 +179,28 @@ def update_door(door, members):
door.doXMLRequest(ROOT(member.make_credentials(
newCards - allCredentials, cardFormats)))
if member.schedulesForDoor(door) != ch.schedules:
print("- Updating schedule for" +
f" {member.forename} {member.surname}:" +
f" {ch.schedules} -> {member.schedulesForDoor(door)}")
door.doXMLRequest(ROOT(member.make_schedules(door, schedulesMap)))
door.doXMLRequest(ROOT(
member.make_schedules(door, schedulesMap)))
else: # cardholder did not exist, so add them
print(f"- Adding Member:")
print(member)
resp = door.doXMLRequest(ROOT(
E.Cardholders({"action": "AD"},
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),
door.doXMLRequest(ROOT(
member.make_credentials(member.credentials, cardFormats),
member.make_schedules(door, schedulesMap)))
# TODO: delete cardholders that are no longer members?
def main():
memberData = getMembershipworksData(
['members', 'staff', 'misc'],
@ -209,4 +212,6 @@ def main():
print(door.name, door.ip)
update_door(door, members)
if __name__ == '__main__':
main()