Commit 9911eec3 authored by kaveena's avatar kaveena

Add UI Floder

parent 144fef40
from flask import Flask, render_template, request, flash, redirect, url_for, session
from flask_mysqldb import MySQL
import yaml
from functools import wraps
from wtforms import Form, StringField, validators # TextAreaField, PasswordField, DateTimeField
app = Flask(__name__)
db = yaml.load(open('db.yaml'))
app.config['MYSQL_HOST'] = db['mysql_host']
app.config['MYSQL_USER'] = db['mysql_user']
app.config['MYSQL_PASSWORD'] = db['mysql_password']
app.config['MYSQL_DB'] = db['mysql_db']
mysql= MySQL(app)
# @app.route('/', methods=['GET', 'POST'])
# def index():
# if request.method == 'POST':
# userDetails = request.form
# name = userDetails['name']
# email = userDetails['email']
# cur = mysql.connection.cursor()
# cur.execute("INSERT INTO users(name, email) VALUES(%s, %s)", (name, email))
# mysql.connection.commit()
# cur.close()
# return 'Success'
# return render_template('index.html')
# @app.route('/users')
# def users():
# cur = mysql.connection.cursor()
# resultValue = cur.execute("SELECT * FROM users")
# if resultValue > 0:
# userDetails = cur.fetchall()
# return render_template('users.html', userDetails = userDetails)
# def Extract(lst):
# return list(list(zip(*lst))[0])
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please login', 'danger')
return redirect(url_for('login'))
return wrap
class AddTruckForm(Form):
vin = StringField('userName', [validators.Length(min=3, max=20)])
location = StringField('password', [validators.Length(min=3, max=20)])
@app.route('/')
def index():
cur1 = mysql.connection.cursor()
cur2 = mysql.connection.cursor()
resultValue1 = cur1.execute("SELECT * FROM users")
resultValue2 = cur2.execute("SELECT id, type, inDate FROM users WHERE isExit = 1")
if resultValue1 > 0 or resultValue2 > 0:
userDetails = cur1.fetchall()
userDetails2 = cur2.fetchall()
# name = (list(list(zip(*userDetails))[0]))
# mail = (list(list(zip(*userDetails))[1]))
# userDetails = [name, mail]
# print(userDetails)
return render_template('index.html', userDetails = userDetails, userDetails2 = userDetails2)
@app.route('/edit_pass', methods=['GET', 'POST'])
@is_logged_in
def edit_pass():
session['is_admin'] = False
form = AddTruckForm(request.form)
if request.method == 'POST':
if request.form['submit'] == 'Cancel':
return redirect(url_for('login'))
# return render_template('admin.html')
elif request.form['submit'] == 'Edit':
# location = form.location.data
# cur = mysql.connection.cursor()
# cur.execute("""UPDATE Truck SET truckLocation='{}' WHERE VIN='{}'""".format(location, id))
# mysql.connection.commit()
# cur.close()
# flash('Truck VIN {} location has been updated to {}'.format(id, location), 'success')
return redirect(url_for('login'))
return render_template('edit_pass.html', form=form)
@app.route('/edit_price', methods=['GET', 'POST'])
@is_logged_in
def edit_price():
session['is_admin'] = False
form = AddTruckForm(request.form)
if request.method == 'POST':
if request.form['submit'] == 'Cancel':
return redirect(url_for('login'))
# return render_template('admin.html')
elif request.form['submit'] == 'Edit':
# location = form.location.data
# cur = mysql.connection.cursor()
# cur.execute("""UPDATE Truck SET truckLocation='{}' WHERE VIN='{}'""".format(location, id))
# mysql.connection.commit()
# cur.close()
# flash('Truck VIN {} location has been updated to {}'.format(id, location), 'success')
return redirect(url_for('login'))
return render_template('edit_price.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username_candidate = request.form['username']
password_candidate = request.form['password']
cur = mysql.connection.cursor()
result = cur.execute("""SELECT * FROM admin WHERE userName='{}'""".format(username_candidate))
if result > 0:
data = cur.fetchone()
username = data[0]
password = data[1]
if password_candidate == password:
session['logged_in'] = True
session['username'] = username
flash("You are now logged in", "success")
cur = mysql.connection.cursor()
# resultValue = cur.execute("SELECT * FROM users")
resultValue = cur.execute("SELECT id, type, inDate FROM users WHERE isVIP = 1")
if resultValue > 0:
session['is_admin'] = True
userDetails = cur.fetchall()
return render_template('admin.html', userDetails = userDetails)
else:
error = 'Invalid login'
return render_template('login.html', error=error)
cur.close()
else:
error = 'Username not found'
return render_template('login.html', error=error)
return render_template('login.html')
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('index'))
@app.route('/admin')
@is_logged_in
def admin():
session['is_admin'] = True
cur = mysql.connection.cursor()
# resultValue = cur.execute("SELECT * FROM users")
resultValue = cur.execute("SELECT id, type, inDate FROM users WHERE isVIP = 1")
userDetails = cur.fetchall()
return render_template('admin.html', userDetails = userDetails)
if __name__ == '__main__':
app.secret_key = '12'
app.run(host='0.0.0.0', port=8080)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment