import re
from flask import Flask, render_template, request, flash, redirect, url_for
from flask import Blueprint
from db import db
from os import path
from models import Student, Studentreview, Admin, Cao, Lecturer, Lecturer_review, Cao_review
from werkzeug.security import generate_password_hash, check_password_hash
import segno
app = Flask(__name__)
app.secret_key = 'gth_dhd11'
MAIN_DB = "database.db"
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{MAIN_DB}'
db.init_app(app)
with app.app_context():
db.create_all()
pattern = r'^[A-Z]{3}/d{4}/d{2}/d{3}$'
registration_bp = Blueprint('registration_bp', __name__)
@registration_bp.route('/registration_success', methods=['GET'])
def registration_success():
return render_template('registration_success.html')
@registration_bp.route('/register', methods=['GET', 'POST'])
def register():
form = ''
if request.method == 'POST':
account_type = request.form.get('register-selector')
if account_type == 'studentform':
firstname = request.form.get('firstname')
lastname = request.form.get('lastname')
matricno = request.form.get('matricno')
sex = request.form.get('sex')
department = request.form.get('department')
dorm = request.form.get('dorm')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
# Check if matricpattern is valid
if not re.match(pattern, matricno):
flash('Invalid matric number format', category='error')
# Check if matricno already exists
existing_student = Student.query.filter_by(matricno=matricno).first()
if existing_student:
flash('Matric Number already exists', category='error')
if len(firstname) < 3:
flash('Names must be greater than 2 chars', category="error")
elif len(lastname) < 3:
flash('Names must be greater than 2 chars', category="error")
elif password1 != password2:
flash('Passwords don't match', category="error")
elif len(password1) < 7:
flash('Password too short, at least 7 chars', category="error")
else:
password = request.form.get('password1')
hashed_password = generate_password_hash(password)
new_student = Studentreview(firstname=firstname, lastname=lastname, matricno=matricno, sex=sex, department=department, dorm=dorm ,password=hashed_password)
db.session.add(new_student)
db.session.commit()
# Generate QR code
qr = segno.make(str(new_student.id))
qr_image = qr.png(scale=10)
new_student.qr_code = qr_image
db.session.commit()
flash('Account Queued!, visit the admin to Complete your registration', category="success")
form = 'studentform'
return redirect(url_for('registration_bp.registration_success'))
elif account_type == 'lecturerform':
username = request.form.get('username')
lastname = request.form.get('lastname')
title = request.form.get('title')
department = request.form.get('department')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
# Check if username already exists
existing_lecturer = Lecturer.query.filter_by(username=username).first()
if existing_lecturer:
flash('Username already exists', category='error')
if len(username) < 3:
flash('Username too short', category="error")
elif len(lastname) < 3:
flash('Names must be greater than 2 chars', category="error")
elif password1 != password2:
flash('Passwords don't match', category="error")
elif len(password1) < 7:
flash('Password too short, atleast 7 chars', category="error")
else:
password = request.form.get('password1')
hashed_password = generate_password_hash(password)
new_lecturer = Lecturer_review(username=username, lastname=lastname, title=title, department=department, password=hashed_password)
db.session.add(new_lecturer)
db.session.commit()
registration_success()
flash('Account Created Successfully!', category="success")
form = 'lecturerform'
return redirect(url_for('registration_bp.registration_success'))
elif account_type == 'caoform':
# Code for cao registration
username = request.form.get('username')
lastname = request.form.get('lastname')
title = request.form.get('title')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
# Check if username already exists
existing_cao = Cao.query.filter_by(username=username).first()
if existing_cao:
flash('Username already exists', category='error')
if len(firstname) < 3:
flash('Firstname must be greater than 2 chars', category="error")
elif len(lastname) < 3:
flash('Lastname must be greater than 2 chars', category="error")
elif password1 != password2:
flash('Passwords don't match', category="error")
elif len(password1) < 7:
flash('Password too short, at least 7 chars', category="error")
else:
password = request.form.get('password1')
hashed_password = generate_password_hash(password)
new_cao = Cao_review(username=username, lastname=lastname, title=title , password=hashed_password)
db.session.add(new_cao)
db.session.commit()
flash('Account Created Successfully!', category="success")
form = 'caoform'
return redirect(url_for('registration_bp.registration_success'))
return render_template('register.html', form=form)
app.register_blueprint(registration_bp, methods=['GET', 'POST'])
if __name__ =="__main__":
app.run('127.0.0.1', debug=True)
This is the route for the register page. i made sure the names of my forms match, theres also a showform function written with javascript but that works well hence why its not in this code. I want to add the models for my tables too but i cant. The issue mostly is the page load, when i fill the forms and press the submit button, the page just reloads instead of redirecting me to the registration_success page or at least show the flash messages. So i suspect the function isnt working as expected, but just cant figure why, remember post 200 is displaying in the terminal whenever i press the submit button.
{% include '_flashes.html' %}
<div class="customboot">
<div class="container d-flex justify-content-center align-centent">
<div class="customselect">
<div class="form-floating">
<select class="form-select" id="register-selector" onchange="showForm()">
<option value="studentform">Student</option>
<option value="lecturerform" >Lecturer</option>
<option value="caoform">CA-Officer</option>
</select>
</div>
</div>
<form name="studentform" class="border shadow p-3 rounded" method="POST", id="studentform" action="{{ url_for('registration_bp.register') }}">
<h4>Student Registration form</h4>
<div class="mb-3">
<input type="text"
class="form-control"
name="firstname"
id="firstname1"
placeholder="First Name">
</div>
<div class="mb-3">
<input type="text"
class="form-control"
name="lastname"
id="lastname1"
placeholder="Last Name">
</div>
<div class="mb-3">
<input type="text"
class="form-control"
name="matricno"
id="matricno"
placeholder="ABC/1234/12/123">
</div>
<div class="mb-3">
<select id="sex1" name="sex">
<option disabled selected value="">Sex</option>
<option value="Male">male</option>
<option value="Female">female</option>
</select>
</div>
<div class="mb-3">
<input type="text"
class="form-control"
name="department"
id="departmemt1"
placeholder="Department">
</div>
<div class="mb-3">
<select id="dorm" name="dorm">
<option disabled selected value="">Dorm</option>
<option value="kaaf1up">KAAF1 Up</option>
<option value="kaaf1down">KAAF1 Down</option>
<option value="kaaf2up">KAAF2 Up</option>
<option value="kaaf2down">KAAF2 Down</option>
<option value="lagcen-up">Lagos Central Up</option>
<option value="lagcen-down">Lagos Central Down</option>
</select>
</div>
<div class="mb-3">
<input type="password"
class="form-control"
name="password1"
id="password1"
placeholder="Password">
</div>
<div class="mb-3">
<input type="password"
class="form-control"
name="password2"
id="password2"
placeholder="Confirm Password">
</div>
<button type="submit"
class="btn btn-primary">Submit</button>
</form>
<form name="lecturerform" class="border shadow p-3 rounded" method="POST" id="lecturerform" action="{{ url_for('registration_bp.register') }}" style="display: none;">
<h4>Lecturer Registration form</h4>
<div class="mb-3">
<input type="text"
class="form-control"
name="username"
id="username1"
placeholder="Username">
</div>
<div class="mb-3">
<input type="text"
class="form-control"
name="lastname"
id="lastname2"
placeholder="Last Name">
</div>
<div class="mb-3">
<select id="title1" name="title">
<option value="" disabled selected>Title</option>
<option value="Mr">Mr</option>
<option value="Miss/Mrs">Miss/Mrs</option>
</select>
</div>
<div class="mb-3">
<input type="text"
class="form-control"
name="department"
id="departmemt2"
placeholder="Department">
</div>
<div class="mb-3">
<input type="password"
class="form-control"
name="password1"
id="password3"
placeholder="Password">
</div>
<div class="mb-3">
<input type="password"
class="form-control"
name="password2"
id="password4"
placeholder="Confirm Password">
</div>
<button type="submit"
class="btn btn-primary">Submit</button>
</form>
<form name="caoform" class="border shadow p-3 rounded" method="POST" id="caoform" action="{{ url_for('registration_bp.register') }}" style="display: none;">
<div class="mb-3">
<h4>C-A Officer Registration form</h4>
<input type="text"
class="form-control"
name="username"
id="username2"
placeholder="Username">
</div>
<div class="mb-3">
<input type="text"
class="form-control"
name="lastname"
id="lastname3"
placeholder="Last Name">
</div>
<div class="mb-3">
<select id="title2" name="title">
<option selected disabled value="">Title</option>
<option value="Mr">Mr</option>
<option value="Miss/Mrs">Miss/Mrs</option>
</select>
</div>
<div class="mb-3">
<input type="password"
class="form-control"
name="password1"
id="password5"
placeholder="Password">
</div>
<div class="mb-3">
<input type="password"
class="form-control"
name="password2"
id="password6"
placeholder="Confirm Password">
</div>
<button type="submit"
class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</body>