-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhackerspace_utils.py
210 lines (124 loc) · 4.41 KB
/
hackerspace_utils.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import copy
import os
import json
import re;
import pandas as pd;
import time;
from datetime import datetime;
debug = False
# What accounts do payments get recorded into?
payment_config="./payment_processing.json"
def get_paymentconfig():
with open(payment_config) as cfgfile:
return json.load(cfgfile)
# Out of the repo, natch.
secrets="../hackerspace-auth.json"
cache="../bearer-cache.json"
empty_cache = {
'empty_cache' : True,
'token' : "No token here",
'refresh_token': "No refresh token here",
'token_expires': time.time(),
'refresh_expires': time.time(),
'token_expires_h': "No token here",
'refresh_expires_h': "No refresh token here",
}
def get_auth_bag_without_cache():
with open(secrets) as authfile:
bag1 = json.load(authfile)
return(bag1)
def get_auth_bag():
with open(secrets) as authfile:
bag1 = json.load(authfile)
bag1.update(get_bearer_cache())
format_apiurl(bag1)
return(bag1)
def format_apiurl(authbag):
authbag['apiurl'] = authbag['apiurl_pattern'] .format(**authbag)
def get_bearer_cache(fail_ood=True,filename=cache):
tempcache = ""
if os.path.isfile(filename):
with open(filename) as infile:
tempcache= json.load(infile)
else:
tempcache = copy.deepcopy(empty_cache)
# Set it already expired if the file is missing.
secs_still_good = tempcache['token_expires'] - time.time()
if(debug): print("# QBO auth token from cache good for {0:.1f} secs".format(secs_still_good))
if(secs_still_good < 1 and fail_ood is True):
raise ValueError('Token from cache no longer valid; expired {0}'.format(tempcache['token_expires_h']))
return(tempcache)
def dump_bearer_cache(bearin,filename=cache):
with open(filename,"w") as outfile:
os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
time.tzset()
json.dump(
{
'token' : bearin.accessToken,
'refresh_token': bearin.refreshToken,
'token_expires': time.time() +bearin.accessTokenExpiry ,
'refresh_expires': time.time() +bearin.refreshExpiry ,
'token_expires_h': time.ctime(time.time() +bearin.accessTokenExpiry) ,
'refresh_expires_h': time.ctime(time.time() +bearin.refreshExpiry) ,
},
outfile,
indent=4,
sort_keys=True,
)
def dump_authfile(bagin,filename=secrets):
with open(filename,"w") as outfile:
os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
time.tzset()
json.dump( bagin, outfile, indent=4, sort_keys=True, )
def add_realm_to_bearer_cache(realm,filename=cache):
with open(filename) as infile:
tempcache= json.load(infile)
tempcache['realm'] = tempcache['realm_id'] = realm
with open(filename,"w") as outfile :
json.dump(tempcache,outfile,indent=4,sort_keys=True)
stripepat = re.compile(r"^stripe: *(?P<custid>.+)$",re.MULTILINE)
def extract_stripecust_from_notes(innotes):
result = stripepat.search(innotes)
if result is None:
return "[NO STRIPE CUST]"
else:
return result.group('custid')
csvin="../memberlist.csv"
cols_we_want = {
"QBO ID" : "QBOID",
"Name" : "name",
"Email" : 'email',
"Phone Number" : 'phone',
}
def all_cust_df(colmap=cols_we_want):
df = pd.read_csv(csvin,header=1)
df = df.loc[ df['ID'].notna()]
df["QBO ID"] = df["QBO ID"].fillna(0).astype(int)
if not (colmap is None):
df = df.filter(items=colmap.keys())
df = df.rename(axis="columns",mapper=colmap)
df = df.fillna("")
return(df)
def active_cust_df(colmap=cols_we_want):
if (debug): print("# Reading active customers")
df = pd.read_csv(csvin,header=1)
df = df.loc[ df['ID'].notna()]
df = df.loc[df['STATUS'] == 'ACTIVE']
if not (colmap is None):
df = df.filter(items=colmap.keys())
df = df.rename(axis="columns",mapper=colmap)
df = df.fillna("")
df['QBOID'] = df['QBOID'].astype(int)
return(df)
memberships={
"REGULAR" : {
"amount" : 35.00,
"itemid" : 4
},
"STUDENT": {
"amount": 25.00
,"itemid" : 5
}
}
def dayify(instamp):
return datetime(instamp.year,instamp.month,instamp.day)