def addSth(): """ The function is an example of how to insert data in the firebase realtimeDB""" if session['user']: # Check if user has logged in yet token = session['user'] # To access to the currenr user's uid user = auth.get_account_info(token)['users'][0]['localId'] data = {"name": "Mortimer 'Morty' Smith"} """ The structure of the database should be | - project name <lunch chat> | - collection | - user's uid | - data you want to safe """ collection = "profile" firebase.database().child(collection).child(user).update(data) print('data inserted') return render_template('/index.html') else: print("You need to log in first") return redirect(url_for("main.homepage"))
def show_students(): """ Return template for student profile. """ if session['user']: student_profile = firebase.database().child('student_profile').get() student_profile = [student.val() for student in student_profile.each()] return render_template('Recruiters/student_grid.html', student_profile=student_profile) else: print("please login first") return redirect(url_for("main.homepage"))
def create_recruiter_profile(): if session['user']: # Check if user has logged in yet if request.method == "GET": return render_template('Recruiters/create_profile.html') elif request.method == "POST": data = { "name": request.form.get("name"), "bio": request.form.get("bio"), "company": request.form.get("company"), "title": request.form.get("title"), "talent": ",".join(request.form.getlist('talent')), } user = getUserID() firebase.database().child("recruiter_profile").child(user).update( data) print('data inserted') return redirect(url_for("recruiter.recruiter_main")) else: print("You need to log in first") return redirect(url_for("main.homepage"))
def getRecruiterProfile(userID): collection = "recruiter_profile" user_profile = firebase.database().child(collection).child(userID).get() user_profile_data = user_profile.val() data = { "name": user_profile_data['name'], "bio": user_profile_data['bio'], "company": user_profile_data['company'], "title": user_profile_data['title'], "talent": user_profile_data['talent'], } return data
def getStudentProfile(userID): collection = "student_profile" user_profile = firebase.database().child(collection).child(userID).get() user_profile_data = user_profile.val() data = { "name": user_profile_data['name'], "bio": user_profile_data['bio'], "school": user_profile_data['school'], "degree": user_profile_data['degree'], "graduationDate": user_profile_data['graduationDate'], } return data
def update_student_profile(): """ The function can update data for the student profile """ if session['user']: if request.method == "GET": user = getUserID() # To access to the currenr user's uid data = getStudentProfile(user) return render_template('Students/update_profile.html', **data) elif request.method == "POST": data = { "name": request.form.get("name"), "school": request.form.get("school"), "degree": request.form.get("degree"), "graduationDate": request.form.get("graduationDate"), "bio": request.form.get("bio") } user = getUserID() firebase.database().child("student_profile").child(user).update( data) print('data updated!') return redirect(url_for("student.student_main")) else: print("You have to be logged in first!") return redirect(url_for("main.homepage"))
def update_recruiter_profile(): """ The function can update data for the recruiter profile """ if session['user']: if request.method == "GET": user = getUserID() # To access to the currenr user's uid data = getRecruiterProfile(user) return render_template('Recruiters/update_profile.html', **data) elif request.method == "POST": data = { "name": request.form.get("name"), "bio": request.form.get("bio"), "company": request.form.get("company"), "title": request.form.get("title"), "talent": request.form.get("talent"), } user = getUserID() firebase.database().child("recruiter_profile").child(user).update( data) print('data updated!') return redirect(url_for("recruiter.recruiter_main")) else: print("You have to be logged in first!") return redirect(url_for("main.homepage"))
def show_recruiters(): """ Return template for student profile. """ if session['user']: recruiter_profile = firebase.database().child( 'recruiter_profile').get() recruiter_profile = [ recruiter.val() for recruiter in recruiter_profile.each() ] return render_template('Students/recruiter_grid.html', recruiter_profile=recruiter_profile) else: print("please login first") return redirect(url_for("main.homepage"))
def create_student_profile(): """ Return template for create_student_profile and Store data to firebase """ if session['user']: # Check if user has logged in yet if request.method == "GET": return render_template('Students/create_profile.html') elif request.method == "POST": data = { "name": request.form.get("name"), "bio": request.form.get("bio"), "school": request.form.get("school"), "degree": request.form.get("degree"), "graduationDate": request.form.get("graduationDate") } user = getUserID() firebase.database().child("student_profile").child(user).update( data) print('data inserted') return redirect(url_for("student.student_main")) else: print("You need to log in first") return redirect(url_for("main.homepage"))
def signup(): """ Get method: Return signup template and pass the role user choosed to template POST method:Create an account, store user role to db and redirect to create profile page """ if request.method == "GET": role = session['role'] if role == None: return render_template("Auth/re_direct_role.html") else: return render_template("Auth/signup.html", role=role) elif request.method == "POST": try: email = request.form.get("email") password = request.form.get("password") # Create User Account signup_user = auth.create_user_with_email_and_password( email, password) loginUser(email, password) # Login user to create session userID = getUserID() # Save User's role with UserID to db firebase.database().child('Role').child(userID).update( {"role": request.form.get("role")}) print("account created!") role = getUserRole() # Redirect user to create profile if role == 'Students': return redirect(url_for("student.create_student_profile")) elif role == 'Recruiters': return redirect(url_for("recruiter.create_recruiter_profile")) else: return render_template('Auth/login.html') except: role = session['role'] error = "Could not sign up" return render_template("Auth/signup.html", role=role, error=error)
def getUserRole(): user = getUserID() role = firebase.database().child("Role").child(user).get() role_data = role.val() return role_data['role']