Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Tour-Guide/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
205 lines (167 sloc)
7.73 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, url_for, request, session, g, jsonify | |
from werkzeug.utils import redirect | |
import os | |
import sqlite3 | |
from DbUserFactory import DbUserFactory | |
from DeleteEmployeeCommand import DeleteEmployeeCommand | |
from InsertCommand import InsertCommand | |
from InternationalCommand import InternationalCommand, InternationalForm | |
from database import get_database | |
from werkzeug.security import generate_password_hash, check_password_hash | |
from databaseFacade import DatabaseFacade | |
from updateemployeecommand import UpdateEmployeeCommand | |
from user import User | |
from flask import jsonify | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = os.urandom(24) | |
with app.app_context(): | |
db = get_database() | |
@app.teardown_appcontext | |
def close_database(error): | |
if hasattr(g, 'guideschedule_db'): | |
g.guideschedule_db.close() | |
# ensure that database connections are always properly closed and released when the application context is torn down. This can help prevent issues such as resource leaks and ensure that your application runs smoothly and reliably. | |
def get_current_user(): | |
user = None | |
if 'user' in session: | |
user = session['user'] | |
db = get_database() | |
user_cur = db.execute('select * from users where name = ?', [user]) | |
user = user_cur.fetchone() | |
return user | |
# which returns the currently logged-in user. | |
@app.before_request | |
def before_request(): | |
session['user'] = get_current_user() | |
# This means that the function will be called automatically before each request is processed by the Flask application. | |
@app.route('/') | |
def index(): | |
user = get_current_user() | |
return render_template('home.html', user=user) | |
@app.route('/login', methods=["POST", "GET"]) | |
def login(): | |
user = get_current_user() | |
if request.method == 'POST': | |
name = request.form['name'] | |
password = request.form['password'] | |
user_factory = DbUserFactory() | |
dbuser_cur = user_factory.get_connection().execute('select * from users where name = ?', [name]) | |
dbuser = dbuser_cur.fetchone() | |
if dbuser is None: | |
return render_template('login.html', loginerror='Invalid username or password'), 404 | |
elif not check_password_hash(dbuser[2], password): | |
return render_template('login.html', loginerror='Invalid username or password'), 404 | |
else: | |
session['username'] = dbuser[1] | |
return redirect(url_for('index')) | |
return render_template('login.html', user=user) | |
# Renders the login page. If the user submits the login form, it checks the credentials against the database and sets a session variable to indicate that the user is logged in. | |
@app.route('/register', methods=["POST", "GET"]) | |
def register(): | |
user = get_current_user() | |
if request.method == 'POST': | |
name = request.form['name'] | |
password = request.form['password'] | |
user_factory = DbUserFactory() | |
try: | |
user_factory.create_user(name, password) | |
except ValueError as e: | |
return render_template('register.html', registererror=str(e)), 404 | |
return redirect(url_for('login')) | |
return render_template('register.html', user=user) | |
# Renders the registration page. If the user submits the registration form, it creates a new user in the database | |
@app.route('/dashboard') | |
def dashboard(): | |
# Check if user is signed in | |
if 'user' not in session: | |
return redirect(url_for('login')) | |
user = get_current_user() | |
db = get_database() | |
sch_cur = db.execute('select * from sch') | |
allsch = sch_cur.fetchall() | |
return render_template('dashboard.html', user=user, allsch=allsch) | |
# Renders the dashboard page. If the user is not logged in, it redirects to the login page. Otherwise, it retrieves all scheduled services from the database and displays them on the page. | |
@app.route('/domestic', methods=["POST", "GET"]) | |
def domestic(): | |
user = get_current_user() | |
if request.method == "POST": | |
name = request.form['name'] | |
email = request.form['email'] | |
phone = request.form['phone'] | |
address = request.form['address'] | |
service = request.form['service'] | |
joining_date = request.form['joining_date'] | |
travel = request.form['travel'] | |
db = DatabaseFacade() | |
insert_command = InsertCommand(db, name, email, phone, address, service, joining_date, travel) | |
insert_command.execute() | |
return redirect(url_for('dashboard')) | |
return render_template('domestic.html', user=user) | |
# Renders the domestic service scheduling page. If the user submits the form, it inserts the data into the database. | |
@app.route('/international', methods=["POST", "GET"]) | |
def international(): | |
user = get_current_user() | |
if request.method == "POST": | |
name = request.form['name'] | |
email = request.form['email'] | |
phone = request.form['phone'] | |
address = request.form['address'] | |
service = request.form['service'] | |
joining_date = request.form['joining_date'] | |
travel = request.form['travel'] | |
db = get_database() | |
# Create a command and an invoker, and execute the command | |
command = InternationalCommand(name, email, phone, address, service, joining_date, travel, db) | |
form = InternationalForm(command) | |
form.submit() | |
return redirect(url_for('dashboard')) | |
return render_template('international.html', user=user) | |
# Renders the international service scheduling page. If the user submits the form, it creates an InternationalCommand object, which is then executed to insert the data into the database. | |
@app.route('/fetchone/<int:empid>') | |
def fetchone(empid): | |
user = get_current_user() | |
db = get_database() | |
sch_cur = db.execute('select * from sch where empid = ?', [empid]) | |
single_sch = sch_cur.fetchone() | |
return render_template('update.html', user=user, single_sch=single_sch) | |
# Retrieves a single scheduled service from the database and renders the update page with the data filled in. | |
@app.route('/update', methods=["POST", "GET"]) | |
def update(): | |
user = get_current_user() | |
if request.method == 'POST': | |
empid = request.form['empid'] | |
name = request.form['name'] | |
email = request.form['email'] | |
phone = request.form['phone'] | |
address = request.form['address'] | |
service = request.form['service'] | |
joining_date = request.form['joining_date'] | |
travel = request.form['travel'] | |
db = get_database() | |
command = UpdateEmployeeCommand(empid, name, email, phone, address, service, joining_date, travel, db) | |
command.execute() | |
return redirect(url_for('dashboard')) | |
return render_template('update.html', user=user) | |
# Updates a scheduled service in the database based on the data submitted in the form. | |
@app.route('/profileguide<int:empid>') | |
def profileguide(empid): | |
user = get_current_user() | |
db = get_database() | |
emp_cur = db.execute('select * from sch where empid = ?', [empid]) | |
guest_emp = emp_cur.fetchone() | |
return render_template('profileguide.html', user=user, guest_emp=guest_emp) | |
@app.route('/deleteemp/<int:empid>', methods=["GET", "POST"]) | |
def deleteemp(empid): | |
user = get_current_user() | |
if request.method == 'GET': | |
command = DeleteEmployeeCommand(empid) | |
command.execute() | |
return redirect(url_for('dashboard')) | |
return render_template('dashboard.html', user=user) | |
# Deletes a scheduled service from the database. | |
@app.route('/logout') | |
def logout(): | |
session.pop('user', None) | |
return render_template('home.html') | |
if __name__ == '__main__': | |
app.run(debug=True) |