Skip to content

Commit

Permalink
Merge pull request #219 from prefeitura-rio/feat/use-cpf-partition
Browse files Browse the repository at this point in the history
Using cpf partition field to cost optimized queries
  • Loading branch information
TanookiVerde committed Sep 2, 2024
2 parents 2ca1daa + a3f9111 commit 16ad1bb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
10 changes: 5 additions & 5 deletions app/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from fastapi.responses import StreamingResponse

from app.models import User
from app.types.frontend import LoginFormWith2FA
from app.types.pydantic_models import Token, Enable2FA
from app.utils import authenticate_user, generate_user_token
from app.security import TwoFactorAuth
Expand Down Expand Up @@ -46,7 +47,7 @@ async def login_without_2fa(

@router.post("/2fa/is-2fa-active/")
async def is_2fa_active(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
form_data: Annotated[LoginFormWith2FA, Depends()],
) -> bool:
user = await authenticate_user(form_data.username, form_data.password)
if not user:
Expand All @@ -61,8 +62,7 @@ async def is_2fa_active(

@router.post("/2fa/login/")
async def login_with_2fa(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
totp_code: str,
form_data: Annotated[LoginFormWith2FA, Depends()],
) -> Token:

user = await authenticate_user(form_data.username, form_data.password)
Expand All @@ -76,7 +76,7 @@ async def login_with_2fa(
secret_key = await TwoFactorAuth.get_or_create_secret_key(user.id)
two_factor_auth = TwoFactorAuth(user.id, secret_key)

is_valid = two_factor_auth.verify_totp_code(totp_code)
is_valid = two_factor_auth.verify_totp_code(form_data.totp_code)
if not is_valid:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down Expand Up @@ -107,7 +107,7 @@ async def enable_2fa(

@router.post("/2fa/generate-qrcode/")
async def generate_qrcode(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
form_data: Annotated[LoginFormWith2FA, Depends()],
) -> bytes:
current_user = await authenticate_user(form_data.username, form_data.password)
if not current_user:
Expand Down
6 changes: 3 additions & 3 deletions app/routers/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def get_patient_header(
f"""
SELECT *
FROM `{BIGQUERY_PROJECT}`.{BIGQUERY_PATIENT_HEADER_TABLE_ID}
WHERE cpf = '{cpf}'
WHERE cpf_particao = {cpf}
""",
from_file="/tmp/credentials.json",
)
Expand Down Expand Up @@ -88,7 +88,7 @@ async def get_patient_summary(
f"""
SELECT *
FROM `{BIGQUERY_PROJECT}`.{BIGQUERY_PATIENT_SUMMARY_TABLE_ID}
WHERE cpf = '{cpf}'
WHERE cpf_particao = {cpf}
""",
from_file="/tmp/credentials.json",
)
Expand Down Expand Up @@ -123,7 +123,7 @@ async def get_patient_encounters(
f"""
SELECT *
FROM `{BIGQUERY_PROJECT}`.{BIGQUERY_PATIENT_ENCOUNTERS_TABLE_ID}
WHERE cpf = '{cpf}' and exibicao.indicador = true
WHERE cpf_particao = {cpf} and exibicao.indicador = true
""",
from_file="/tmp/credentials.json",
)
Expand Down
22 changes: 19 additions & 3 deletions app/types/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
from typing import Optional, List
from pydantic import BaseModel

from fastapi.security import OAuth2PasswordRequestForm


class LoginFormWith2FA(OAuth2PasswordRequestForm):
def __init__(
self,
username: str,
password: str,
totp: str,
):
super().__init__(username=username, password=password)
self.totp = totp


# Clinic Family model
class FamilyClinic(BaseModel):
Expand All @@ -16,11 +29,13 @@ class FamilyHealthTeam(BaseModel):
name: Optional[str]
phone: Optional[str]


# Clinical Exam Model
class ClinicalExam(BaseModel):
type: str
description: Optional[str]


# Medical Conditions model
class PatientSummary(BaseModel):
allergies: List[str]
Expand All @@ -29,7 +44,7 @@ class PatientSummary(BaseModel):

# Responsible model
class Responsible(BaseModel):
name: str
name: Optional[str] # Temporary
role: str


Expand All @@ -40,7 +55,7 @@ class Encounter(BaseModel):
location: str
type: str
subtype: Optional[str]
exhibition_type: str = 'default'
exhibition_type: str = "default"
active_cids: List[str]
responsible: Optional[Responsible]
clinical_motivation: Optional[str]
Expand All @@ -56,8 +71,9 @@ class UserInfo(BaseModel):
email: Optional[str]
role: Optional[str]


class Professional(BaseModel):
name: str
name: Optional[str]
registry: Optional[str]


Expand Down

0 comments on commit 16ad1bb

Please sign in to comment.