-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
183 lines (176 loc) · 6.17 KB
/
server.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
from flask import Flask, request, jsonify, render_template, send_from_directory, send_file
from pymongo import MongoClient
from datetime import datetime
from bson import ObjectId
import pymongo
import os
import ast
application = Flask(__name__)
client = MongoClient("ds151293.mlab.com:51293",51293)
db = client["finmap"]
db.authenticate("admin","finmap123");
# static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'public')
# @application.route('/accounts/{id}/purchases', methods=['GET'])
# def purchases():
# user_info = request.form.to_dict();
#
# #})
# })
@application.route('/search', methods=['POST'])
def search():
query_info = request.form.to_dict();
query = query_info["query"]
store_lst = []
#store name
#avg_spent
#lat
#long
for a in db.stores.find({"keywords":query}):
print(a)
del a["_id"]
store_lst.append(a)
return jsonify(store_lst)
@application.route('/transaction', methods=['POST'])
def transaction():
trans_info = request.form.to_dict();
trans_id = []
for a in db.transaction.find({"trans_name" : trans_info["storename"]}):
trans_id.append(a["_id"])
t = db.transactions.insert_one({
"storename" : trans_info["storename"],
"lat" : trans_info["lat"],
"long" : trans_info["long"],
"spent" : trans_info["spent"],
"user_id" : db.users.find_one({"first_name" : trans_info["first_name"], "last_name" : trans_info["last_name"]})["_id"],
"time" : trans_info["time"],
"day" : trans_info["day"]
})
id = t.inserted_id
print(id)
db.stores.update({"name" : trans_info["storename"]}, {"$push": {"transactions_ids" : str(id)}})
store = db.stores.find_one({"name" : trans_info["storename"]})
print("testing",store)
print(trans_info["storename"])
print(store["avg_spent"],float(store["avg_spent"]))
curr_sum = float(store["avg_spent"])*float(store["count"])
new_spent = float(trans_info["spent"])
new_count = (int(store["count"])+1)
new_avg = (curr_sum + new_spent)/new_count
db.stores.update({"name" : trans_info["storename"]},
{"$set":{
"avg_spent" : new_avg
}})
db.stores.update({"name": trans_info["storename"]}, {"$inc": {"count" : 1}})
return "true"
@application.route('/addstore', methods=['POST'])
def addstore():
store_info = request.form.to_dict();
keywords = store_info["keywords"]
db.stores.insert_one({
"name" : store_info["name"],
"avg_spent" : 0.0,
"lat" : store_info["lat"],
"long" : store_info["long"],
"count" : 0,
"keywords" : ast.literal_eval(keywords),
"transaction_ids" : []
})
return "true"
@application.route('/adduser', methods=['POST'])
def adduser():
user_info = request.form.to_dict();
print(user_info)
db.users.insert_one({
"first_name" : user_info["first_name"],
"last_name" : user_info["last_name"],
"age" : user_info["age"],
"income_level" : user_info["income_level"]
})
return "true"
#
#
# @application.route('/analytics', methods=['GET'])
# def analytics():
# # print(static_file_dir)
# print("hi")
# return render_template("analytics.html")
# @application.route('/getappointment', methods=['GET'])
# def get_appointment():
# lst = []
# for a in db.appointments.find({}):
# print(a["_id"])
# lst.append({
# "name" : db.users.find_one({"_id":ObjectId(a["user-id"])})["name"],
# "operations" : a["operations"],
# "start_time": a["start_time"],
# "end_time": a["end_time"],
# })
# return jsonify(lst)
# @application.route('/makeuser', methods=['POST'])
# def make_user():
# user_info = request.form.to_dict()
# db.users.insert_one({
# "name" : user_info["name"],
# "dob" : user_info["dob"],
# "email" : user_info["email"],
# "number" : user_info["number"]
# })
# return jsonify("Success!")
# @application.route('/users', methods=['GET'])
# def users():
# users = db.users.find({})
# u = []
# for user in users:
# u.append({
# "name" : user["name"],
# "dob" : user["dob"],
# "email" : user["email"],
# "number" : user["number"]
# })
# return jsonify(u)
# @application.route('/makeappointment', methods=['POST'])
# def make_appointment():
# appointment_info = request.form.to_dict()
# print(appointment_info)
# print(appointment_info["name"])
# user_id = db.users.find_one({"name":appointment_info["name"]})
# print("OK",user_id)
# if(user_id == None):
# usr = db.users.insert_one({
# "name" : appointment_info["name"],
# "dob" : appointment_info["dob"],
# "email" : appointment_info["email"],
# "number" : appointment_info["number"]
# })
# print(usr)
# user_id = db.users.find_one({"name" : appointment_info["name"]})["_id"]
# else:
# user_id = user_id["_id"]
# print(user_id)
# db.appointments.insert_one({
# "user-id" : user_id,
# "operations" : appointment_info["operations"],
# "start_time": appointment_info["start_time"],
# "end_time": appointment_info["end_time"],
# })
# return jsonify(appointment_info)
# # @application.route('/getwaittime', methods=['POST'])
# # def make_waittime():
# # waittimedetails = request.form.to_dict()
# # user_id = db.appointments.find_one("name":appointment_info["name"]})["_id"]
# # db.appointments.insert_one({
# # "waittime" : waittimedetails,
# # "operations" : appointment_info["operations"],
# # "start_time": appointment_info["operations"]
# # })
# # return jsonify(appointment_info)
# @application.route('/tardies', methods=['POST'])
# def list_tardies():
# tardy_info = request.form.to_dict()
# user_id = db.users.find_one({"name":tardy_info["name"]})
# db.usrs.update({"username":user_id},{"$inc":{
# "tardies":{ quantity: 1}
# }})
# return jsonify(tardy_info)
if __name__ == '__main__':
application.run(host='0.0.0.0',debug=True)