Skip to content

Instantly share code, notes, and snippets.

@rayvoelker
Created January 26, 2025 22:58
Show Gist options
  • Select an option

  • Save rayvoelker/87aa3399177a3f4d87f22733839439df to your computer and use it in GitHub Desktop.

Select an option

Save rayvoelker/87aa3399177a3f4d87f22733839439df to your computer and use it in GitHub Desktop.
import pandas as pd
from math import isnan
class StudentNew:
"""
Defines a CUSTOMIZED patron record object for use with the API.
"""
def __init__(self,
last_name,
first_name,
barcode,
student_id,
school_district,
pin,
school,
birth_date,
phone_number,
home_legal_address,
home_legal_address_city,
home_legal_address_state,
home_legal_address_zip,
notice_pref=None,
email_address=None,
home_library_code=None,
patron_agency=None,
alt_id=None,
years_until_expiration=3
):
"""
:param birth_date: a pandas Timestamp (or python datetime) for the child's DOB
:param years_until_expiration: e.g. 3, to set the expiration date as today + 3 years
"""
# Keep a reference if needed:
self.alt_id = alt_id
self.birth_date = birth_date
# 1) Calculate an expiration date N years from now
expiration_date = (pd.Timestamp.now() + pd.DateOffset(years=years_until_expiration)).strftime('%Y-%m-%d')
# 2) Patron type logic (example: 1 => child, 2 => teen, 3 => adult)
self.years_old = int((pd.Timestamp.now() - birth_date).days / 365)
if self.years_old >= 18:
self.patron_type = 3
elif self.years_old >= 13:
self.patron_type = 2
else:
self.patron_type = 1
# 3) Format addresses
addresses_line1 = home_legal_address
addresses_line2 = f"{home_legal_address_city}, {home_legal_address_state} {home_legal_address_zip}"
# 4) Build the final data structure that matches Sierra’s patron schema
self.patron_data = {
'expirationDate': expiration_date,
'birthDate': birth_date.strftime('%Y-%m-%d'),
'patronType': self.patron_type,
'blockInfo': {'code': '-'},
'phones': [{'number': str(phone_number), 'type': 't'}],
'pMessage': '-',
'fixedFields': {
# Examples, your codes may differ
'44': {'label': 'E-Lib Update? (P1)', 'value': 'n'},
'45': {'label': 'Friends? (P2)', 'value': 'n'},
'46': {'label': 'Foundation? (P3)', 'value': '1'},
'86': {'label': 'Agency', 'value': str(patron_agency)},
'158': {'label': 'Patron Agency', 'value': str(patron_agency)},
# Default to 'z' => "no notices" or "email"?
# Possibly read from your data or override with "notice_pref"
'268': {'label': 'Notice Preference', 'value': 'z'},
},
"names": [
f"{last_name}, {first_name}"
],
"barcodes": [
str(barcode)
],
"homeLibraryCode": str(home_library_code),
"varFields": [
# fieldTag "d" => “birthDate” in MMDDYYYY
{
'fieldTag': 'd',
'content': birth_date.strftime('%m%d%Y')
},
{
"fieldTag": 'x',
"content": "ConnectED"
},
{
"fieldTag": "l",
"content": str(school).strip().title()
},
],
"addresses": [
{
"lines": [str(addresses_line1), str(addresses_line2)],
"type": "a"
},
],
"pin": str(pin),
}
# If notice preference is non-null in your data, override the default
if notice_pref is not None and not isnan(notice_pref):
self.patron_data['fixedFields']['268']['value'] = str(notice_pref)
# If email is non-null or non-empty
if email_address is not None and not isnan(email_address):
self.patron_data['emails'] = [str(email_address)]
# If alt_id is present, add it as a varField with fieldTag 'v'
if alt_id is not None and str(alt_id).lower() not in ["nan", "none", ""]:
self.patron_data['varFields'].append({
"fieldTag": "v",
"content": str(alt_id)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment