def addallaeriesTeachers(): settings = Config.objects.first() if settings.devenv == False: flash("You can only run scripts in a local environment.") return redirect('/') # This reads a csv file in to a dataframe df_aeriesteachers = pd.read_csv('csv/aeriesteachers.csv') df_aeriesteachers.fillna('', inplace=True) # This turns that dataframe in to a python dictionary aeriesteachers = df_aeriesteachers.to_dict(orient='records') l = len(aeriesteachers) for index, teacher in enumerate(aeriesteachers): try: otdatateacher = User.objects.get(otemail = teacher['email'].strip()) editTeacher = True except: editTeacher = False if not teacher['teachernum']: teacher['teachernum'] = 0 if editTeacher: otdatateacher.update( taeriesname = teacher['aeriesname'].strip(), tnum = teacher['teachernum'], afname = teacher['fname'].strip(), alname = teacher['lname'].strip(), troom = teacher['room'].strip(), trmphone = teacher['rmphone'].strip(), tdept = teacher['dept'].strip(), role = teacher['title'].strip() ) otdatateacher.reload() else: otdatateacher = User( taeriesname = teacher['aeriesname'], tnum = teacher['teachernum'], afname = teacher['fname'], alname = teacher['lname'], otemail = teacher['email'], troom = teacher['room'], trmphone = teacher['rmphone'], tdept = teacher['dept'], role = teacher['title'] ) otdatateacher.save() print(f"{index}/{l}: {otdatateacher.taeriesname}") return redirect('/')
def login(): # Go and get the users credentials from google. The /authorize and /oauth2callback functions should not be edited. # That is where the user is sent if their credentials are not currently stored in the session. More about sessions below. if 'credentials' not in session: # send a msg to the user flash('From /login - No credentials in your session. Adding them now.') # send the user to get authenticated by google return redirect(url_for('authorize')) # Now that the user has credentials, use those credentials to access Google's people api and get the users information credentials = google.oauth2.credentials.Credentials(**session['credentials']) session['credentials'] = credentials_to_dict(credentials) people_service = googleapiclient.discovery.build('people', 'v1', credentials=credentials) # set data to be the dictionary that contains all the information about the user that google has. You can see this # information displayed via the current profile template data = people_service.people().get(resourceName='people/me', personFields='names,emailAddresses,photos').execute() # get the google email address from the data object and check to see if the user has an ousd email account. # Deny access if they do not #if not data['emailAddresses'][0]['value'][-8:] == "ousd.org": # flash('You must have an ousd.org email address to access this site') # return redirect(url_for('logout')) try: # see if the user already exists in the user dtabase document. If they don't then this attempt # to create a currUser object from the User class in the data.py file will fail currUser = User.objects.get(gid = data['emailAddresses'][0]['metadata']['source']['id']) flash(f'Welcome Back! {currUser.fname}') # Check the email address in the data object to see if it is in the admins list and update the users # database record if needed. if data['emailAddresses'][0]['value'] in admins: admin = True if currUser.admin == False: currUser.update(admin=True) else: admin = False if currUser.admin == True: currUser.update(admin=False) except: # If the user was not in the database, then set some variables and create them # first decide if they are a student or a teacher by checking the front of their email address if data['emailAddresses'][0]['value'][0:2] == 's_': role = 'student' else: role = 'teacher' #See if the new user is in the Admins list if data['emailAddresses'][0]['value'] in admins: admin = True else: admin = False # Create a newUser object filled with the google values and the values that were just created newUser = User( gid=data['emailAddresses'][0]['metadata']['source']['id'], gfname=data['names'][0]['displayName'], glname=data['names'][0]['familyName'], fname=data['names'][0]['givenName'], lname=data['names'][0]['familyName'], email=data['emailAddresses'][0]['value'], image=data['photos'][0]['url'], role=role, admin=admin ) # save the newUser newUser.save() # then use the mongoengine get() method to get the newUser from the database as the currUser # gid is a unique attribute in the User class that matches google's id which is in the data object currUser = User.objects.get(gid = data['emailAddresses'][0]['metadata']['source']['id']) # send the new user a msg flash(f'Welcome {currUser.fname}. A New user has been created for you.') # this code puts several values in the session list variable. The session variable is a great place # to store values that you want to be able to access while a user is logged in. The values in the sesion # list can be added, changed, deleted as you would with any python list. session['currUserId'] = str(currUser.id) session['displayName'] = currUser.fname+" "+currUser.lname session['gid'] = data['emailAddresses'][0]['metadata']['source']['id'] # this stores the entire Google Data object in the session session['gdata'] = data session['role'] = currUser.role session['admin'] = admin # The return_URL value is set above in the before_request route. This enables a user you is redirected to login to # be able to be returned to the page they originally asked for. return redirect(session['return_URL'])
def login(): # Go and get the users credentials from google. The /authorize and /oauth2callback functions should not be edited. # That is where the user is sent if their credentials are not currently stored in the session. More about sessions below. if 'credentials' not in session: # send a msg to the user # send the user to get authenticated by google return redirect(url_for('authorize')) # Now that the user has credentials, use those credentials to access Google's people api and get the users information credentials = google.oauth2.credentials.Credentials( **session['credentials']) session['credentials'] = credentials_to_dict(credentials) people_service = googleapiclient.discovery.build('people', 'v1', credentials=credentials) # set data to be the dictionary that contains all the information about the user that google has. You can see this # information displayed via the current profile template data = people_service.people().get( resourceName='people/me', personFields='names,emailAddresses,photos').execute() try: issenior = OTSeniors.objects.get( ousdemail=data['emailAddresses'][0]['value']) issenior = True except: issenior = False if data['emailAddresses'][0]['value'][-8:] == "ousd.org" and not data[ 'emailAddresses'][0]['value'][0:2] == "s_": isteacher = True else: isteacher = False if data['emailAddresses'][0]['value'] in admins: isadmin = True else: isadmin = False if not isteacher and not isadmin and not issenior: return redirect(url_for('revoke')) try: # see if the user already exists in the user database document. If they don't then this attempt # to create a currUser object from the User class in the data.py file will fail currUser = User.objects.get( gid=data['emailAddresses'][0]['metadata']['source']['id']) flash(f'Welcome Back! {currUser.fname}') except: # If the user was not in the database, then set some variables and create them # first decide if they are a student or a teacher by checking the front of their email address if data['emailAddresses'][0]['value'][0:2] == 's_': role = 'student' else: role = 'teacher' # Create a newUser object filled with the google values and the values that were just created newUser = User( gid=data['emailAddresses'][0]['metadata']['source']['id'], gfname=data['names'][0]['givenName'], glname=data['names'][0]['familyName'], fname=data['names'][0]['givenName'], lname=data['names'][0]['familyName'], email=data['emailAddresses'][0]['value'], role=role) newUser.save() # Are they a senior if newUser.role == 'student': try: oTSenior = OTSeniors.objects.get(ousdemail=newUser.email) newUser.update(issenior=True, aeriesid=oTSenior.aeriesid) except: pass currUser = User.objects.get(email=newUser.email) flash( f'Welcome {currUser.fname} {currUser.lname}. A New user has been created for you.' ) if currUser.email in admins: if currUser.admin == False: currUser.update(admin=True) else: if currUser.admin == True: currUser.update(admin=False) try: issenior = OTSeniors.objects.get(ousdemail=currUser.email) if currUser.issenior == False: currUser.update(issenior=True, aeriesid=issenior.aeriesid) except: if currUser.issenior == True: currUser.update(issenior=False) session['currUserId'] = str(currUser.id) session['displayName'] = currUser.fname + " " + currUser.lname session['gid'] = currUser.gid # this stores the entire Google Data object in the session session['gdata'] = data session['role'] = currUser.role session['admin'] = currUser.admin session['issenior'] = currUser.issenior # The return_URL value is set above in the before_request route. This enables a user you is redirected to login to # be able to be returned to the page they originally asked for. return redirect(session['return_URL'])