forked from arjo129/CS2102Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
53 lines (44 loc) · 1.64 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from config import conn_string
from flask import Flask, render_template, request, redirect, send_from_directory, session, g
from modules.users import user_module, get_current_user
from modules.items import item_module
from modules.categories import category_module
import os
import psycopg2
app = Flask(__name__)
app.secret_key = os.urandom(12)
# add your modules as blueprints here:
app.register_blueprint(user_module)
app.register_blueprint(item_module)
app.register_blueprint(category_module)
def getItems(searchParams=None):
conn = psycopg2.connect(conn_string)
curr = conn.cursor()
if searchParams != None:
curr.execute(
"SELECT i.item_id, i.name, u.display_name, i.location "
"FROM items i, users u "
"WHERE i.owner = u.email AND "
"LOWER(i.name) LIKE LOWER(%s) OR LOWER(i.owner) LIKE LOWER(%s) OR LOWER(i.location) LIKE LOWER(%s)",
('%'+searchParams+'%',
'%'+searchParams+'%', '%'+searchParams+'%')
)
else:
curr.execute("SELECT i.item_id, i.name, u.display_name, i.location FROM items i, users u WHERE i.owner = u.email")
items = []
for item in curr:
items.append({"id": item[0], "name": item[1], "owner": item[2],
"location": item[3]})
return items
@app.before_request
def before_request():
if 'user' in session:
g.user = get_current_user()
else:
g.user = None
@app.route("/")
def index():
return render_template("index.jinja2", items=getItems(request.args.get("searchParams")))
@app.route('/static/<path:path>')
def send_js(path):
return send_from_directory('static', path)