Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced 'import_users.py' script using logging module #1240

Open
wants to merge 17 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ RUN date
RUN pip install --upgrade pip

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends default-mysql-client ack bash-completion unixodbc unixodbc-dev
&& apt-get -y install --no-install-recommends default-mysql-client ack bash-completion unixodbc unixodbc-dev \
build-essential libssl-dev libffi-dev freetds-dev freetds-bin unixodbc-dev tdsodbc

RUN sudo bash -c "echo -e '[FreeTDS]\nDescription=FreeTDS Driver\nDriver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\
\nSetup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so' >> /etc/odbcinst.ini"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way to break this into two lines while keeping the indentation style so we're just going to have to live with it.


COPY requirements.txt /tmp/pip-tmp/
RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt && rm -rf /tmp/pip-tmp
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ app/static/files/
local-override.yml
geckodriver.log
.coverage
cron.log
5 changes: 4 additions & 1 deletion app/logic/emailHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,7 @@ def replaceStaticPlaceholders(eventId, email_body):
event_link="{event_link}",
recipient_name="{recipient_name}",
relative_time=event.relativeTime)
return new_body
return new_body



254 changes: 157 additions & 97 deletions app/scripts/import_users.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,117 @@
import logging
import pyodbc
import sys
import argparse
from ldap3 import Server, Connection, ALL
import peewee

from app import app
from app.models.user import User
from app.logic.utils import getUsernameFromEmail

def main():
"""
This function runs the updateRecords function once the script is run.
"""
print("Don't forget to put the correct Tracy and LDAP passwords in app/config/local-override.yml")

print("\nGetting Updated Names, Majors, and Class Levels\n--------------------------------------------------\n")
# Argument parser for log levels
def parseArgs():
parser = argparse.ArgumentParser(description="Import users script with logging.")
parser.add_argument(
'--log-level',
default='INFO',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
help='Set the logging level'
)
return parser.parse_args()

arguments = parseArgs()
logLevels = getattr(logging, arguments.log_level.upper(), logging.INFO)

# Configure logging
logger = logging.getLogger(__name__)
logger.setLevel(logLevels)

addToDb(getStudentData())
print("done.")
addToDb(getFacultyStaffData())
print("done.")
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setLevel(logLevels)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
consoleHandler.setFormatter(formatter)

print("\n\nGetting Preferred Names\n--------------------------\n")
logger.addHandler(consoleHandler)


def main():
"""
This function runs the updateRecords function once the script is run.
"""
logger.info("Script started.")
logger.info("Don't forget to put the correct Tracy and LDAP passwords in app/config/local-override.yml")

logger.info("Getting Updated Names, Majors, and Class Levels")

studentData = addToDb(getStudentData())
studentAdded = studentData[0]
studentUpdated = studentData[1]
logger.info(f"{studentAdded} students were added.")
logger.info(f"{studentUpdated} students were updated.")
logger.info("Finished updating student data.")

facultyStaffData = addToDb(getFacultyStaffData())
facultyStaffAdded = facultyStaffData[0]
facultyStaffUpdated = facultyStaffData[1]
logger.info(f"{facultyStaffAdded} faculties/staffs were added.")
logger.info(f"{facultyStaffUpdated} faculties/staffs were updated.")
logger.info("Finished updating faculty and staff data.")

logger.info("Getting Preferred Names from LDAP")
ldap = getLdapConn()
print("LDAP Connected.")
logger.info("LDAP Connected.")

people = fetchLdapList(ldap, alphaRange('a','d'))
people += fetchLdapList(ldap, alphaRange('e','j'))
people += fetchLdapList(ldap, alphaRange('k','p'))
people += fetchLdapList(ldap, alphaRange('q','z'))

updateFromLdap(people)
print("Update Complete.")
logger.info("Update from LDAP Complete.")

def alphaRange(start,end):
def alphaRange(start, end):
return [chr(i) for i in range(ord(start), ord(end)+1)]

def getLdapConn():
server = Server ('berea.edu', port=389, use_ssl=False, get_info='ALL')
conn = Connection (server, user=app.config['ldap']['user'], password=app.config['ldap']['password'])
if not conn.bind():
print(conn.result)
raise Exception("BindError")

return conn
try:
server = Server('berea.edu', port=389, use_ssl=False, get_info=ALL)
conn = Connection(server, user=app.config['ldap']['user'], password=app.config['ldap']['password'])
if not conn.bind():
logger.error(f"LDAP bind failed: {conn.result}")
raise Exception("BindError")
return conn
except Exception as e:
logger.error(f"Failed to connect to LDAP: {e}")
raise

def fetchLdapList(conn, startletters):
# Get the givennames from LDAP - we have to segment them to make sure each request is under 1500
conn.search('dc=berea,dc=edu',
f"(|" + "".join(map(lambda s: f"(givenname={s}*)", startletters)) + ")",
attributes = ['samaccountname', 'givenname', 'sn', 'employeeid']
)
print(f"Found {len(conn.entries)} {startletters[0]}-{startletters[-1]} in AD");
return conn.entries
try:
conn.search(
'dc=berea,dc=edu',
f"(|" + "".join(map(lambda s: f"(givenname={s}*)", startletters)) + ")",
attributes=['samaccountname', 'givenname', 'sn', 'employeeid']
)
logger.info(f"Found {len(conn.entries)} entries for {startletters[0]}-{startletters[-1]} in AD")
return conn.entries
except Exception as e:
logger.error(f"Failed to fetch LDAP list for {startletters[0]}-{startletters[-1]}: {e}")
raise

def updateFromLdap(people):
for person in people:
bnumber = str(get_key(person, 'employeeid')).strip()
preferred = str(get_key(person, 'givenname')).strip()

if preferred:
count = User.update(firstName=preferred).where(User.bnumber == bnumber).execute()
if count:
print(f"Updating {bnumber} name to {preferred}")
try:
count = User.update(firstName=preferred).where(User.bnumber == bnumber).execute()
if count:
logger.debug(f"Updated {bnumber} name to {preferred}")
except Exception as e:
logger.error(f"Failed to update user {bnumber} with preferred name {preferred}: {e}")

# Return the value for a key or None
# Can't use .get() because it's a ldap3.abstract.entry.Entry instead of a Dict
def get_key(entry, key):
if key in entry:
return entry[key]
Expand All @@ -78,81 +125,94 @@ def getMssqlCursor():
"host": app.config["tracy"]["host"],
"db": app.config["tracy"]["name"]
}
pyodbc_uri = 'DRIVER=FreeTDS;SERVER={};PORT=1433;DATABASE={};UID={};PWD={};TDS_Version=8.0;'.format(details['host'],details['db'],details['user'],details['password'])

pyconn = pyodbc.connect(pyodbc_uri) # connects a tcp based client socket to a tcp based server socket
return pyconn.cursor() # allows python to execute sql database commands
pyodbc_uri = 'DRIVER=FreeTDS;SERVER={};PORT=1433;DATABASE={};UID={};PWD={};TDS_Version=8.0;'.format(
details['host'], details['db'], details['user'], details['password']
)
try:
pyconn = pyodbc.connect(pyodbc_uri)
logger.info("Connected to Tracy database.")
return pyconn.cursor()
except Exception as e:
logger.error(f"Failed to connect to Tracy database: {e}")
raise

def addToDb(userList):
usersAdded = 0
usersUpdated = 0
for user in userList:
try:
User.insert(user).execute()

logger.debug(f"Inserted user {user['bnumber']}")
usersAdded += 1
except peewee.IntegrityError as e:
if user['username']:
(User.update(firstName = user['firstName'], lastName = user['lastName'], email = user['email'], major = user['major'], classLevel = user['classLevel'], cpoNumber = user['cpoNumber'])
.where(user['bnumber'] == User.bnumber)).execute()
else:
print(f"No username for {user['bnumber']}!", user)

try:
if user['username']:
(User.update(
firstName=user['firstName'],
lastName=user['lastName'],
email=user['email'],
major=user['major'],
classLevel=user['classLevel'],
cpoNumber=user['cpoNumber']
).where(User.bnumber == user['bnumber'])).execute()
logger.debug(f"Updated user {user['bnumber']}")
usersUpdated += 1
else:
logger.warning(f"No username for {user['bnumber']}!", user)
except Exception as e:
logger.error(f"Failed to update user {user['bnumber']}: {e}")
except Exception as e:
print(e)
logger.error(f"Failed to insert or update user {user['bnumber']}: {e}")

return [usersAdded, usersUpdated]

def getFacultyStaffData():
"""
This function pulls all the faculty and staff data from Tracy and formats for our table

Tracy's STUSTAFF table has the following columns:
1. PIDM
2. ID
3. FIRST_NAME
4. LAST_NAME
5. EMAIL
6. CPO
7. ORG
8. DEPT_NAME
"""
print("Retrieving Faculty data from Tracy...",end="")
c = getMssqlCursor()
return [
{ "username": getUsernameFromEmail(row[4].strip()),
"bnumber": row[1].strip(),
"email": row[4].strip(),
"phoneNumber": None,
"firstName": row[2].strip(),
"lastName": row[3].strip(),
"isStudent": False,
"isFaculty": True,
"isStaff": False,
"major": None,
"classLevel": None,
"cpoNumber": row[5].strip(),
}
for row in c.execute('select * from STUSTAFF')
]
logger.info("Retrieving Faculty and Staff data from Tracy...")
try:
c = getMssqlCursor()
return [
{
"username": getUsernameFromEmail(row[4].strip()),
"bnumber": row[1].strip(),
"email": row[4].strip(),
"phoneNumber": None,
"firstName": row[2].strip(),
"lastName": row[3].strip(),
"isStudent": False,
"isFaculty": True,
"isStaff": False,
"major": None,
"classLevel": None,
"cpoNumber": row[5].strip(),
}
for row in c.execute('select * from STUSTAFF')
]
except Exception as e:
logger.error(f"Failed to retrieve Faculty and Staff data: {e}")
raise

def getStudentData():
"""
This function pulls all the student data from Tracy and formats for our table
"""
print("Retrieving Student data from Tracy...",end="")
c = getMssqlCursor()
return [
{ "username": getUsernameFromEmail(row[9].strip()),
"bnumber": row[1].strip(),
"email": row[9].strip(),
"phoneNumber": None,
"firstName": row[2].strip(),
"lastName": row[3].strip(),
"isStudent": True,
"major": row[6].strip(),
"classLevel": row[4].strip(),
"cpoNumber": row[10].strip(),
}
for row in c.execute('select * from STUDATA')
]
logger.info("Retrieving Student data from Tracy...")
try:
c = getMssqlCursor()
return [
{
"username": getUsernameFromEmail(row[9].strip()),
"bnumber": row[1].strip(),
"email": row[9].strip(),
"phoneNumber": None,
"firstName": row[2].strip(),
"lastName": row[3].strip(),
"isStudent": True,
"major": row[6].strip(),
"classLevel": row[4].strip(),
"cpoNumber": row[10].strip(),
}
for row in c.execute('select * from STUDATA')
]
except Exception as e:
logger.error(f"Failed to retrieve Student data: {e}")
raise

if __name__ == '__main__':
main()