from flask import Flask, request, session, g, redirect, url_for, flash
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required
from wtforms import Form, StringField, PasswordField, SelectField, DateTimeField, DurationField
from wtforms.validators import DataRequired, Length, Email, EqualTo
from flask_sqlalchemy import SQLAlchemy
app = Flask(name)
app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘sqlite:///volunteer_database.db’
db = SQLAlchemy(app)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(128), nullable=False)
def set_password(self, password):
self.password_hash = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
def check_password(self, password):
return bcrypt.checkpw(password.encode('utf-8'), self.password_hash)
class Deployment(db.Model):
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String(32), nullable=False)
location = db.Column(db.String(32), nullable=False)
date = db.Column(DateTimeField, nullable=False)
time = db.Column(DurationField, nullable=False)
duration = db.Column(DurationField, nullable=False)
volunteers = db.relationship(‘User’, secondary=’deployment_users’)
def __repr__(self):
return f"Deployment({self.type}, {self.location}, {self.date}, {self.time}, {self.duration})"
class DeploymentUsers(db.Model):
user_id = db.Column(db.Integer, db.ForeignKey(‘user.id’))
deployment_id = db.Column(db.Integer, db.ForeignKey(‘deployment.id’))
confirmed = db.Column(db.Boolean, default=False)
@app.route(‘/’)
def index():
return render_template(‘index.html’)
@app.route(‘/login’, methods=[‘GET’, ‘POST’])
def login():
if request.method == ‘POST’:
username = request.form[‘username’]
password = request.form[‘password’]
user = User.query.filter_by(username=username).first()
if user is None:
flash(‘Invalid username’)
return redirect(url_for(‘login’))
if not user.check_password(password):
flash(‘Invalid password’)
return redirect(url_for(‘login’))
login_user(user)
flash(‘Logged in successfully’)
return redirect(url_for(‘home’))
return render_template(‘login.html’)
@app.route(‘/logout’)
def logout():
logout_user()
flash(‘Logged out successfully’)
return redirect(url_for(‘index’))
@app.route(‘/home’)
def home():
user = g.user
deployments = Deployment.query.all()
return render_template(‘home.html’, deployments=deployments)
@app.route(‘/deployments/’)
def deployment(deployment_id):
deployment = Deployment.query.get(deployment_id)
if deployment is None:
flash(‘Deployment not found’)
return redirect(url_for(‘home’))
return render_template(‘deployment.html’, deployment=deployment)
@app.route(‘/book/’, methods=[‘GET’, ‘POST’])
def book(deployment_id):
deployment = Deployment.query.get(deployment_id)