Skip to content

Commit

Permalink
cleaned up validation in main, sizing changes in student/teacher_logi…
Browse files Browse the repository at this point in the history
…n, base & change.html
  • Loading branch information
dannyneira committed Sep 3, 2017
1 parent c9e2957 commit 2a9d392
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 47 deletions.
70 changes: 38 additions & 32 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
from flask import request, redirect, render_template, session, flash
from models import Student, Teacher, Attendance
from datetime import datetime, date
from app import app, db
from models import Student, Teacher, Attendance
from hash_tools import make_hash, check_hash
from random import choice
import val

def bg_image(key=None):
bg_images = {'index':'cover_banner_blue-8152795f6794e4bbb9fae2a63ad5bb01.jpg',
'teacher':'learn_banner_blue-105e0234e99f61dcc8f06852d653d617.jpg',
'student':'apply_banner_blue-1340ee49156f5f7ac7a4b9bb0ce8ef5c.jpg',
'settings':'hire_banner_blue-8e55ca145435f6a988067be969412c24.jpg'}
if key == None:
return choice(list(bg_images.values()))
else:
return bg_images[key]

# Main View
@app.route('/')
def index():
bg_images = {'index':'cover_banner_blue-8152795f6794e4bbb9fae2a63ad5bb01.jpg',
'teacher':'learn_banner_blue-105e0234e99f61dcc8f06852d653d617.jpg',
'student':'apply_banner_blue-1340ee49156f5f7ac7a4b9bb0ce8ef5c.jpg',
'settings':'hire_banner_blue-8e55ca145435f6a988067be969412c24.jpg'}

session['email'] = "[email protected]"
return render_template('index.html', bg_image=bg_image['index'])
return render_template('index.html', title='LaunchCode Attendance', bg_image=bg_image())

# Logout
@app.route('/logout')
Expand All @@ -27,7 +32,7 @@ def logout():
# Attendance List
@app.route('/attendance_list', methods=["POST", "GET"])
def attendance_list():
return render_template('attendance_list.html')
return render_template('attendance_list.html', title='Attendance', bg_image=bg_image('settings'))

#TODO
@app.route('/students', methods=["POST", "GET"])
Expand All @@ -42,9 +47,7 @@ def students():
return redirect('/students')

students = Student.query.all()
return render_template('students.html', students=students)
# else:
# return render_template('teacher_login.html', title = 'Signup', signup='active')
return render_template('students.html', students=students, bg_image=bg_image('settings'))

@app.route("/teacher_signup", methods=['POST'])
def teacher_signup():
Expand Down Expand Up @@ -101,7 +104,7 @@ def teacher_signup():
err = True

if err == True:
return render_template('teacher_login.html', title = 'Signup', signup='active')
return render_template('teacher_login.html', title='Signup',signup='active', bg_image=bg_image('teacher'))

new_teacher = Teacher(first, last, email, password)
db.session.add(new_teacher)
Expand All @@ -121,7 +124,7 @@ def teacher_login():
elif teacher and not check_hash(password, teacher.password):
flash("Wrong Password!", 'error')

return render_template('teacher_login.html', title = 'Login', login='active')
return render_template('teacher_login.html', title='Login', login='active', bg_image=bg_image('teacher'))

@app.route('/start_day')
def start_day():
Expand All @@ -141,55 +144,59 @@ def start_day():
else:
# the day's list has not been created
flash('Today\'s attendance hasn\'t been created yet.', 'error')
return render_template('index.html', title = 'Attendance App')
return redirect('/')
else:
# the day's list already created
flash('Today\'s attendance already created', 'error')
return render_template('index.html', title = 'Attendance App')
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()).first()


### Validation ###
err = False

if not pin:
flash("Please enter your Pin!", 'error')
return render_template('student_login.html', title ='Student Login', students = students)
err = True
elif not pin.isdigit():
flash("Your Pin cannot have Letters!", 'error')
return render_template('student_login.html', title ='Student Login', students = students)
err = True
elif student and student.pin == 0 and student.pin == int(pin):
# Redirect student to change pin if it's the first time the sign in.
flash("Please change your pin", 'error')
return redirect('/change_pin?id=' + student_id)
elif student and student.pin != int(pin):
flash("Wrong Pin!", 'error')
return render_template('student_login.html', title ='Student Login', students = students)
err = True

if err == True:
return render_template('student_login.html', title='Student Login', student_err=student, students=students, bg_image=bg_image('student'))
else:
# no validation error
# make student present in attendance table
student_att.present = True
db.session.commit()
flash(student.first_name.title()+" Signed in!", 'info')
return render_template('student_login.html', title ='Student Login', students = students)
return render_template('student_login.html', title='Student Login', students=students, bg_image=bg_image('student'))
else:
attendance_exists = Attendance.query.filter_by(date_now = date.today()).first()

# Validate if today's date exists in database
if attendance_exists is None:
flash('Please create today\'s attendance list first (Press \'START DAY\' button)' , 'error')
return render_template('index.html', title = 'Attendance App')
return redirect('/')

return render_template('student_login.html', title = 'Student Login', students = students)
return render_template('student_login.html', title='Student Login', students=students, bg_image=bg_image('student'))

# Allows students to change their pin the very first time
# (first time an attendance list is created) the sign in.
Expand All @@ -199,13 +206,12 @@ def change_pin():
if request.method == 'GET':
student_id = request.args.get('id')
student = Student.query.get(student_id)
# return render_template('change_pin.html', name = name, student = student)
return render_template('change_pin.html',student = student,
title = 'Change Pin')

return render_template('change_pin.html', student=student, title='Change Pin', bg_image=bg_image('student'))
else:
student_id = request.form['student_id']
student = Student.query.get(student_id)
student_att = Attendance.query.filter_by(owner_id = student_id,
student_att = Attendance.query.filter_by(owner_id=student_id,
date_now = date.today()).first()
pin = request.form['pin']
confirm_pin = request.form['confirm_pin']
Expand All @@ -222,11 +228,11 @@ def change_pin():
flash('Pin must be 4 digits long and can only contain integers', 'error')
err = True
elif int(pin) == 0:
flash('Your pin can\'t be 0', 'error')
flash('Your pin can\'t be 0000', 'error')
err = True

if err == True:
return render_template('change_pin.html',student = student, title = 'Change Pin')
return render_template('change_pin.html',student=student, title='Change Pin', bg_image=bg_image('student'))

# change pin in the user table
student.pin = pin
Expand All @@ -241,10 +247,10 @@ def attendance():
if request.method == 'POST':
date_now = request.form['date_now']
attendance = Attendance.query.filter_by(date_now=date_now).all()
return render_template("attendance.html", attendance=attendance)
return render_template("attendance.html", attendance=attendance, bg_image=bg_image('settings'))
else:
dates = Attendance.query.filter_by().all()
return render_template("attendance.html", dates=dates)
return render_template("attendance.html", dates=dates, bg_image=bg_image('settings'))


@app.route("/edit_student", methods=['GET', 'POST'])
Expand All @@ -270,7 +276,7 @@ def edit_student():
else:
id = request.args.get('id')
student = Student.query.filter_by(id=id).first()
return render_template("edit_student.html", student=student)
return render_template("edit_student.html", student=student, bg_image=bg_image('settings'))

if __name__ == "__main__":
app.run()
22 changes: 11 additions & 11 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
main {
flex: 1 0 auto;
background-image: url('https://www.launchcode.org/assets/cover_banner_blue-8152795f6794e4bbb9fae2a63ad5bb01.jpg');
background-image: url('https://www.launchcode.org/assets/{{ bg_image }}');
background-position: center -50px;
background-repeat: no-repeat;
background-attachment: fixed;
Expand Down Expand Up @@ -62,29 +62,29 @@
background-color: rgba(51,51,51,0.1) !important;
}
#teacher-card{
margin: 3rem -1.5rem 3rem -1.5rem;
height: 550px;
margin: 3rem 0;
height: auto;
}
#teacher{
width: 50%;
width: 40%;
}
#student-card{
margin: 8rem 0 8rem 0;
height: 325px;
margin: 6rem 0;
height: auto;
}
#student{
width: 20%;
}
#change-card{
margin: 6rem 0 6rem 0;
height: 425px;
margin: 6rem 0;
height: auto;
}
#change{
width: 30%;
width: 20%;
}
#att-date-card{
margin: 6rem 0 6rem 0;
height: 425px;
margin: 2rem 0;
height: auto;
}
#index{
margin: 12rem 0 12rem 0;
Expand Down
2 changes: 1 addition & 1 deletion templates/change_pin.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</div>
</div>
<div class="row">
<button class="btn waves-effect waves-light lc-orange right" type="submit" name="action">Change Pin</button>
<button class="btn waves-effect waves-light lc-light-orange right" type="submit" name="action">Change Pin</button>
</div>
</form>
</div>
Expand Down
10 changes: 9 additions & 1 deletion templates/student_login.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
<div class="input-field col s12">
<select name="student_id" id="students">
{% for student in students %}
{% if student_err %}
{% if student_err.id == student.id %}
<option value="{{ student.id }}" selected>{{ student.last_name }}, {{ student.first_name }}</option>
{% else %}
<option value="{{ student.id }}">{{ student.last_name }}, {{ student.first_name }}</option>
{% endif %}
{% else %}
<option value="{{ student.id }}">{{ student.last_name }}, {{ student.first_name }}</option>
{% endif %}
{% endfor %}
<label for="students">Students</label>
</select>
Expand All @@ -20,7 +28,7 @@
</div>
</div>
<div class="row">
<button class="btn waves-effect waves-light lc-light-orange right" type="submit" name="action">Sign-In</button>
<button class="btn waves-effect waves-light right" type="submit" name="action">Sign-In</button>
</div>
</form>
</div>
Expand Down
4 changes: 2 additions & 2 deletions templates/teacher_login.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</div>
</div>
<div class="row">
<button class="btn waves-effect waves-light lc-orange right" type="submit" name="action">Login</button>
<button class="btn waves-effect waves-light lc-light-orange right" type="submit" name="action">Login</button>
</div>
</form>
</div>
Expand Down Expand Up @@ -59,7 +59,7 @@
</div>
</div>
<div class="row">
<button class="btn waves-effect waves-light lc-orange right" type="submit" name="action">Signup</button>
<button class="btn waves-effect waves-light lc-light-orange right" type="submit" name="action">Signup</button>
</div>
</form>
</div>
Expand Down

0 comments on commit 2a9d392

Please sign in to comment.