-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
263 lines (211 loc) Β· 7.58 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
# -*- coding: utf-8 -*-
"""A FastAPI application on Cloud Run"""
import base64
import json
from datetime import date, timedelta
from os import environ
import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
import uvicorn
from fastapi import Depends, FastAPI, Header, HTTPException, Query
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import ORJSONResponse
from pydantic import conint, constr
from typing import Optional
from sqlalchemy.orm import Session
from starlette.middleware.cors import CORSMiddleware
from app.resources.strings import NO_SCOPE, NO_X_SCOPE, NO_CONTACT, NO_CONTENT
from app.crud import (
contact,
elections,
enmarche,
jemengage,
mail_campaign,
)
from app.database import SessionLocal
from app.schemas import schemas
# profiling
# from fastapi_profiler.profiler_middleware import PyInstrumentProfilerMiddleware
sentry_sdk.init(dsn="https://[email protected]/5890683")
app = FastAPI(
title="API pour le CRM de LaREM",
description="GET uniquements pour rΓ©cupΓ©rer les donnΓ©es des contacts de notre base",
version="1.0.0",
)
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(SentryAsgiMiddleware)
# app.add_middleware(PyInstrumentProfilerMiddleware)
# Constant VAR
MAX_HISTORY = 30
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
async def get_scopes(
scope: str, X_Scope: str = Header(None), db: Session = Depends(get_db)
) -> dict:
if scope is None:
raise HTTPException(status_code=400, detail=NO_SCOPE)
if X_Scope is None:
raise HTTPException(status_code=400, detail=NO_X_SCOPE)
try:
scope = enmarche.decode_scopes(db, X_Scope)
except base64.binascii.Error as err:
raise HTTPException(status_code=422, detail=f"Could not decode scope - {err}")
except json.decoder.JSONDecodeError as err:
raise HTTPException(status_code=422, detail=f"Could not decode scope - {err}")
return scope
@app.get("/")
async def home():
"""Message d'accueil"""
return {"message": "Welcome to building RESTful APIs with FastAPI"}
@app.get("/contacts", response_class=ORJSONResponse)
async def read_contacts(
selected_scope: dict = Depends(get_scopes), db: Session = Depends(get_db)
):
try:
contacts = contact.get_contacts(db, selected_scope)
except BaseException:
raise HTTPException(status_code=204, detail=NO_CONTACT)
return contacts
@app.get("/contacts-v01", response_class=ORJSONResponse)
async def read_contacts_v01(
selected_scope: dict = Depends(get_scopes),
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
):
try:
contacts = contact.get_contacts_v01(db, selected_scope, skip, limit)
except BaseException:
raise HTTPException(status_code=204, detail=NO_CONTACT)
return contacts
@app.get("/contacts-v02", response_class=ORJSONResponse)
async def read_contacts_v02(
selected_scope: dict = Depends(get_scopes),
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
q: list = Query([]),
):
try:
contacts = contact.get_contacts_v02(db, selected_scope, skip, limit, q)
except BaseException:
raise HTTPException(status_code=204, detail=NO_CONTACT)
return contacts
@app.get("/adherents", response_class=ORJSONResponse)
async def get_adherents(
selected_scope: dict = Depends(get_scopes), db: Session = Depends(get_db)
):
return contact.get_number_of_contacts(db, selected_scope)
@app.get("/jemengage/downloads", response_class=ORJSONResponse)
async def jemengage_downloads(
selected_scope: dict = Depends(get_scopes), db: Session = Depends(get_db)
):
res = jemengage.get_downloads(db, selected_scope)
if res.empty:
raise HTTPException(status_code=204, detail=NO_CONTENT)
total = int(res.unique_user.sum())
res = res.to_json(orient="records")
return {"totalDownloads": total, "downloads": json.loads(res)}
@app.get("/jemengage/users", response_class=ORJSONResponse)
async def jemengage_users(
selected_scope: dict = Depends(get_scopes), db: Session = Depends(get_db)
):
res = jemengage.get_users(db, selected_scope)
if res.empty:
raise HTTPException(status_code=204, detail=NO_CONTENT)
total = int(res.unique_user.sum())
res = res.to_json(orient="records")
return {"totalUsers": total, "users": json.loads(res)}
@app.get("/jemengage/survey", response_class=ORJSONResponse, response_model=schemas.JemarcheDataSurveyOut)
async def jemengage_survey(
max_history: Optional[conint(ge=1)] = MAX_HISTORY,
survey_uuid: Optional[constr(min_length=36, max_length=36)] = None,
selected_scope: dict = Depends(get_scopes),
db: Session = Depends(get_db)
):
return jemengage.get_survey(db, selected_scope, max_history, survey_uuid)
@app.get("/mailCampaign/reports", response_class=ORJSONResponse)
async def mail_reports(
max_history: Optional[conint(ge=1)] = MAX_HISTORY,
selected_scope: dict = Depends(get_scopes),
db: Session = Depends(get_db)
):
return [
await mail_campaign.get_campaign_reports(
db, zone, max_history, selected_scope["code"]
)
for zone in selected_scope["zones"]
]
@app.get("/mailCampaign/reportsRatios", response_class=ORJSONResponse)
async def mail_ratios(
max_history: Optional[conint(ge=1)] = MAX_HISTORY,
selected_scope: dict = Depends(get_scopes),
db: Session = Depends(get_db)
):
result = await mail_campaign.get_mail_ratios(
db, selected_scope['zones'], max_history, selected_scope['code']
)
return {
"zones": [zone.name for zone in selected_scope["zones"]],
"since": (date.today() - timedelta(days=max_history)).strftime("%Y-%m-%dT%H:%M:%S"),
**result,
}
@app.get("/election/participation", response_class=ORJSONResponse)
async def election_participation(
election: elections.ELECTION,
maillage: elections.DIVISION,
code_zone: constr(min_length=1),
tour: conint(ge=1, le=2) = 1,
selected_scope: dict = Depends(get_scopes),
db: Session = Depends(get_db),
):
res = elections.get_participation(
db, selected_scope, election, tour, maillage, code_zone
)
if res.empty:
return []
res = res.to_json(orient="records")
return json.loads(res)
@app.get("/election/results", response_class=ORJSONResponse)
async def election_results(
election: elections.ELECTION,
maillage: elections.DIVISION,
code_zone: constr(min_length=1),
tour: conint(ge=1, le=2) = 1,
selected_scope: dict = Depends(get_scopes),
db: Session = Depends(get_db),
):
res = elections.get_results(db, selected_scope, election, tour, maillage, code_zone)
if res.empty:
return []
res = res.to_json(orient="records")
return json.loads(res)
@app.get("/election/density", response_class=ORJSONResponse)
async def nuanceResults(
election: elections.ELECTION,
maillage: elections.DIVISION,
nuance_liste: constr(min_length=1),
tour: conint(ge=1, le=2) = 1,
selected_scope: dict = Depends(get_scopes),
db: Session = Depends(get_db),
):
res = elections.get_density(
db, selected_scope, election, tour, maillage, nuance_liste
)
if res.empty:
return []
res = res.to_json(orient="records")
return json.loads(res)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=int(environ.get("PORT", 8080)))