-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
270 lines (238 loc) · 11.7 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from fastapi import FastAPI, HTTPException, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import logging
from schemas import UserCreate, GetUser, DeleteUser, Login, SessionCookie, DrugReplacements, Favorite, UploadSnapshot
from userFunctions import create_user, get_user_data_by_name, get_user_data_by_id, delete_user
from loginFunctions import login, is_session_cookie_valid, logout
from replacmentsFunctions import replacements
from historyFunctions import getHistory, addToFavorites, removeFromFavorites, getFavorites
from s3Functions import upload_snapshot, decode_file
import pandas as pd
# Setup logging
logging.basicConfig(filename='backend.log', level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
app = FastAPI(docs_url="/documentation", redoc_url=None)
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Setup
@app.get("/api/test")
async def test():
logging.info("Accessed the test endpoint")
return "Hello World!"
# Hello World Call
@app.get("/")
async def hello_world():
logging.info("Accessed the root endpoint")
return {"about": "Welcome to PAPO API! -- See /documentation for more details"}
# User Calls (CRUD Operations)
@app.post("/user/create") # Creating a new user
async def create_new_user(user: UserCreate):
try:
create_user(
user.username,
user.password,
user.center,
user.permission,
user.employeeid,
user.islocked
)
logging.info(f"User created: {user.username}")
return {"message": "User created successfully."}
except Exception as e:
logging.error(f"Error creating user {user.username}: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
@app.get("/user/get") # Getting user by ID or username
async def get_user(get_user: GetUser):
try:
if get_user.username:
user_data = get_user_data_by_name(get_user.username)
if user_data:
logging.info(f"Retrieved user by username: {get_user.username}")
return user_data
else:
logging.warning(f"User not found by username: {get_user.username}")
raise HTTPException(status_code=404, detail="User not found.")
elif get_user.id:
user_data = get_user_data_by_id(get_user.id)
if user_data:
logging.info(f"Retrieved user by ID: {get_user.id}")
return user_data
else:
logging.warning(f"User not found by ID: {get_user.id}")
raise HTTPException(status_code=404, detail="User not found.")
else:
logging.error("Invalid request for getting user")
raise HTTPException(status_code=400, detail="Invalid request.")
except Exception as e:
logging.error(f"Error retrieving user: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
@app.delete("/user/delete") # Deleting a user by ID
async def delete_user_endpoint(delete_user_request: DeleteUser):
try:
user_id = delete_user_request.id
if user_id:
user_data = get_user_data_by_id(user_id)
if user_data:
delete_user(user_id) # Assuming you have a function to delete a user by ID
logging.info(f"User deleted successfully: {user_id}")
return {"message": "User deleted successfully."}
else:
logging.warning(f"User not found for deletion: {user_id}")
raise HTTPException(status_code=404, detail="User not found.")
else:
logging.error("Invalid request for deleting user")
raise HTTPException(status_code=400, detail="Invalid request.")
except Exception as e:
logging.error(f"Error deleting user: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
# Login Calls
@app.post("/session/login") # Login function
async def user_login(login_data: Login):
try:
login_result = login(login_data.username, login_data.password)
if login_result is not None:
session_cookie, timestamp = login_result
logging.info(f"User logged in: {login_data.username}")
return {"session_cookie": session_cookie, "timestamp": timestamp}
else:
logging.warning(f"Invalid login attempt: {login_data.username}")
raise HTTPException(status_code=401, detail="Invalid credentials.")
except Exception as e:
logging.error(f"Error during login: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
@app.post("/session/is_session_cookie_valid") # Check if the session cookie is valid
async def check_session_cookie(session_cookie: SessionCookie):
if is_session_cookie_valid(session_cookie.session_cookie):
logging.info("Session cookie validated successfully")
return {"message": "Session cookie is valid."}
else:
logging.warning("Invalid or expired session cookie")
raise HTTPException(status_code=401, detail="Session cookie is invalid or expired.")
@app.post("/session/logout") # Logout function
async def user_logout(session_cookie: SessionCookie):
try:
if is_session_cookie_valid(session_cookie.session_cookie):
logout(session_cookie.session_cookie)
logging.info("User logged out successfully")
return {"message": "User logged out successfully."}
else:
logging.warning("Attempted logout with invalid or expired session cookie")
raise HTTPException(status_code=401, detail="Session cookie is invalid or expired.")
except Exception as e:
logging.error(f"Error during logout: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
# Drug Calls
@app.post("/drugs/replacements") # Upload a file
async def get_drug_replacements(drug_replacements: DrugReplacements):
try:
if is_session_cookie_valid(drug_replacements.session_cookie):
result = replacements(drug_replacements.session_cookie,
drug_replacements.drugid,
drug_replacements.isMultiple,
drug_replacements.w1,
drug_replacements.w2,
drug_replacements.w3)
logging.info("Drug replacements retrieved successfully")
return result
else:
logging.warning("Invalid or expired session cookie for drug replacements")
raise HTTPException(status_code=401, detail="Session cookie is invalid or expired.")
except Exception as e:
logging.error(f"Error retrieving drug replacements: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
# History Calls
@app.post("/history/get") # Get the history of a user
async def get_user_history(session_cookie: SessionCookie):
try:
if is_session_cookie_valid(session_cookie.session_cookie):
history = getHistory(session_cookie.session_cookie)
logging.info("User history retrieved successfully")
return history
else:
logging.warning("Invalid or expired session cookie for getting history")
raise HTTPException(status_code=401, detail="Session cookie is invalid or expired.")
except Exception as e:
logging.error(f"Error retrieving user history: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
@app.post("/history/add_to_favorites") # Add a history item to favorites
async def add_to_favorites(favorite_data: Favorite):
try:
if is_session_cookie_valid(favorite_data.session_cookie):
addToFavorites(favorite_data.session_cookie, favorite_data.history_id)
logging.info("Item added to favorites successfully")
return {"message": "Item added to favorites."}
else:
logging.warning("Invalid or expired session cookie for adding to favorites")
raise HTTPException(status_code=401, detail="Session cookie is invalid or expired.")
except Exception as e:
logging.error(f"Error adding item to favorites: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
@app.post("/history/remove_from_favorites") # Remove a history item from favorites
async def remove_from_favorites(favorite_data: Favorite):
try:
if is_session_cookie_valid(favorite_data.session_cookie):
removeFromFavorites(favorite_data.session_cookie, favorite_data.history_id)
logging.info("Item removed from favorites successfully")
return {"message": "Item removed from favorites."}
else:
logging.warning("Invalid or expired session cookie for removing from favorites")
raise HTTPException(status_code=401, detail="Session cookie is invalid or expired.")
except Exception as e:
logging.error(f"Error removing item from favorites: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
@app.post("/history/get_favorites") # Get the favorites of a user
async def get_user_favorites(session_cookie: SessionCookie):
try:
if is_session_cookie_valid(session_cookie.session_cookie):
favorites = getFavorites(session_cookie.session_cookie)
logging.info("User favorites retrieved successfully")
return favorites
else:
logging.warning("Invalid or expired session cookie for getting favorites")
raise HTTPException(status_code=401, detail="Session cookie is invalid or expired.")
except Exception as e:
logging.error(f"Error retrieving user favorites: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
# Snapshot Calls
@app.post("/snapshot/upload") # Upload a snapshot
async def upload_snapshot_endpoint(snapshot: UploadSnapshot):
try:
if is_session_cookie_valid(snapshot.session_cookie):
# Decode the base64 string and save the snapshot
decode_file(snapshot.file)
# Upload the snapshot
upload_snapshot("snapshot.csv")
logging.info("Snapshot uploaded successfully")
return {"message": "Snapshot uploaded successfully."}
else:
logging.warning("Invalid or expired session cookie for uploading snapshot")
raise HTTPException(status_code=401, detail="Session cookie is invalid or expired.")
except Exception as e:
logging.error(f"Error uploading snapshot: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
@app.post("/snapshot/upload2")
async def upload_file(file: UploadFile = File(...)):
if not file.filename.endswith(".csv"):
raise HTTPException(status_code=400, detail="Invalid file type, expected CSV")
try:
# Read the content of the file into a DataFrame
df = pd.read_csv(file.file)
# Define the path to save the file
save_path = f"./{file.filename}"
# Save the CSV file to the current directory
df.to_csv(save_path, index=False)
return {"message": "File uploaded successfully", "filename": file.filename}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
finally:
await file.close()
# Run the app
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
# uvicorn.run(app, host="127.0.0.1", port=8000)