-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
153 lines (128 loc) · 4.8 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
from flask import Flask,request
from flask import jsonify
import firebase_admin
from firebase_admin import credentials,firestore
from flask_cors import CORS,cross_origin
import json
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
cred = credentials.Certificate("serviceAccount.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
product_ref = db.collection('products')
@app.route('/')
def index():
response = jsonify("Hello World")
return response
contractors = db.collection('contractors')
@app.route('/signup',methods=['POST'])
@cross_origin()
def sign_up():
data = request.json
print(data)
try:
contractorEmail = data['email']
a = contractors.document(contractorEmail).get().to_dict()
if a != None:
return {"success":False,"details":"Email already exists"}, 400
contractors.document(contractorEmail).set(data)
response = jsonify({"success":True})
return response
except Exception as e:
return f"An Error Occured: {e}",404
@app.route('/login',methods=['GET','POST'])
@cross_origin()
def login():
data = request.json
try:
email = data['email_id']
contractor = contractors.document(email).get().to_dict()
print(contractor)
if contractor == None:
return {"success":False,"details":"Vendor does not exist"}, 404
else:
actualPassword = contractor['password']
sentPassword = data['pass']
if actualPassword == sentPassword:
response = jsonify({"success":True,"email":contractor['email'],"address":contractor['address'],"name":contractor['username']})
return response
else:
response = jsonify({"success":False, "details":"Invalid password"})
return response
except Exception as e:
return f"An Error Occured: {e}"
@app.route('/getAllLabourers',methods=['GET'])
@cross_origin()
def getAllLabourers():
args = request.args
labourers = db.collection('labourers')
try:
all_labourers = [labourer.to_dict() for labourer in labourers.stream()]
response = jsonify(all_labourers)
return response , 200
except Exception as e:
return f"An Error Occured: {e}", 500
@app.route('/getALabourer')
@cross_origin()
def specificOrder():
labourerId = request.args['labourerId']
labourers = db.collection('labourers')
try:
labourer = labourers.document(labourerId).get().to_dict()
response = jsonify(labourer)
return response, 200
except Exception as e:
return f"An Error Occured: {e}"
workDict = {'1':"Plumbing",'2':"Carpenter",'3':"Daily Wage Worker",'4':"House hold worker"}
@app.route('/getPlumbers',methods=['GET'])
def getPlumbers():
args = request.args
labourers = db.collection('labourers')
try:
all_labourers = [labourer.to_dict() for labourer in labourers.stream() if labourer.to_dict()['workId'] == 1]
response = jsonify(all_labourers)
return response , 200
except Exception as e:
return f"An Error Occured: {e}", 500
@app.route('/getCarpenters',methods=['GET'])
def getCarpenters():
args = request.args
labourers = db.collection('labourers')
try:
all_labourers = [labourer.to_dict() for labourer in labourers.stream() if labourer.to_dict()['workId'] == 2]
response = jsonify(all_labourers)
return response , 200
except Exception as e:
return f"An Error Occured: {e}", 500
@app.route('/getDailyWage',methods=['GET'])
def getDailyWage():
args = request.args
labourers = db.collection('labourers')
try:
all_labourers = [labourer.to_dict() for labourer in labourers.stream() if labourer.to_dict()['workId'] == 3]
response = jsonify(all_labourers)
return response , 200
except Exception as e:
return f"An Error Occured: {e}", 500
@app.route('/getHomeWorkers',methods=['GET'])
def getHomeWorkers():
args = request.args
labourers = db.collection('labourers')
try:
all_labourers = [labourer.to_dict() for labourer in labourers.stream() if labourer.to_dict()['workId'] == 4]
response = jsonify(all_labourers)
return response , 200
except Exception as e:
return f"An Error Occured: {e}", 500
@app.route('/getWorkersInCity',methods=['GET'])
def getWorkersInCity():
city = request.args['city'].lower().strip()
labourers = db.collection('labourers')
try:
all_labourers = [labourer.to_dict() for labourer in labourers.stream() if labourer.to_dict()['place'] == city]
response = jsonify(all_labourers)
return response , 200
except Exception as e:
return f"An Error Occured: {e}", 500
if __name__ == '__main__':
app.run(debug=True,port=5000)