2019-11-04 01:25:17 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import requests
|
|
|
|
from lxml import etree
|
|
|
|
|
|
|
|
from common import doors, getMembershipworksData, memberLevels
|
|
|
|
from hid.DoorController import E, ROOT
|
|
|
|
|
|
|
|
class Member():
|
|
|
|
def __init__(self, forename, surname, membershipWorksID,
|
|
|
|
middleName="", email="", phone="",
|
|
|
|
cardholderID=None, doorAccess=[], onHold=False,
|
|
|
|
credentials=[], levels=[], schedules=[]):
|
|
|
|
self.forename = forename
|
|
|
|
self.surname = surname
|
|
|
|
self.membershipWorksID = membershipWorksID
|
|
|
|
self.middleName = middleName
|
|
|
|
self.email = email
|
|
|
|
self.phone = phone
|
|
|
|
self.cardholderID = cardholderID
|
|
|
|
self.doorAccess = doorAccess
|
|
|
|
self.onHold = onHold
|
|
|
|
self.credentials = credentials
|
|
|
|
self.levels = levels
|
|
|
|
self.schedules = schedules
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"""Name: {self.forename} | {self.middleName} | {self.surname}
|
|
|
|
MembershipWorks ID: {self.membershipWorksID}
|
|
|
|
Email: {self.email}
|
|
|
|
Phone: {self.phone}
|
|
|
|
Cardholder ID: {self.cardholderID}
|
|
|
|
doorAccess: {self.doorAccess}
|
|
|
|
On Hold? {self.onHold}
|
|
|
|
Credentials: {self.credentials}
|
|
|
|
Levels: {self.levels}
|
|
|
|
Schedules: {self.schedules}
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_cardholder(cls, data, cardFormats):
|
|
|
|
# TODO: maybe keep all attribs, and just add them in
|
|
|
|
# from_MembershipWorks?
|
|
|
|
cardFormatsByID = {v: k for k, v in cardFormats.items()}
|
|
|
|
credentials = [
|
|
|
|
(cardFormatsByID[c.attrib['formatID']], c.attrib['cardNumber'])
|
|
|
|
for c in data.findall('{*}Credential')]
|
|
|
|
roles = [r.attrib['scheduleName']
|
|
|
|
for r in data.findall('{*}Role')]
|
|
|
|
levels = data.attrib.get('custom1', "").split('|')
|
|
|
|
return cls(
|
|
|
|
forename=data.get('forename', ""),
|
|
|
|
surname=data.get('surname', ""),
|
|
|
|
membershipWorksID=data.attrib.get('custom2', ""),
|
|
|
|
middleName=data.attrib.get('middleName', ""),
|
|
|
|
email=data.attrib.get('email', ""),
|
|
|
|
phone=data.attrib.get('phone', ""),
|
|
|
|
cardholderID=data.attrib['cardholderID'],
|
|
|
|
credentials=credentials,
|
|
|
|
levels=levels,
|
|
|
|
schedules=roles)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_MembershipWorks(cls, data):
|
|
|
|
if data["Access Card Number"] != "":
|
|
|
|
credentials = [(data["Access Card Facility Code"],
|
|
|
|
data["Access Card Number"])]
|
|
|
|
else:
|
|
|
|
credentials = []
|
|
|
|
|
|
|
|
levels = {k: v for k, v in memberLevels.items() if data[k] == k}
|
|
|
|
doorAccess = [door for door, doorData in doors.items()
|
|
|
|
if data["Access " + doorData.access + "?"] == "Y"]
|
|
|
|
|
|
|
|
return cls(data["First Name"],
|
|
|
|
data["Last Name"],
|
|
|
|
membershipWorksID=data["Account ID"],
|
|
|
|
email=data["Email"],
|
|
|
|
phone=data["Phone"],
|
|
|
|
doorAccess=doorAccess,
|
|
|
|
onHold=data["Account on Hold"] != "",
|
|
|
|
credentials=credentials,
|
|
|
|
levels=list(levels.keys()),
|
|
|
|
schedules=list(levels.values()))
|
|
|
|
|
|
|
|
def attribs(self):
|
2019-11-07 15:22:50 -05:00
|
|
|
return {"forename": self.forename,
|
|
|
|
"surname": self.surname,
|
|
|
|
"middleName": self.middleName,
|
|
|
|
"email": self.email,
|
|
|
|
"phone": self.phone,
|
|
|
|
"custom1": "|".join(self.levels).replace("&", "and"),
|
|
|
|
"custom2": self.membershipWorksID}
|
|
|
|
|
|
|
|
def schedulesForDoor(self, door):
|
2019-11-04 01:25:17 -05:00
|
|
|
if door.name not in self.doorAccess or self.onHold:
|
|
|
|
return []
|
2019-11-07 15:22:50 -05:00
|
|
|
else:
|
|
|
|
return self.schedules
|
|
|
|
|
|
|
|
def make_schedules(self, door, schedulesMap):
|
|
|
|
roles = [
|
|
|
|
E.Role({"roleID": self.cardholderID,
|
|
|
|
"scheduleID": schedulesMap[schedule],
|
|
|
|
"resourceID": "0"})
|
|
|
|
for schedule in self.schedulesForDoor(door)]
|
2019-11-04 01:25:17 -05:00
|
|
|
|
2019-11-07 15:22:50 -05:00
|
|
|
return E.RoleSet({"action": "UD", "roleSetID": self.cardholderID},
|
|
|
|
E.Roles(*roles))
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
def make_credentials(self, cardFormats):
|
2019-11-07 15:22:50 -05:00
|
|
|
credentials = [
|
|
|
|
E.Credential(
|
2019-11-04 01:25:17 -05:00
|
|
|
{"formatName": credential[0],
|
|
|
|
"cardNumber": credential[1],
|
|
|
|
"formatID": cardFormats[credential[0]],
|
|
|
|
"isCard": "true",
|
|
|
|
"cardholderID": self.cardholderID})
|
2019-11-07 15:22:50 -05:00
|
|
|
for credential in self.credentials]
|
2019-11-04 01:25:17 -05:00
|
|
|
|
2019-11-07 15:22:50 -05:00
|
|
|
return E.Credentials({"action": "AD"}, *credentials)
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
def update_door(door, members):
|
2019-11-07 15:22:50 -05:00
|
|
|
cardFormats = door.get_cardFormats()
|
|
|
|
cardholders = {member.membershipWorksID: member
|
|
|
|
for member in [Member.from_cardholder(ch, cardFormats)
|
|
|
|
for ch in door.get_cardholders()]}
|
|
|
|
schedulesMap = door.get_scheduleMap()
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
for member in members:
|
|
|
|
# TODO: can I combine requests?
|
|
|
|
if member.membershipWorksID in cardholders: # cardholder exists, compare contents
|
|
|
|
ch = cardholders.pop(member.membershipWorksID)
|
|
|
|
member.cardholderID = ch.cardholderID
|
2019-11-07 15:22:50 -05:00
|
|
|
if member.attribs() != ch.attribs(): # update cardholder attributes
|
2019-11-04 01:25:17 -05:00
|
|
|
print(f"- Updating profile for {member.forename} {member.surname}")
|
|
|
|
print(f" - Old: {ch.attribs()}")
|
|
|
|
print(f" - New: {member.attribs()}")
|
|
|
|
door.doXMLRequest(ROOT(
|
|
|
|
E.Cardholders(
|
|
|
|
{"action": "UD", "cardholderID": member.cardholderID},
|
|
|
|
E.CardHolder(member.attribs()))))
|
2019-11-07 15:22:50 -05:00
|
|
|
|
2019-11-04 01:25:17 -05:00
|
|
|
if member.credentials != ch.credentials:
|
|
|
|
print(f"- Updating card for {member.forename} {member.surname}")
|
|
|
|
print(f" - {ch.credentials} -> {member.credentials}")
|
2019-11-07 15:22:50 -05:00
|
|
|
door.doXMLRequest(ROOT(member.make_credentials(cardFormats)))
|
2019-11-04 01:25:17 -05:00
|
|
|
|
2019-11-07 15:22:50 -05:00
|
|
|
if member.schedulesForDoor(door) != ch.schedules:
|
2019-11-04 01:25:17 -05:00
|
|
|
print("- Updating schedule for" +
|
|
|
|
f" {member.forename} {member.surname}:" +
|
2019-11-07 15:22:50 -05:00
|
|
|
f" {ch.schedules} -> {member.schedulesForDoor(door)}")
|
|
|
|
door.doXMLRequest(ROOT(member.make_schedules(door, schedulesMap)))
|
|
|
|
else: # cardholder did not exist, so add them
|
2019-11-04 01:25:17 -05:00
|
|
|
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"]
|
2019-11-07 15:22:50 -05:00
|
|
|
door.doXMLRequest(ROOT(member.make_credentials(cardFormats),
|
|
|
|
member.update_schedules(door, schedulesMap)))
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
# TODO: delete cardholders that are no longer members?
|
|
|
|
|
|
|
|
def main():
|
|
|
|
memberData = getMembershipworksData(
|
|
|
|
['members', 'staff', 'misc'],
|
|
|
|
"_id,nam,phn,eml,lvl,lbl,xws,xms,xsc,xas,xfd,xac,xcf")
|
|
|
|
|
|
|
|
members = [Member.from_MembershipWorks(m) for m in memberData]
|
|
|
|
|
|
|
|
for door in doors.values():
|
|
|
|
print(door.name, door.ip)
|
|
|
|
update_door(door, members)
|
|
|
|
|
|
|
|
main()
|