-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
217 lines (171 loc) · 5.28 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
213
214
215
216
217
from flask import Flask, jsonify, request, render_template
from utility_functions import generate_unique_string
from database import Livedb
from sendemail import SendEmail
port = 5000 # Global parameter
db = {}
app = Flask(__name__)
@app.route("/")
def welcome():
return render_template("home.html")
@app.route("/sign_up", methods=["POST"])
def sign_up():
database = Livedb()
email = request.form.get("email")
user_code, success = database.insert_user(email)
sendemails = SendEmail()
send_status = sendemails.send_email(email, user_code)
if send_status:
message = "Email sent successfully to " + email
else:
message = "Error sending email to " + email
data = {"message": message, "created": send_status, "user_status": success}
database.close_connection()
print(data)
return jsonify(data)
@app.route("/db_status", methods=["GET"])
def db_status():
data = {"status": "Active"}
return jsonify(data)
@app.route("/user_status", methods=["POST"])
def user_status():
database = Livedb()
user = request.json.get("user_code")
exists, num_keys = database.user_exist(user)
data = {"user_status": exists}
database.update_last_used(user)
database.close_connection()
return jsonify(data)
@app.route("/insert_value", methods=["POST"])
def insert_value():
database = Livedb()
user = request.json.get("user_code")
key = request.json.get("key")
value = request.json.get("value")
if not user or not key or not value:
return jsonify(
{
"message": "Error: Missing required data (user_code, key, or value)",
"result": False,
}
)
exists, num_keys = database.user_exist(user)
if user not in db:
db[user] = {"code": user, "data": {}}
if len(db[user]["data"]) > num_keys and key not in db[user]["data"]:
return jsonify(
{
"message": "Error: Maximum number of keys are reached. Please delete some",
"result": False,
}
)
db[user]["data"][key] = value
database.update_last_used(user)
database.close_connection()
return jsonify({"result": True})
@app.route("/get_value", methods=["POST"])
def get_value():
user_code = request.json.get("user_code")
key = request.json.get("key")
if not user_code or not key:
return jsonify(
{
"message": "Error : Missing required data (user_code or key)",
"result": False,
}
)
if user_code not in db:
return jsonify(
{
"message": "Error : Not a single value pair found",
"result": False,
}
)
user_data = db[user_code]["data"]
value = user_data.get(key)
if value is None:
return jsonify(
{"message": "Error : Key not found for the user", "result": False}
)
return jsonify({"result": True})
@app.route("/delete_key", methods=["POST"])
def delete_key():
user_code = request.json.get("user_code")
key = request.json.get("key")
if not user_code or not key:
return jsonify(
{
"message": "Error : Missing required data (user_code or key)",
"result": False,
}
)
if user_code not in db:
return jsonify(
{
"message": "Error : There are no key value pairs for the user saved in DB",
"result": False,
}
)
user_data = db[user_code]["data"]
if key not in user_data:
return jsonify(
{
"message": "Key not found for the user",
"result": False,
}
)
del user_data[key]
return jsonify(
{
"result": True,
}
)
@app.route("/delete_user_keys", methods=["POST"])
def delete_user():
user_code = request.json.get("user_code")
if not user_code:
return jsonify(
{
"message": "Error : Missing required data (user_code)",
"result": False,
}
)
if user_code not in db:
return jsonify(
{
"message": "Error : There are no key value pairs for the user saved in DB",
"result": False,
}
)
del db[user_code] # Delete the user from the database
return jsonify(
{
"result": True,
}
)
@app.route("/listkeyvalues", methods=["POST"])
def list_key_values():
user_code = request.json.get("user_code")
if not user_code:
return jsonify(
{
"message": "Error : Missing required data (user_code)",
"result": False,
}
)
if user_code not in db:
return jsonify(
{
"message": "Error : There are no key value pairs for the user saved in DB",
"result": False,
}
)
user_data = db.get(user_code, {}).get("data", {})
key_value_pairs = list(user_data.items())
if len(key_value_pairs) > 0:
status = True
else:
status = False
return jsonify({"key_value_pairs": key_value_pairs, "result": status})
if __name__ == "__main__":
app.run(port=port)