forked from asmbly-makerspace/NeonIntegrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattendanceToTestout.py
More file actions
151 lines (134 loc) · 4.57 KB
/
Copy pathattendanceToTestout.py
File metadata and controls
151 lines (134 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import base64
import datetime
import logging
import traceback
import helpers.neon as neon
from helpers.api import apiCall
from config import N_APIkey, N_APIuser
# Neon Account Info
N_auth = f"{N_APIuser}:{N_APIkey}"
N_baseURL = "https://api.neoncrm.com/v2"
N_signature = base64.b64encode(bytearray(N_auth.encode())).decode()
N_headers = {
"Content-Type": "application/json",
"Authorization": f"Basic {N_signature}",
}
TODAY = datetime.date.today()
DELTA_DAYS = (TODAY - datetime.timedelta(days=7)).isoformat()
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%H:%S",
)
EVENT_FIELDS = {
"Festool Domino": "440",
"Shaper Origin": "274",
"Woodshop Safety": "84",
"Metal Shop Safety": "520",
"Big Lasers": "86",
"CNC Router": "435",
"Stationary Sanders": "675",
"Metal Lathe": "676",
"Wood Lathe": "677",
"MIG Welding": "678",
"TIG Welding": "679",
"Small Lasers": "680",
"Laser Engrave Round": "1007",
"Milling": "681",
"Tormach": "682",
"Filament": "683",
"Resin": "684",
"Sublimation": "685",
"Vinyl": "686",
"Orientation": "182",
}
def toolTestingUpdate(className: str, neonId: int, inputDate: str):
date = datetime.datetime.strftime(
datetime.datetime.strptime(inputDate, "%Y-%m-%d"), "%m/%d/%Y"
)
fieldId = None
for name, id in EVENT_FIELDS.items():
if name in className:
fieldId = id
shortName = name
acctCustFields = neon.getAccountIndividual(neonId)["individualAccount"][
"accountCustomFields"
]
customIdList = []
for field in acctCustFields:
customIdList.append(field["id"])
if fieldId and fieldId not in customIdList:
try:
##### NEON #####
# Update part of an account
# https://developer.neoncrm.com/api-v2/#/Accounts/patchAccount
httpVerb = "PATCH"
resourcePath = f"/accounts/{neonId}"
queryParams = ""
data = {
"individualAccount": {
"accountCustomFields": [{"id": fieldId, "value": date}]
}
}
url = N_baseURL + resourcePath + queryParams
patch = apiCall(httpVerb, url, data, N_headers)
if patch.status_code == 200:
logging.info(
"%s SUCCESS! \n\tAccount ID %s \n\tClass '%s'",
patch.status_code,
neonId,
shortName,
)
else:
logging.error(
"%s FAILED! \n\tAccount ID %s \n\tClass '%s'",
patch.status_code,
neonId,
shortName,
)
except:
logging.error(
"UPDATE FAILED FOR UNKNOWN REASON! \n\tAccount ID %s \n\tClass '%s'",
neonId,
shortName,
)
elif not fieldId:
logging.info("%s does not have a corresponding custom field", className)
else:
logging.debug("Account ID %s already has %s marked", neonId, shortName)
searchFields = [
{"field": "Event End Date", "operator": "GREATER_AND_EQUAL", "value": DELTA_DAYS},
{
"field": "Event End Date",
"operator": "LESS_AND_EQUAL",
"value": TODAY.isoformat(),
},
{"field": "Event Archived", "operator": "EQUAL", "value": "No"},
]
outputFields = ["Event Name", "Event ID", "Event End Date"]
logging.info("Starting Tool Testing update for %s:", TODAY.isoformat())
try:
eventSearch = neon.postEventSearch(searchFields, outputFields)
if responseEvents := eventSearch["searchResults"]:
for event in responseEvents:
eventName = event["Event Name"]
eventId = event["Event ID"]
eventDate = event["Event End Date"]
registrants = neon.getEventRegistrants(eventId)["eventRegistrations"]
if type(registrants) is not type(None):
for registrant in registrants:
attended = registrant["tickets"][0]["attendees"][0][
"markedAttended"
]
if attended == True:
toolTestingUpdate(
eventName, registrant["registrantAccountId"], eventDate
)
else:
logging.info("Event Search contained no results")
except TypeError:
pass
except:
logging.error("Event Search Failed")
if traceback.format_exc():
logging.error(traceback.format_exc())