-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
135 lines (120 loc) · 3.99 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from flask import Flask, redirect, url_for, request, render_template
from flask.globals import current_app
from map import getMap
import database
app = Flask(__name__)
current_user = ""
class History:
def __init__(self, _type, points, timestamp):
self.type = _type
self.points = points
self.timestamp = timestamp
#self.location = location
def compute_history(x):
print(x)
if (x[2] == "pick"):
return History(_type = "Picked up trash at", points = x[3], timestamp = str(x[1]))
return History(_type = "Reported trash at", points = x[3], timestamp = str(x[1]))
@app.route("/")
def hello_world():
return redirect('/home')
@app.route("/home", methods = ['GET', 'POST'])
def home():
global current_user
if request.method == 'POST':
if current_user == "":
return redirect('/login')
else:
return redirect('/user/'+current_user)
return getMap()
@app.route('/login', methods = ['GET', 'POST'])
def login():
global current_user
if request.method == 'GET':
if current_user:
return redirect('/user/'+current_user)
return render_template('login.html')
if request.method == 'POST':
uname = request.form['uname']
pword = request.form['pword']
result = database.log_in(uname, pword)
if result['code']:
return result['msg']
current_user = uname
return redirect('/home')
@app.route('/signup', methods = ['GET', 'POST'])
def signup():
if request.method == 'GET':
return render_template('signup.html')
if request.method == 'POST':
fname = request.form['fname']
lname = request.form['lname']
email = request.form['email']
uname = request.form['uname']
pword = request.form['pword']
result = database.add_user(uname, pword, fname + " " + lname, email, "")
if result['code']:
return result['msg']
return redirect('/login')
@app.route('/user/signout', methods = ['GET'])
def signout():
global current_user
current_user = ""
return redirect('/home')
@app.route('/user/<username>', methods = ['GET'])
def profile(username):
if request.method == 'GET':
# log_action("adela", "report", 1)
# log_action("adela", "pick", 20)
# add_user("adela", "adela", "Adela Vais", "+490668955844", "Arcisstraße 21, 80333 München")
user_data = database.get_user(username)
# print(user_data)
# history1 = History(_type = "Reported trash at", points = 10, location = "Arcisstrasse")
# history2 = History(_type = "Picked up trash at", points = 100, location = "Marienplatz")
history_data = database.get_history(username) # [history1, history2]
# print(history_data)
history = list(map(compute_history, history_data))
# history_points = map(lambda a: a.points, history)
return render_template(
'profile.html',
name = user_data[3],
# address = "Arcisstraße 21, 80333 München",
address = user_data[5],
email = user_data[4],
phone = user_data[4],
points = user_data[2], #reduce(lambda a,b: a + b, history_points, 0),
history = history,
leng = len(history),
username = username,
)
@app.route('/user/edit/<username>', methods = ['POST', 'GET'])
def edit_user_data(username):
if request.method == 'GET':
user_data = database.get_user(username)
# print(user_data)
return render_template(
'edit_profile.html',
name = user_data[3],
# address = "Arcisstraße 21, 80333 München",
address = user_data[5],
email = user_data[4],
phone = user_data,
username = username,
)
if request.method == 'POST':
name = request.form['name']
# print(name)
address = request.form['address']
# print(address)
email = request.form['email']
# print(email)
database.update_user_data(username=username, name=name, address=address, email=email)
return redirect(url_for('profile', username = username))
@app.route('/report', methods=['POST'])
def report():
lat = request.form['lat']
lon = request.form['lon']
database.log_active_request(lat, lon, "report", "trash")
if current_user:
database.log_action(current_user, "report", 1)
return redirect('/home')