-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_faculty.py
executable file
·92 lines (78 loc) · 2.45 KB
/
sync_faculty.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
from ldap3 import Server, Connection, ALL
from peewee import *
from api.everything import *
cfg = load_config('api/config.yaml')
# skt = load_config('api/secret_config.yaml')
theStaticDB = SqliteDatabase(cfg['databases']['static'])
theStaticDB.drop_table(LDAPFaculty)
server = Server ('berea.edu', port=389, use_ssl=False, get_info='ALL')
# conn = Connection (server, user=skt['ldap']['user'], password=skt['ldap']['pass'])
conn = Connection (server, user="BPLP", password="Ol1v4r!Tun4bp")
if not conn.bind():
print('error in bind', conn.result)
# search_base and search_filter are the parameters
conn.search('dc=berea,dc=edu',
'(description=Faculty)',
attributes = ['samaccountname', 'givenname', 'sn', 'employeeid']
)
print ("Found {0} faculty.".format(len(conn.entries)))
# print (conn.entries[0])
def safe (d, k):
result = ""
try:
if k in d:
print("Result: ", d[k])
result = d[k]
# If we can't find a key, skip it.
except:
print("Skipping: ", d, k)
pass
return result
# Recreate the table.
theStaticDB.create_table(LDAPFaculty)
faculty = conn.entries
for fac in faculty:
try:
if str(fac.samaccountname) != 'ptfaculty':
print ("Faculty: {0}".format(fac.samaccountname))
o, c = LDAPFaculty.get_or_create(
lastname = safe (fac, 'sn'),
firstname = safe (fac, 'givenname'),
username = fac.samaccountname,
bnumber = safe (fac, 'employeeid')
)
except:
print("Error")
#o = LDAPFaculty (
# lastname = safe (fac, 'sn'),
# firstname = safe (fac, 'givenname'),
# username = fac.samaccountname,
# bnumber = safe (fac, 'employeeid'),
# )
#o.save()
# Now do Staff
conn.search('dc=berea,dc=edu',
'(description=Staff)',
attributes = ['samaccountname', 'givenname', 'sn', 'employeeid']
)
print ("Found {0} staff.".format(len(conn.entries)))
faculty = conn.entries #actually staff, but who cares?
for fac in faculty:
try:
if str(fac.samaccountname) != 'ptfaculty':
print ("Staff: {0}".format(fac.samaccountname))
o, c = LDAPFaculty.get_or_create(
lastname = safe (fac, 'sn'),
firstname = safe (fac, 'givenname'),
username = fac.samaccountname,
bnumber = safe (fac, 'employeeid')
)
except:
print("Error")
#o = LDAPFaculty (
# lastname = safe (fac, 'sn'),
# firstname = safe (fac, 'givenname'),
# username = fac.samaccountname,
# bnumber = safe (fac, 'employeeid'),
# )
# o.save()