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?
software-dev-7054cem/app.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
203 lines (173 sloc)
6.68 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
""" This is the app""" | |
from flask import Flask, render_template, request, make_response | |
from flaskext.mysql import MySQL | |
from pip._vendor import requests | |
from werkzeug.utils import redirect | |
from userfactory import UserFactory | |
from staff import Staff | |
from flask_mail import Mail, Message | |
from datetime import date | |
import json | |
mysql = MySQL() | |
# initializing a variable of Flask | |
app = Flask(__name__, template_folder="templates") | |
# MySQL configurations | |
app.config['MYSQL_DATABASE_USER'] = 'root' | |
app.config['MYSQL_DATABASE_PASSWORD'] = '' | |
app.config['MYSQL_DATABASE_DB'] = 'data_staff' | |
app.config['MYSQL_DATABASE_HOST'] = 'localhost' | |
# email set-up | |
app.config['MAIL_SERVER'] = 'smtp.gmail.com' | |
app.config['MAIL_PORT'] = 465 | |
app.config['MAIL_USERNAME'] = 'contact.forkswap@gmail.com' | |
app.config['MAIL_PASSWORD'] = 'hashedpassword' | |
app.config['MAIL_USE_TLS'] = False | |
app.config['MAIL_USE_SSL'] = True | |
mysql.init_app(app) | |
mail = Mail(app) | |
mail.init_app(app) | |
the_list = [] # a list of StaffProject objects | |
@app.route('/') | |
def landing_page(): | |
return render_template('index.html') | |
@app.route('/sign-in', methods=['GET', 'POST']) | |
def sign_in(): | |
if request.method == 'GET': | |
return render_template('signin.html') | |
else: | |
con = mysql.connect() # set up database connection | |
cur = con.cursor() | |
email = request.form['email'] | |
password = request.form['password'] | |
cur.execute('SELECT * FROM users WHERE email=%s AND password=%s', [email, password]) | |
print("retrieve the data from the database") | |
rows = cur.fetchall() | |
con.commit() | |
# Group of Four: FACTORY pattern | |
the_user = UserFactory().factory(rows[0][3]) | |
if len(rows) != 0: # necessary check for db non indexed / null values | |
if the_user.get_role() == "staff": # factory pattern continued | |
resp = redirect("/dashboard") | |
resp.set_cookie('loggedInToken', "staff") # instead of staff on main we would have a session token | |
return resp | |
elif the_user.get_role() == "user": # factory pattern continued | |
resp = make_response(render_template('index.html')) | |
resp.set_cookie('loggedInToken', "user") # instead of user on main we would have a session token | |
return resp | |
@app.route('/dashboard') | |
def dashboard(): | |
loggedInToken = request.cookies.get('loggedInToken') | |
if loggedInToken == "staff": | |
con = mysql.connect() # set up database connection | |
cur = con.cursor() | |
cur.execute('SELECT * FROM issues') | |
rows = cur.fetchall() | |
con.commit() | |
print(rows) | |
return render_template('dashboard.html', rows=rows) | |
else: | |
return render_template('signin.html') | |
@app.route('/report-issue') | |
def report_issue(): | |
loggedInToken = request.cookies.get('loggedInToken') | |
if loggedInToken == "user": | |
return render_template('report.html') | |
else: | |
return render_template('signin.html') | |
@app.route('/issue', methods=['POST']) | |
def issue(): | |
if request.method == 'POST': | |
con = mysql.connect() # set up database connection | |
cur = con.cursor() | |
title = request.form['title'] | |
query = request.form['query'] | |
description = request.form['description'] | |
postcode = request.form['postcode'] | |
today = date.today() | |
cur.execute('INSERT INTO issues (title, type_of_issue, description, postcode, time, userId)' | |
'VALUES( %s, %s, %s, %s, %s, %s)', | |
(title, query, description, postcode, today, 1)) | |
con.commit() | |
con.close() | |
return render_template('index.html') | |
@app.route('/issue/<id>', methods=['GET', 'POST']) | |
def issue_detailed(id): | |
if request.method == 'GET': | |
con = mysql.connect() # set up database connection | |
cur = con.cursor() | |
cur.execute('SELECT * FROM issues WHERE id=%s', [id]) | |
rows = cur.fetchall() | |
con.commit() | |
title = rows[0][1] | |
query = rows[0][2] | |
description = rows[0][3] | |
postcode = rows[0][4] | |
date_var = rows[0][5] | |
userid = rows[0][6] | |
solved = rows[0][7] | |
# API implementation | |
location = requests.get('https://geocode.search.hereapi.com/v1/geocode?apikey=9Ps1jjtBfqi8UrDg3V1tpKnAqbdLUI6KqhY3NU062K4&q='+postcode) | |
location = json.loads(location.text)["items"][0]["title"] | |
cur.execute('SELECT * FROM users WHERE id=%s', rows[0][6]) | |
rows = cur.fetchall() | |
cur.execute('SELECT * FROM messages WHERE issue_id=%s', id) | |
rows2 = cur.fetchall() | |
message = '' | |
if rows2: | |
message = rows2[0][2] | |
staff = Staff() | |
# GoF composite pattern ----------------------- | |
cur.execute('SELECT * FROM managers') | |
rows3 = cur.fetchall() | |
manager = Staff() | |
manager.set_email(rows3[0][1]) | |
staff.add_manager(manager) | |
manager_info = staff.get_managers_email() | |
con.close() | |
return render_template("report_details.html", title=title, query=query, description=description, location=location, date=date_var, phone=rows[0][4], id=id, solved=solved, userid = userid, messages = message, manager_info = manager_info) | |
else: | |
con = mysql.connect() | |
cur = con.cursor() | |
cur.execute('UPDATE issues SET solved=1 WHERE id=%s', (id)) | |
con.commit() | |
con.close() | |
return redirect("/dashboard") | |
@app.route('/delete/<id>', methods=['POST']) | |
def delete(id): | |
if request.method == 'POST': | |
try: | |
con = mysql.connect() | |
cur = con.cursor() | |
cur.execute('DELETE FROM Issues WHERE id=%s', id) | |
con.commit() | |
finally: | |
con.close() | |
return redirect("/dashboard") | |
@app.route('/email/<id>/<issue>', methods=['POST']) | |
def email(id, issue): | |
if request.method == 'POST': | |
con = mysql.connect() | |
cur = con.cursor() | |
emailText = request.form['emailText'] | |
cur.execute('SELECT * FROM users WHERE id=%s', id) | |
rows = cur.fetchall() | |
con.commit() | |
receiver = rows[0][1] | |
# Group of Four: MEDIATOR PATTERN | |
staff = Staff() | |
message = staff.send_message(emailText) | |
# Add message to db | |
cur.execute('INSERT INTO messages (issue_id, message)' | |
'VALUES( %s, %s)', | |
(issue, message)) | |
con.commit() | |
msg = Message("Update on Council Matter", | |
sender="contact.forkswap@gmail.com", | |
recipients=[receiver]) | |
msg.body = emailText; | |
mail.send(msg) | |
con.close() | |
return redirect("/dashboard") | |
if __name__ == "__main__": | |
app.run() |