forked from answerquest/pmgsy_osm_comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_feedback.py
202 lines (145 loc) · 4.9 KB
/
api_feedback.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
# api_feedback
import os, time, json, requests
from typing import Optional, List
from pydantic import BaseModel
from fastapi.responses import FileResponse
from fastapi import HTTPException, Header, Path
import pandas as pd
from geosadak_api_launch import app
import commonfuncs as cf
import dbconnect
from globalvars import logIP, ipTracker
#########
# FUNCTIONS
##################
# APIs
@app.get("/API/registerSession", tags=["feedback"])
def registerSession(X_Forwarded_For: Optional[str] = Header(None) ):
logIP(X_Forwarded_For,'registerSession', limit=60)
return {'sessionId': cf.makeUID(10)}
class feedback1_payload(BaseModel):
ref_id: str
category: str
STATE_ID: str
DISTRICT_ID: str
BLOCK_ID: str
feedback1_submitter_name: str
feedback1_submitter_phone: str
feedback1_submitter_email: str
feedback1_changeName: Optional[str] = None
feedback1_changePopulation: Optional[int] = None
lat: Optional[float] = None
lon: Optional[float] = None
feedback1_comments: Optional[str] = None
@app.post("/API/feedback1", tags=["feedback"])
def feedback1(req: feedback1_payload, X_Forwarded_For: Optional[str] = Header(None)):
logIP(X_Forwarded_For,'feedback1', limit=60)
cf.logmessage("feedback1 POST API call")
if not X_Forwarded_For: X_Forwarded_For = 'localhost'
print(req)
iCols = []
iVals = []
uid = cf.makeUID(6)
iCols.append('uid')
iVals.append(f"'{uid}'")
iCols.append('ref_id')
iVals.append(f"'{req.ref_id}'")
iCols.append('category')
iVals.append(f"'{req.category}'")
iCols.append('ip')
iVals.append(f"'{X_Forwarded_For}'")
iCols.append('name')
iVals.append(f"'{req.feedback1_submitter_name}'")
iCols.append('phone')
iVals.append(f"'{req.feedback1_submitter_phone}'")
iCols.append('email')
iVals.append(f"'{req.feedback1_submitter_email}'")
iCols.append('state_id')
iVals.append(f"'{req.STATE_ID}'")
iCols.append('district_id')
iVals.append(f"'{req.DISTRICT_ID}'")
iCols.append('block_id')
iVals.append(f"'{req.BLOCK_ID}'")
iCols.append('ts')
iVals.append(f"CURRENT_TIMESTAMP")
if req.feedback1_comments:
iCols.append('comments')
iVals.append(f"'{req.feedback1_comments}'")
if req.feedback1_changeName:
iCols.append('name_change')
iVals.append(f"'{req.feedback1_changeName}'")
if req.feedback1_changePopulation:
iCols.append('pop_change')
iVals.append(f"{req.feedback1_changePopulation}")
if req.lat and req.lon:
iCols.append('geometry')
iVals.append(f"ST_GeomFromText('POINT({req.lon} {req.lat})',4326)")
i1 = f"""insert into feedback ({','.join(iCols)} ) values
({','.join(iVals)})
"""
print(i1)
dbconnect.execSQL(i1)
return {'status':'ok'}
@app.get("/API/ipcheck", tags=["feedback"])
def ipcheck():
print(ipTracker)
return ipTracker
########
class feedback2_payload(BaseModel):
ref_id: str
category: str
STATE_ID: str
DISTRICT_ID: str
BLOCK_ID: str
feedback2_submitter_name: str
feedback2_submitter_phone: str
feedback2_submitter_email: str
feedback2_name: Optional[str] = None
feedback2_population: Optional[int] = None
feedback2_comments: Optional[str] = None
@app.post("/API/feedback2", tags=["feedback"])
def feedback2(req: feedback2_payload, X_Forwarded_For: Optional[str] = Header(None)):
logIP(X_Forwarded_For,'feedback2', limit=60)
cf.logmessage("feedback2 POST API call")
if not X_Forwarded_For: X_Forwarded_For = 'localhost'
print(req)
iCols = []
iVals = []
uid = cf.makeUID(6)
iCols.append('uid')
iVals.append(f"'{uid}'")
iCols.append('ref_id')
iVals.append(f"'{req.ref_id}'")
iCols.append('category')
iVals.append(f"'{req.category}'")
iCols.append('ip')
iVals.append(f"'{X_Forwarded_For}'")
iCols.append('name')
iVals.append(f"'{req.feedback2_submitter_name}'")
iCols.append('phone')
iVals.append(f"'{req.feedback2_submitter_phone}'")
iCols.append('email')
iVals.append(f"'{req.feedback2_submitter_email}'")
iCols.append('state_id')
iVals.append(f"'{req.STATE_ID}'")
iCols.append('district_id')
iVals.append(f"'{req.DISTRICT_ID}'")
iCols.append('block_id')
iVals.append(f"'{req.BLOCK_ID}'")
iCols.append('ts')
iVals.append(f"CURRENT_TIMESTAMP")
if req.feedback2_comments:
iCols.append('comments')
iVals.append(f"'{req.feedback2_comments}'")
if req.feedback2_name:
iCols.append('name_change')
iVals.append(f"'{req.feedback2_name}'")
if req.feedback2_population:
iCols.append('pop_change')
iVals.append(f"{req.feedback2_population}")
i1 = f"""insert into feedback ({','.join(iCols)} ) values
({','.join(iVals)})
"""
print(i1)
dbconnect.execSQL(i1)
return {'status':'ok'}