-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
221 lines (197 loc) · 7.31 KB
/
Copy pathapp.py
File metadata and controls
221 lines (197 loc) · 7.31 KB
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
218
219
220
from flask import Flask, request, jsonify
from flask_cors import CORS
from flask_pymongo import PyMongo
from flask_mail import Mail
from bson import ObjectId
import json
app = Flask(__name__)
app.config.update(
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 465,
MAIL_USE_SSL = True,
MAIL_USERNAME = 'rudreshsingh999@gmail.com',
MAIL_PASSWORD = 'bpudlfeitrrdvxxx'
)
mail = Mail(app)
mongo1 = PyMongo(app, uri="mongodb://localhost:27017/user")
CORS(app)
db = mongo1.db.user
admin_db = mongo1.db.admin
col1 = mongo1.db.userstatus
@app.route('/sendmail/<id>', methods = ["POST"])
def sendmail(id):
subject = "Account {}"
body = "Your account is {}. Please contact admin for queries"
if id == "1":
subject = subject.format("deleted")
body = body.format("deleted")
print("yes")
if id == "2":
subject = subject.format("approved")
body = body.format("approved")
if id == "3":
subject = subject.format("updated")
body = body.format("updated")
msg = mail.send_message(
subject,
sender = 'rudreshsingh999@gmail.com',
recipients = [request.json['email']],
body = body
)
return 'Mail sent'
@app.route('/status', methods = ["GET", "POST"])
def status():
if request.method == "GET":
o = []
for i in col1.find():
o.append({
"_ID":str(ObjectId(i["_id"])),
"name": i["name"],
"vc": i["vc"],
"phone": i["phone"],
"address": i["address"],
"payment": i["payment"]
})
return jsonify(o)
elif request.method == "POST":
# print(request.json["details"][0]["name"])
id = col1.insert({
"name": request.json["name"],
"vc": request.json["vc"],
"phone": request.json["phone"],
"address": request.json["address"],
"payment": {"January": False,
"February": False,
"March": False,
"April": False,
"May": False,
"June": False,
"July": False,
"August": False,
"September": False,
"October": False,
"November": False,
"December": False}
})
return jsonify(str(ObjectId(id)))
@app.route('/<id>', methods=["DELETE"])
def delete1(id):
col1.delete_one({"_id":ObjectId(id)})
return jsonify({"message":"deleted"})
@app.route('/status/<id>', methods=["PUT"])
def put_all(id):
if request.method == "PUT":
col1.update({"_id":ObjectId(id)}, {"$set": {
"name" : request.json["name"],
"phone" : request.json["phone"],
"vc" : request.json["vc"],
"address": request.json["address"],
"payment": request.json["payment"]
}})
return jsonify({"message":"updated"})
@app.route('/status/payment/<id>', methods=["PUT"])
def put_payment(id):
if request.method == "PUT":
col1.update({"_id":ObjectId(id)}, {"$set": {
"payment": request.json["payment"],
"name": request.json["name"],
"vc": request.json["vc"]
}})
return jsonify({"message":"updated"})
# @app.route('/<id>', methods = ["PUT"])
# def add(id):
# if request.method == "PUT":
# res = col1.find_one({"_id": ObjectId(id)})
# list_add = res["details"]
# list_add.append(request.json)
# col1.update({"_id":ObjectId(id)}, {"$set" : {
# "details": list_add
# }})
# return jsonify({"message":"updated"})
@app.route('/', methods = ["GET", "POST"])
def getpost():
if request.method == "GET":
o = []
for i in admin_db.find():
o.append({
"_ID":str(ObjectId(i["_id"])),
"username": i["username"],
"password": i["password"],
"email": i["email"],
"customers": i["customers"]
})
return jsonify(o)
elif request.method == "POST":
print(request.json)
id = admin_db.insert({
"username":request.json["username"],
"password":request.json["password"],
"email":request.json["email"],
"customers":request.json["customers"]
})
return jsonify(str(ObjectId(id)))
@app.route('/<id>', methods = ["PUT"])
def addCustomer(id):
if request.method == "PUT":
res = admin_db.find_one({"_id": ObjectId(id)})
list_add = res["customers"]
list_add.append(request.json)
admin_db.update({"_id":ObjectId(id)}, {"$set" : {
"customers": list_add
}})
return jsonify({"message":"updated"})
@app.route('/signin_admin', methods = ['POST'])
def post():
for i in admin_db.find():
if(i["username"] == request.json["username"]):
if(i["password"] == request.json["password"]):
return jsonify({"message" : "success"})
else:
return jsonify({"message" : "password incorrect"})
return jsonify({"message" : "user not registered"})
@app.route('/signin_customer', methods = ['POST'])
def post_customer():
for i in admin_db.find():
list_customers = i["customers"]
for x in list_customers:
if(x["username"] == request.json["username"]):
if(x["password"] == request.json["password"]):
return jsonify({"message" : "success"})
else:
return jsonify({"message" : "password incorrect"})
return jsonify({"message" : "user not registered"})
@app.route('/<id>', methods=["DELETE"])
def delete(id):
admin_db.delete_one({"_id":ObjectId(id)})
return jsonify({"message":"deleted"})
@app.route('/<id1>/<username>', methods=["DELETE"])
def deleteCustomer(id1, username):
res = admin_db.find_one({"_id": ObjectId(id1)})
list_customers = res["customers"]
updated_res = list(filter(lambda i: i['username'] != username, list_customers))
admin_db.update({"_id":ObjectId(id1)}, {"$set" : {
"customers": updated_res
}})
return jsonify({"message":"deleted"})
@app.route('/getone/<id>/<username>', methods=["GET"])
def getone(id, username):
res = admin_db.find_one({"_id": ObjectId(id)})
list_customers = res["customers"]
for i in range(len(list_customers)):
if(list_customers[i].get("username") == username):
return {"id": list_customers[i]["id"], "username": username, "email": list_customers[i]["email"], "password": list_customers[i]["password"], "is_new": list_customers[i]["is_new"]}
@app.route('/<id>/<username>', methods = ["PUT"])
def updateCustomer(id, username):
if request.method == "PUT":
res = admin_db.find_one({"_id": ObjectId(id)})
list_add = res["customers"]
for i in range(len(list_add)):
if(list_add[i].get("username") == username):
list_add[i]["email"] = request.json["email"]
list_add[i]["password"] = request.json["password"]
list_add[i]["is_new"] = "0"
break
admin_db.update({"_id":ObjectId(id)}, {"$set" : {
"customers": list_add
}})
return jsonify({"message":"updated"})