Skip to content

Commit

Permalink
start_date route almost done, need to add error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jonager committed Aug 31, 2017
1 parent 92da07c commit 2132b73
Showing 1 changed file with 99 additions and 75 deletions.
174 changes: 99 additions & 75 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,38 @@
from app import app, db
from flask import request, redirect, render_template, session, flash
from models import Student, Teacher, Attendance
<<<<<<< HEAD
from datetime import datetime, date
from app import app, db
from models import Student, Teacher, Attendance
from hash_tools import make_hash, check_hash
import val

# # Main View
# @app.route('/')
# def index():
# return render_template('index.html')
# Main View
@app.route('/')
def index():
return render_template('index.html')

# # Logout
# @app.route('/logout')
# def logout():
# del session['email']
# return redirect('/')
# Logout
@app.route('/logout')
def logout():
del session['email']
return redirect('/')


# Student Login
@app.route('/student_login', methods=["POST", "GET"])
def student_login():
if request.method == 'POST':
first_last = request.form['name'].split()
first = first_last[0]
last = first_last[1]
pin = request.form['pin']
student = Student.query.filter_by(first = first, last = last).first()

if student and student.pin == pin:
# push student into attendance table
new_attendee = Attendance(student)
db.session.add( new_attendee)
db.session.commit()
elif student and student.pin != pin:
return render_template('student_login.html', title = 'Student Login',
pin_err = 'Wrong Pin')
else:
students = Student.query.order_by(Student.last_name).all()
return render_template('student_login.html', title = 'Student Login', students = students)

# Attendance List
@app.route('/attendance_list', methods=["POST", "GET"])
def attendance_list():
return render_template('attendance_list.html')

# # Attendance List
# @app.route('/attendance_list', methods=["POST", "GET"])
# def attendance_list():
# return render_template('attendance_list.html')


# # Student List
# Student List
# @app.route('/student_list', methods=["POST", "GET"])
# def student_list():
# return render_template('student_list.html')




@app.route("/teacher_login", methods=['GET', 'POST'])
def teacher_login():

if request.method == 'POST':
email = request.form['email']
password = request.form['password']
teacher = Teacher.query.filter_by(email = email).first()

if teacher and check_hash(password, teacher.password):
session['email'] = email
return redirect('/')
elif teacher and not check_hash(password, teacher.password):
return render_template('teacher_login.html', title = 'Login', login='active',
password_err = 'Wrong password')
else:
return render_template('teacher_login.html', title = 'Login', login='active',
email_err = 'Wrong username')
else:
return render_template('teacher_login.html', title = 'Signup', signup='active')
# else:
# return render_template('teacher_login.html', title = 'Signup', signup='active')


@app.route("/teacher_signup", methods=['POST'])
Expand All @@ -94,33 +51,33 @@ def teacher_signup():

# check for empty fields
if val.is_empty(first):
return render_template('teacher_login.html', title = 'Signup', signup='active',
firstname_err = 'Please fill in the first name')
return render_template('teacher_login.html', title = 'Signup',
signup='active', firstname_err = 'Please fill in the first name')
elif val.is_empty(last):
return render_template('teacher_login.html', title = 'Signup', signup='active',
lastname_err = 'Please fill in the last name')
return render_template('teacher_login.html', title = 'Signup',
signup='active', lastname_err = 'Please fill in the last name')
elif val.is_empty(email):
return render_template('teacher_login.html', title = 'Signup', signup='active',
email_err = 'Please fill in the email')
return render_template('teacher_login.html', title = 'Signup',
signup='active', email_err = 'Please fill in the email')
elif val.is_empty(password):
return render_template('teacher_login.html', title = 'Signup', signup='active',
password_err = 'Please fill in the password')

return render_template('teacher_login.html', title = 'Signup',
signup='active', password_err = 'Please fill in the password')
# check for spaces
if val.space(email):
return render_template('teacher_login.html', title = 'Signup', signup='active',
email_err = 'Email can\'t have space')
return render_template('teacher_login.html', title = 'Signup',
signup='active', email_err = 'Email can\'t have space')

#check if email already exists
if email_DB:
if email_DB.email:
return render_template('teacher_login.html', title = 'Signup', signup='active',
email_err = 'Email already in use')
return render_template('teacher_login.html', title = 'Signup',
signup='active', email_err = 'Email already in use')

# check for match
if password != confirm_pass:
return render_template('teacher_login.html', title = 'Signup', signup='active',
password_err = 'Email already in use', confirm_err = 'Passwords must match')
return render_template('teacher_login.html', title = 'Signup',
signup='active', confirm_err = 'Passwords must match')

# checks length is bigger than 3 characters.
if val.wrong_len(password) or val.wrong_len(confirm_pass):
Expand All @@ -137,6 +94,73 @@ def teacher_signup():
db.session.add(new_teacher)
db.session.commit()
session['email'] = username



@app.route("/teacher_login", methods=['GET', 'POST'])
def teacher_login():

if request.method == 'POST':
email = request.form['email']
password = request.form['password']
teacher = Teacher.query.filter_by(email = email).first()

if teacher and check_hash(password, teacher.password):
session['email'] = email
return redirect('/')
elif teacher and not check_hash(password, teacher.password):
return render_template('teacher_login.html', title = 'Login', login='active',
password_err = 'Wrong password')
else:
return render_template('teacher_login.html', title = 'Login', login='active',
email_err = 'Wrong username')


@app.route('/start_day')
def start_day():
students = Student.query.all()
students_att = Attendance.query.filter_by(date_now = date.today()).all()

# checks if not attendace list has been created for the day.
if not students_att:
if students:
# pushes all students into the attendance table, creating
# a list for today's date.
for student in students:
record = Attendance(student)
db.session.add(record)
db.session.commit()
return redirect('/student_login')
else:
# the day's list already created
return redirect ('/')




@app.route('/student_login', methods=["POST", "GET"])
def student_login():

students = Student.query.order_by(Student.last_name).all()

if request.method == 'POST':
student_id = request.form['student_id']
pin = request.form['pin']
student = Student.query.get(student_id)
student_att = Attendance.query.filter_by(owner_id = student_id,
date_now = date.today())

if student and student.pin == pin:
# make student present in attendance table
student_att.present = True
db.session.commit()
elif student and student.pin != pin:
return render_template('student_login.html', title ='Student Login',
pin_err = 'Wrong Pin', students = students,
student_id = student_id)
else:
return render_template('student_login.html', title = 'Student Login',
students = students)


if __name__ == "__main__":
app.run()

0 comments on commit 2132b73

Please sign in to comment.