Skip to content

Commit

Permalink
Merge pull request #22 from prefeitura-rio/development
Browse files Browse the repository at this point in the history
Expansão da Cobertura de Testes, Melhorias na Validação e Modelo de Dados
  • Loading branch information
TanookiVerde authored Jan 19, 2024
2 parents 4a3d689 + 2b6ff08 commit 607269b
Show file tree
Hide file tree
Showing 20 changed files with 1,111 additions and 457 deletions.
1 change: 0 additions & 1 deletion api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
app.include_router(entities_mrg.router)
app.include_router(entities.router)
app.include_router(auth.router)
# app.include_router(users.router)

register_tortoise(
app,
Expand Down
51 changes: 24 additions & 27 deletions api/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from tortoise.models import Model

from app.enums import RaceEnum, GenderEnum, NationalityEnum, ConditionCodeTypeEnum, CategoryEnum, ClinicalStatusEnum, SystemEnum

from app.validators import CPFValidator


class RawPatientRecord(Model):
id = fields.UUIDField(pk=True)
patient_cpf = fields.CharField(max_length=11)
data = fields.JSONField()
creator = fields.ForeignKeyField("app.User", related_name="raw_record_creator", null=False)
id = fields.UUIDField(pk=True)
patient_cpf = fields.CharField(max_length=11, validators=[CPFValidator()])
data = fields.JSONField()
data_source = fields.ForeignKeyField("app.DataSource", related_name="raw_record_creator", null=False)

created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)
Expand All @@ -21,9 +21,9 @@ class Meta:

class RawPatientCondition(Model):
id = fields.UUIDField(pk=True)
patient_cpf = fields.CharField(max_length=11)
patient_cpf = fields.CharField(max_length=11, validators=[CPFValidator()])
data = fields.JSONField()
creator = fields.ForeignKeyField("app.User", related_name="raw_condition_creator", null=False)
data_source = fields.ForeignKeyField("app.DataSource", related_name="raw_condition_creator", null=False)

created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)
Expand All @@ -34,10 +34,10 @@ class Meta:

class StandardizedPatientRecord(Model):
id = fields.UUIDField(pk=True)
patient_cpf = fields.CharField(max_length=11)
birth_city = fields.CharField(max_length=32)
birth_state = fields.CharField(max_length=32)
birth_country = fields.CharField(max_length=32)
patient_cpf = fields.CharField(max_length=11, validators=[CPFValidator()])
birth_city = fields.ForeignKeyField("app.City", related_name="birthcity_stdpatients", null=True)
birth_state = fields.ForeignKeyField("app.State", related_name="birthstate_stdpatients", null=True)
birth_country = fields.ForeignKeyField("app.Country", related_name="birthcountry_stdpatients", null=True)
birth_date = fields.DateField()
active = fields.BooleanField(default=True,null=True)
protected_person = fields.BooleanField(null=True)
Expand All @@ -48,7 +48,7 @@ class StandardizedPatientRecord(Model):
name = fields.CharField(max_length=512)
race = fields.CharEnumField(enum_type=RaceEnum)
gender = fields.CharEnumField(enum_type=GenderEnum)
nationality = fields.CharEnumField(enum_type=NationalityEnum)
nationality = fields.CharEnumField(enum_type=NationalityEnum, null=True)
raw_source = fields.ForeignKeyField("app.RawPatientRecord", related_name="std_record_raw", null=False)
cns_list = fields.JSONField(null=True)
address_list = fields.JSONField(null=True)
Expand All @@ -62,11 +62,11 @@ class Meta:

class StandardizedPatientCondition(Model):
id = fields.UUIDField(pk=True)
patient_cpf = fields.CharField(max_length=11)
patient_cpf = fields.CharField(max_length=11, validators=[CPFValidator()])
cid = fields.CharField(max_length=4)
ciap = fields.CharField(max_length=4, null=True)
clinical_status = fields.CharEnumField(enum_type=ClinicalStatusEnum, max_length=32)
category = fields.CharEnumField(enum_type=CategoryEnum, max_length=32)
clinical_status = fields.CharEnumField(enum_type=ClinicalStatusEnum, null=True)
category = fields.CharEnumField(enum_type=CategoryEnum, null=True)
date = fields.DatetimeField()
raw_source = fields.ForeignKeyField("app.RawPatientCondition", related_name="std_condition_raw", null=False)
created_at = fields.DatetimeField(auto_now_add=True)
Expand All @@ -77,9 +77,8 @@ class Meta:


class DataSource(Model):
id = fields.UUIDField(pk=True)
cnes = fields.CharField(max_length=50, unique=True, pk=True)
system = fields.CharEnumField(SystemEnum)
cnes = fields.CharField(max_length=50, unique=True)
description = fields.CharField(max_length=512)


Expand All @@ -91,21 +90,18 @@ class ConditionCode(Model):


class City(Model):
id = fields.UUIDField(pk=True)
code = fields.CharField(max_length=10, unique=True)
code = fields.CharField(max_length=10, pk=True)
name = fields.CharField(max_length=512)
state = fields.ForeignKeyField("app.State", related_name="cities")


class Country(Model):
id = fields.UUIDField(pk=True)
code = fields.CharField(max_length=10, unique=True)
code = fields.CharField(max_length=10, pk=True)
name = fields.CharField(max_length=512)


class State(Model):
id = fields.UUIDField(pk=True)
code = fields.CharField(max_length=10, unique=True)
code = fields.CharField(max_length=10, pk=True)
name = fields.CharField(max_length=512)
country = fields.ForeignKeyField("app.Country", related_name="states")

Expand Down Expand Up @@ -160,7 +156,7 @@ class PatientCns(Model):

class Patient(Model):
id = fields.UUIDField(pk=True)
patient_cpf = fields.CharField(max_length=11, unique=True)
patient_cpf = fields.CharField(max_length=11, unique=True, validators=[CPFValidator()])
birth_date = fields.DateField()
active = fields.BooleanField(default=True)
protected_person = fields.BooleanField(null=True)
Expand All @@ -171,17 +167,18 @@ class Patient(Model):
name = fields.CharField(max_length=512)
race = fields.ForeignKeyField("app.Race", related_name="patient_race")
gender = fields.ForeignKeyField("app.Gender", related_name="patient_gender")
nationality = fields.ForeignKeyField("app.Nationality", related_name="patient_nationality")
nationality = fields.ForeignKeyField("app.Nationality", related_name="patient_nationality", null=True)
birth_city = fields.ForeignKeyField("app.City", related_name="birth_patients", null=True)
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)


class PatientCondition(Model):
id = fields.UUIDField(pk=True)
patient = fields.ForeignKeyField("app.Patient", related_name="patientconditions")
condition_code = fields.ForeignKeyField("app.ConditionCode", related_name="codes")
clinical_status = fields.CharEnumField(enum_type=ClinicalStatusEnum, max_length=32)
category = fields.CharEnumField(enum_type=CategoryEnum, max_length=32)
clinical_status = fields.CharEnumField(enum_type=ClinicalStatusEnum, null=True)
category = fields.CharEnumField(enum_type=CategoryEnum, null=True)
date = fields.DatetimeField()
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)
Expand Down
45 changes: 33 additions & 12 deletions api/app/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,23 @@ class TokenData(BaseModel):
username: Optional[str]


class RawDataModel(BaseModel):
patient_cpf: str
data: dict


class RawDataListModel(BaseModel):
data_list: List[RawDataModel]
cnes: str

class BulkInsertOutputModel(BaseModel):
count: int


class ConditionListModel(BaseModel):
code: str
clinical_status: str
category: str
clinical_status: Optional[str]
category: Optional[str]
date: datetime


Expand All @@ -73,13 +86,13 @@ class PatientConditionListModel(BaseModel):


class PatientModel(BaseModel):
active: Optional[bool]
active: Optional[bool] = True
birth_city: Optional[str]
birth_state: Optional[str]
birth_country: Optional[str]
birth_date: date
patient_cpf: str
deceased: Optional[bool]
deceased: Optional[bool] = False
deceased_date: Optional[date]
father_name: Optional[str]
gender: str
Expand All @@ -102,11 +115,11 @@ class CompletePatientModel(BaseModel):
telecom_list: List[TelecomModel]
address_list: List[AddressModel]
condition_list: List[ConditionListModel]
active: Optional[bool]
active: Optional[bool] = True
birth_city: Optional[str]
birth_state: Optional[str]
birth_country: Optional[str]
deceased: Optional[bool]
deceased: Optional[bool] = False
deceased_date: Optional[date]
father_name: Optional[str]
mother_name: Optional[str]
Expand Down Expand Up @@ -138,9 +151,9 @@ class StandardizedTelecomModel(BaseModel):

class StandardizedPatientRecordModel(BaseModel):
active: Optional[bool] = True
birth_city: Optional[str]
birth_state: Optional[str]
birth_country: Optional[str]
birth_city_cod: Optional[str]
birth_state_cod: Optional[str]
birth_country_cod: Optional[str]
birth_date: date
patient_cpf: str
deceased: Optional[bool] = False
Expand All @@ -162,7 +175,15 @@ class StandardizedPatientConditionModel(BaseModel):
patient_cpf : str
cid : str
ciap: Optional[str]
clinical_status: str
category: str
clinical_status: Optional[str]
category: Optional[str]
date: datetime
raw_source_id: Optional[str]
raw_source_id: Optional[str]


class StandardizedPatientRecordListModel(BaseModel):
records: List[StandardizedPatientRecordModel]


class StandardizedPatientConditionListModel(BaseModel):
conditions: List[StandardizedPatientConditionModel]
30 changes: 22 additions & 8 deletions api/app/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@
from app.pydantic_models import Token
from app.utils import authenticate_user, create_access_token


router = APIRouter(prefix="/auth", tags=["Autenticação"])


@router.post("/token", response_model=Token)
async def login_for_access_token(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
@router.post("/token")
async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
) -> Token:

user: User = await authenticate_user(form_data.username, form_data.password)

if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
status_code = status.HTTP_401_UNAUTHORIZED,
detail = "Incorrect username or password",
headers = {"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=config.JWT_ACCESS_TOKEN_EXPIRE_MINUTES)

access_token_expires = timedelta(
minutes = config.JWT_ACCESS_TOKEN_EXPIRE_MINUTES
)

access_token = create_access_token(
data={"sub": user.username}, expires_delta=access_token_expires
data = {"sub": user.username},
expires_delta = access_token_expires
)
return {"access_token": access_token, "token_type": "bearer"}

return {
"access_token": access_token,
"token_type": "bearer"
}
17 changes: 8 additions & 9 deletions api/app/routers/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
router = APIRouter(prefix="/outros", tags=["Outras Entidades"])


@router.post("/user", response_model=UserRegisterOutputModel, status_code=200)
@router.post("/user", status_code=201)
async def create_user(
_: Annotated[User, Depends(get_current_active_user)],
user: UserRegisterInputModel,
_ : Annotated[User, Depends(get_current_active_user)],
user : UserRegisterInputModel,
) -> UserRegisterOutputModel:

user_data = user.dict()
datasource_data = user_data.pop('data_source')

Expand All @@ -32,16 +33,14 @@ async def create_user(
description = datasource_data['description']
)

user_data['password'] = password_hash(user_data['password'])
user_data['data_source'] = datasource_instance
user_data['password'] = password_hash(user_data['password'])
user_data['data_source'] = datasource_instance

user_instance = await User.create(
**user_data
)
user_instance = await User.create(**user_data)

output = {
**dict(user_instance),
'data_source': dict(datasource_instance)
}

return output
return output
Loading

0 comments on commit 607269b

Please sign in to comment.