-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
212 lines (171 loc) · 6.57 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from flask import Flask, render_template, request, redirect, jsonify, make_response
from flask_cors import CORS
import base64
import uuid
import bcrypt
import os
import json
import time
from db_access import *
app = Flask(__name__)
def generate_unique_filename():
# Generate a UUID and convert it to a string
unique_id = str(uuid.uuid4())
# Remove hyphens and return the unique filename
return unique_id.replace('-', '')
@app.route('/upload', methods=['POST', 'GET'])
def upload():
if (request.method == 'POST'):
req = request.form
if(req['user_id'] != ''):
imagesData = req['blob'].split(' ')
imagesData.pop()
imageName = []
for imageData in imagesData:
data = base64.b64decode(imageData)
uniqueName = generate_unique_filename()
imageName.append(uniqueName)
script_path = os.path.abspath(__file__)
app_directory = os.path.dirname(script_path)
static_directory = os.path.join(app_directory, "static/post_images/" + uniqueName + ".jpg")
with open(static_directory, 'wb') as f:
f.write(data)
lat = float(req['coordinates'].split(' ')[0])
lon = float(req['coordinates'].split(' ')[1])
dict = {
'description': req['desc'],
'waste_type': req['waste-category'] if req.get('waste-category') is not None else '',
'image': imageName,
'user_id': req['user_id'],
'location' : {"type": "Point", "coordinates": [lat, lon]}
}
print(dict)
storePost(dict)
return redirect('/feed')
return render_template('upload.html')
@app.route('/feed_old', methods=['POST', 'GET'])
def feed():
posts = getPost()
return render_template('feed_old.html', posts = posts)
@app.route('/feed', methods=['POST', 'GET'])
def feed2():
return render_template('feed.html')
@app.route('/load_feed', methods=['POST', 'GET'])
def load_feed():
time.sleep(0.2)
page = request.args.get('page')
order = request.args.get('order')
sort = request.args.get('sort')
print("Sort value = ", sort)
if(order == 'latest'):
print('\n\nLATEST\n\n')
posts = getPostsForFeed(page_no=page, sort=sort)
return json.dumps(posts, default=str)
elif(order == 'home'):
print('\n\nCLOSE to HOME\n\n')
uid = request.args.get('uid')
posts = getPostsForFeed(page_no=page, uid=uid, sort=sort)
return json.dumps(posts, default=str)
elif(order == 'location'):
print('\n\nCLOSE to CURRENT LOCATION\n\n')
lat = request.args.get('lat')
lon = request.args.get('lon')
posts = getPostsForFeed(page_no=page, location = [float(lat), float(lon)], sort=sort)
print("Location Posts - > ",posts)
return json.dumps(posts, default=str)
@app.route('/load_profile', methods=['POST', 'GET'])
def load_profile():
time.sleep(0.5)
page = request.args.get('page')
profile_id = request.args.get('profile_id')
posts = getPostsForProfile(page_no=page, profile_id = profile_id)
print(posts)
return json.dumps(posts, default=str)
@app.route('/post', methods=['POST', 'GET'])
def post():
if(request.method == 'GET'):
post_id = request.args.get('id')
post = getPost(post_id)
return render_template('post.html', post=post)
elif(request.method == 'POST'):
req = request.form
print(req)
if(req['user_id'] != ''):
imagesData = req['blob'].split(' ')
imagesData.pop()
imageName = []
for imageData in imagesData:
data = base64.b64decode(imageData)
uniqueName = generate_unique_filename()
imageName.append(uniqueName)
script_path = os.path.abspath(__file__)
app_directory = os.path.dirname(script_path)
static_directory = os.path.join(app_directory, "static/post_images/" + uniqueName + ".jpg")
with open(static_directory, 'wb') as f:
f.write(data)
dict = {
'description': req['desc'],
'image': imageName,
'user_id': req['user_id']
}
postResolved(req['post_id'], dict)
return redirect('/post?id='+str(req.get('post_id')))
@app.route('/userhome', methods=['POST', 'GET'])
def userhome():
uid = request.args.get('uid')
lat, lon = getUserHome(uid)
location = [lat, lon]
return location
@app.route('/about', methods=['POST', 'GET'])
def about():
return render_template('about.html', param = "HEllo")
@app.route('/login', methods=['POST', 'GET'])
def login():
if (request.method == 'POST'):
req = request.get_json()
print(req)
user_collection = fetchUserByEmail(req['email'])
if user_collection is not None:
print(user_collection)
entered_password = req['password'].encode('utf-8')
if bcrypt.checkpw(entered_password, user_collection['password']):
print("Password is correct!")
uid = str(user_collection['_id'])
return uid
# profile redirect
else:
print("Password is incorrect.")
return "wrong_password"
else:
print("No user with such email")
return "wrong_email"
return render_template('login.html')
@app.route('/register', methods=['POST', 'GET'])
def register():
if (request.method == 'POST'):
req = request.get_json()
print(req)
# if cookie then profile
password = req['password'].encode('utf-8')
salt = bcrypt.gensalt()
hashed_password = bcrypt.hashpw(password, salt)
dict = {
'name': req['name'],
'coordinates': req['coordinates'],
'email': req['email'],
'password': hashed_password
}
if registerDb(dict) is not None:
print("Email doesnt exist")
return "false"
else: #None
print("Email exists")
return "true"
return render_template('register.html')
@app.route('/profile', methods=['POST', 'GET'])
def profile():
uid = request.args.get('id')
user_info = fetchUserByUID(uid)
return render_template('profile.html', user_info = user_info)
if __name__ == '__main__':
app.run(port = '5000', debug=True)