Move getMembershipworksData to common.py

for use in members web list
This commit is contained in:
Adam Goldsmith 2018-06-19 18:24:15 -04:00
parent a6191a5b38
commit c0d7ef4f86
2 changed files with 48 additions and 46 deletions

View File

@ -1,6 +1,8 @@
import csv
import json import json
import urllib3 import urllib3
import sys import sys
from io import StringIO
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
import requests import requests
@ -16,6 +18,50 @@ ET.register_namespace("hid", "http://www.hidcorp.com/VertX")
fieldnames = "CardNumber,CardFormat,PinRequired,PinCode,ExtendedAccess,ExpiryDate,Forename,Initial,Surname,Email,Phone,Custom1,Custom2,Schedule1,Schedule2,Schedule3,Schedule4,Schedule5,Schedule6,Schedule7,Schedule8".split(",") fieldnames = "CardNumber,CardFormat,PinRequired,PinCode,ExtendedAccess,ExpiryDate,Forename,Initial,Surname,Email,Phone,Custom1,Custom2,Schedule1,Schedule2,Schedule3,Schedule4,Schedule5,Schedule6,Schedule7,Schedule8".split(",")
def getMembershipworksData(columns):
""" Pull the members csv from the membershipworks api
columns: which columns to get"""
BASE_URL = "https://api.membershipworks.com/v1/"
# login
r = requests.post(BASE_URL + 'usr',
data={"_st": "all",
"eml": MEMBERSHIPWORKS_USERNAME,
"org": "10000",
"pwd": MEMBERSHIPWORKS_PASSWORD})
if r.status_code != 200 or 'SF' not in r.json():
print("MembershipWorks Login Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
SFtoken = r.json()['SF']
# get list of member/staff IDs
r = requests.get(BASE_URL + "ylp",
params={"SF": SFtoken,
"lbl": "5ae37979f033bfe8534f8799,5771675edcdf126302a2f6b9", # members and staff
"org": "15475", # unknown
"var": "_id,nam,ctc"})
if r.status_code != 200 or 'usr' not in r.json():
print("MembershipWorks User Listing Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
ids = [user['_id'] for user in r.json()['usr']]
# get members CSV
# TODO: maybe can just use previous get instead? would return JSON
r = requests.post(BASE_URL + "csv",
params={"SF": SFtoken},
data={"_rt": "946702800", # unknown
"mux": "", # unknown
"tid": ",".join(ids), # ids of members to get
"var": columns})
if r.status_code != 200:
print("MembershipWorks CSV Generation Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
return list(csv.DictReader(StringIO(r.text)))
def doImportRequest(ip, params=None, files=None): def doImportRequest(ip, params=None, files=None):
"""Send a request to the door control import script""" """Send a request to the door control import script"""
r = requests.post( r = requests.post(

View File

@ -16,49 +16,6 @@ memberLevels = {"CMS Staff": "7x24",
"CMS Unlimited": "Unlimited", "CMS Unlimited": "Unlimited",
"CMS Nights & Weekends": "Nights and Weekends"} "CMS Nights & Weekends": "Nights and Weekends"}
def getMembershipworksData():
""" Pull the members csv from the membershipworks api """
BASE_URL = "https://api.membershipworks.com/v1/"
# login
r = requests.post(BASE_URL + 'usr',
data={"_st": "all",
"eml": MEMBERSHIPWORKS_USERNAME,
"org": "10000",
"pwd": MEMBERSHIPWORKS_PASSWORD})
if r.status_code != 200 or 'SF' not in r.json():
print("MembershipWorks Login Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
SFtoken = r.json()['SF']
# get list of member/staff IDs
r = requests.get(BASE_URL + "ylp",
params={"SF": SFtoken,
"lbl": "5ae37979f033bfe8534f8799,5771675edcdf126302a2f6b9", # members and staff
"org": "15475", # unknown
"var": "_id,nam,ctc"})
if r.status_code != 200 or 'usr' not in r.json():
print("MembershipWorks User Listing Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
ids = [user['_id'] for user in r.json()['usr']]
# get members CSV
# TODO: maybe can just use previous get instead? would return JSON
r = requests.post(BASE_URL + "csv",
params={"SF": SFtoken},
data={"_rt": "946702800", # unknown
"mux": "", # unknown
"tid": ",".join(ids), # ids of members to get
"var": "lvl,xws,xms,xsc,xas,xfd,xac,phn,eml,lbl,xcf,nam"}) # which columns to get
if r.status_code != 200:
print("MembershipWorks CSV Generation Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
return r.text
def makeMember(member, doorAuth): def makeMember(member, doorAuth):
"""Create an output CSV row for the member""" """Create an output CSV row for the member"""
if member["Access Card Number"] == "": if member["Access Card Number"] == "":
@ -114,9 +71,8 @@ def makeDoor(doorName, doorData, members, hashes):
json.dump(hashes, f) json.dump(hashes, f)
def main(): def main():
data = getMembershipworksData() members = getMembershipworksData(
reader = csv.DictReader(StringIO(data)) "lvl,xws,xms,xsc,xas,xfd,xac,phn,eml,lbl,xcf,nam")
members = list(reader)
members.sort(key=lambda x: x['Last Name']) members.sort(key=lambda x: x['Last Name'])
if os.path.exists('/tmp/doorUpdaterLastHash'): if os.path.exists('/tmp/doorUpdaterLastHash'):