Skip to content

Commit

Permalink
Using bump-pydantic to upgrade to v2
Browse files Browse the repository at this point in the history
Signed-off-by: Toomore Chiang <[email protected]>
  • Loading branch information
toomore committed Jan 21, 2025
1 parent e06dd32 commit b0b6eac
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 213 deletions.
46 changes: 24 additions & 22 deletions api/apistructs/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@
from datetime import date
from typing import Any

from pydantic import BaseModel, EmailStr, Field, HttpUrl, validator
from pydantic import field_validator, BaseModel, EmailStr, Field, HttpUrl


class ProjectItem(BaseModel):
''' ProjectItem '''
id: str = Field(description='`pid`, project id')
name: str = Field(description='project name')
desc: str | None = Field(description='desc')
action_date: date | None = Field(description='action date')
owners: list[str] | None = Field(description='list of owners')
calendar: str | None = Field(description='calendar url')
gitlab_project_id: str | None = Field(description='gitlab project id')
desc: str | None = Field(None, description='desc')
action_date: date | None = Field(None, description='action date')
owners: list[str] | None = Field(None, description='list of owners')
calendar: str | None = Field(None, description='calendar url')
gitlab_project_id: str | None = Field(None, description='gitlab project id')
mailling_leader: EmailStr | None = Field(
description='mailing list of leader')
None, description='mailing list of leader')
mailling_staff: EmailStr | None = Field(
description='mailing list of staff')
None, description='mailing list of staff')
mattermost_ch_id: str | None = Field(
description='Mattermost main channel id')
shared_drive: HttpUrl | None = Field(description='Google shared drive')
None, description='Mattermost main channel id')
shared_drive: HttpUrl | None = Field(None, description='Google shared drive')
traffic_fee_doc: HttpUrl | None = Field(
description='doc fields for traffic fee')
None, description='doc fields for traffic fee')
volunteer_certificate_hours: int | None = Field(
description='hours for volunteer certificate')
None, description='hours for volunteer certificate')

@validator('*', pre=True)
@field_validator('*', mode="before")
@classmethod
def skip_empty_str(cls, value: Any) -> Any: # pylint:disable=no-self-argument
''' skip empty string '''
if isinstance(value, str):
Expand All @@ -44,13 +45,14 @@ class TeamItem(BaseModel):
pid: str = Field(description='`pid`, project id')
id: str = Field(description='`tid`, team id')
name: str = Field(description='team name')
chiefs: list[str] | None = Field(description="list of chiefs' uids")
members: list[str] | None = Field(description="list of members' uids")
desc: str | None = Field(description='desc')
mailling: EmailStr | None = Field(description='mailing list for team')
headcount: int | None = Field(description='the headcount of team')
chiefs: list[str] | None = Field(None, description="list of chiefs' uids")
members: list[str] | None = Field(None, description="list of members' uids")
desc: str | None = Field(None, description='desc')
mailling: EmailStr | None = Field(None, description='mailing list for team')
headcount: int | None = Field(None, description='the headcount of team')

@validator('*', pre=True)
@field_validator('*', mode="before")
@classmethod
def skip_empty_str(cls, value: Any) -> Any: # pylint:disable=no-self-argument
''' skip empty string '''
if isinstance(value, str):
Expand All @@ -72,6 +74,6 @@ class UserItem(BaseModel):
id: str = Field(description='user id')
badge_name: str = Field(description='badge name')
avatar: HttpUrl = Field(description='url for avatar')
intro: str | None = Field(description='introduction')
chat: MattermostAccount | None = Field(description='Mattermost account')
is_chief: bool | None = Field(description='is the chief in the team')
intro: str | None = Field(None, description='introduction')
chat: MattermostAccount | None = Field(None, description='Mattermost account')
is_chief: bool | None = Field(None, description='is the chief in the team')
30 changes: 16 additions & 14 deletions api/apistructs/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import date, datetime

import arrow
from pydantic import BaseModel, EmailStr, Field, HttpUrl, validator
from pydantic import field_validator, BaseModel, EmailStr, Field, HttpUrl

from api.apistructs.items import ProjectItem, TeamItem
from structs.projects import ProjectTrafficLocationFeeItem
Expand All @@ -29,36 +29,38 @@ class ProjectCreateOutput(ProjectCreate):
''' Project create output '''
pid: str = Field(description='project id', alias='_id')

@validator('action_date', pre=True)
@field_validator('action_date', mode="before")
@classmethod
def convert_action_date(cls, value: datetime) -> str: # pylint: disable=no-self-argument
''' convert_action_date '''
return arrow.get(value).format('YYYY/MM/DD')


class ProjectItemUpdateInput(BaseModel):
''' Update project item input '''
name: str | None = Field(description='project name')
desc: str | None = Field(description='desc')
action_date: date | None = Field(description='action date')
calendar: str | None = Field(description='calendar url')
gitlab_project_id: str | None = Field(description='gitlab project id')
name: str | None = Field(None, description='project name')
desc: str | None = Field(None, description='desc')
action_date: date | None = Field(None, description='action date')
calendar: str | None = Field(None, description='calendar url')
gitlab_project_id: str | None = Field(None, description='gitlab project id')
mailling_leader: EmailStr | None = Field(
description='mailing list of leader')
None, description='mailing list of leader')
mailling_staff: EmailStr | None = Field(
description='mailing list of staff')
None, description='mailing list of staff')
mattermost_ch_id: str | None = Field(
description='Mattermost main channel id')
shared_drive: HttpUrl | None = Field(description='Google shared drive')
None, description='Mattermost main channel id')
shared_drive: HttpUrl | None = Field(None, description='Google shared drive')
traffic_fee_doc: HttpUrl | None = Field(
description='doc fields for traffic fee')
None, description='doc fields for traffic fee')
volunteer_certificate_hours: int | None = Field(
description='hours for volunteer certificate')
None, description='hours for volunteer certificate')


class ProjectItemUpdateOutput(ProjectItemUpdateInput):
''' Update project item output '''

@validator('action_date', pre=True)
@field_validator('action_date', mode="before")
@classmethod
def convert_action_date(cls, value: str | int | float) -> date: # pylint:disable=no-self-argument
''' convert action_date to date '''
return arrow.get(value).date()
Expand Down
8 changes: 5 additions & 3 deletions api/apistructs/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any

import arrow
from pydantic import BaseModel, Field, validator
from pydantic import field_validator, BaseModel, Field


class SenderCampaignInput(BaseModel):
Expand All @@ -18,7 +18,8 @@ class Creater(BaseModel):
uid: str = Field(description='user id')
at: datetime = Field(description='created at in timestamp')

@validator('at', pre=True)
@field_validator('at', mode="before")
@classmethod
def convert_to_datetime(cls, value: int | float | datetime) -> datetime: # pylint: disable=no-self-argument
''' convert to datetime '''
return arrow.get(value).naive
Expand All @@ -41,7 +42,8 @@ class Receiver(BaseModel):
all_users: bool = Field(
default=False, description='Send to all users in platform')

@validator('team_w_tags', pre=True)
@field_validator('team_w_tags', mode="before")
@classmethod
def convert_to_list(cls, # pylint: disable=no-self-argument
value: Any | list[dict[str, str | list[str]]]) -> list[
dict[str, str | list[str]]]:
Expand Down
30 changes: 10 additions & 20 deletions api/apistructs/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
''' API Structs - Tasks '''
from datetime import datetime

from pydantic import BaseModel, Field
from pydantic import ConfigDict, BaseModel, Field

from api.apistructs.items import UserItem
from structs.tasks import TaskItem
Expand All @@ -23,11 +23,7 @@ class TaskCreateInput(BaseModel):
endtime: datetime = Field(
default_factory=datetime.now, description='task end')
limit: int = Field(default=1, ge=1, description='expect required users')

class Config: # pylint: disable=too-few-public-methods
''' Config '''
anystr_strip_whitespace = True
validate_assignment = True
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True)


class TaskCreateOutput(TaskItem):
Expand All @@ -40,25 +36,19 @@ class TaskGetOutput(TaskItem):

class TaskUpdateInput(BaseModel):
''' TaskUpdateInput '''
title: str | None = Field(description='title')
cate: str | None = Field(description='cate')
desc: str | None = Field(description='desc')
starttime: datetime | None = Field(description='task start')
endtime: datetime | None = Field(description='task end')
limit: int | None = Field(description='expect required users')

class Config: # pylint: disable=too-few-public-methods
''' Config '''
anystr_strip_whitespace = True
title: str | None = Field(None, description='title')
cate: str | None = Field(None, description='cate')
desc: str | None = Field(None, description='desc')
starttime: datetime | None = Field(None, description='task start')
endtime: datetime | None = Field(None, description='task end')
limit: int | None = Field(None, description='expect required users')
model_config = ConfigDict(str_strip_whitespace=True)


class TaskAttendeeInput(BaseModel):
''' TaskAttendeeInput '''
uids: list[str] = Field(description='uids')

class Config: # pylint: disable=too-few-public-methods
''' Config '''
anystr_strip_whitespace = True
model_config = ConfigDict(str_strip_whitespace=True)


class TaskGetAttendeeOutput(BaseModel):
Expand Down
23 changes: 9 additions & 14 deletions api/apistructs/teams.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
''' API Structs - Teams '''
from typing import Any

from pydantic import BaseModel, EmailStr, Field, validator
from pydantic import field_validator, ConfigDict, BaseModel, EmailStr, Field

from api.apistructs.items import UserItem


class TeamItemUpdateInput(BaseModel):
''' Update Team item input '''
name: str | None = Field(description='team name')
desc: str | None = Field(description='desc')
mailling: EmailStr | None = Field(description='mailing list for team')
headcount: int | None = Field(description='the headcount of team')
name: str | None = Field(None, description='team name')
desc: str | None = Field(None, description='desc')
mailling: EmailStr | None = Field(None, description='mailing list for team')
headcount: int | None = Field(None, description='the headcount of team')

@validator('*', pre=True)
@field_validator('*', mode="before")
@classmethod
def skip_empty_str(cls, value: Any) -> Any: # pylint:disable=no-self-argument
''' skip empty string '''
if isinstance(value, str):
Expand All @@ -37,10 +38,7 @@ class TeamCreateInput(BaseModel):
''' Team create input '''
id: str = Field(description='team id')
name: str = Field(description='team name')

class Config: # pylint: disable=too-few-public-methods
''' Config '''
anystr_strip_whitespace = True
model_config = ConfigDict(str_strip_whitespace=True)


class TeamCreateOutput(TeamCreateInput):
Expand All @@ -50,10 +48,7 @@ class TeamCreateOutput(TeamCreateInput):
class TeamUpdateMembers(BaseModel):
''' TeamUpdateMembers '''
uids: list[str] = Field(description='uids')

class Config: # pylint: disable=too-few-public-methods
''' Config '''
anystr_strip_whitespace = True
model_config = ConfigDict(str_strip_whitespace=True)


class TeamUpdateMembersOutput(BaseModel):
Expand Down
10 changes: 3 additions & 7 deletions api/apistructs/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
''' API Structs - Users '''
from datetime import date

from pydantic import BaseModel, Field, HttpUrl
from pydantic import ConfigDict, BaseModel, Field, HttpUrl

from api.apistructs.items import ProjectItem, TeamItem
from module.skill import SkillEnum, StatusEnum, TeamsEnum, TobeVolunteerStruct
Expand Down Expand Up @@ -75,7 +75,7 @@ class UserMeDietaryHabitInput(BaseModel):
''' UserMeDietaryHabitInput '''
checked: list[str] = Field(
description='lists value of `DietaryHabitItemsValue`',
example=['0.001', '0.002'],
examples=[['0.001', '0.002']],
)


Expand Down Expand Up @@ -116,11 +116,7 @@ class UserMeToBeVolunteerInput(BaseModel):
hours: int = Field(description='Hours in an week')
status: StatusEnum = Field(description='status')
desc: str = Field(default='', description='more description')

class Config: # pylint: disable=too-few-public-methods
''' Config '''
anystr_strip_whitespace: bool = True
use_enum_values: bool = True
model_config = ConfigDict()


class UserMeToBeVolunteerOutput(BaseModel):
Expand Down
7 changes: 2 additions & 5 deletions module/api_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import arrow
from passlib.context import CryptContext # type: ignore
from pydantic import BaseModel, Field
from pydantic import ConfigDict, BaseModel, Field

from models.api_tokendb import APITokenDB

Expand All @@ -22,10 +22,7 @@ class APITokenBase(BaseModel):
create_at: datetime = Field(default_factory=datetime.now)
alive: bool = Field(default=True)
token_type: APITokenType

class Config: # pylint: disable=too-few-public-methods
''' config '''
use_enum_values = True
model_config = ConfigDict(use_enum_values=True)


class APITokenTemp(APITokenBase):
Expand Down
13 changes: 6 additions & 7 deletions module/budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Optional, Union

import arrow
from pydantic import BaseModel, error_wrappers, validator
from pydantic import field_validator, ConfigDict, BaseModel, error_wrappers
from pymongo.cursor import Cursor

from models.budgetdb import BudgetDB
Expand Down Expand Up @@ -64,12 +64,10 @@ class BudgetImportItem(BaseModel):
currency: Currency
paydate: str
estimate: str
model_config = ConfigDict(use_enum_values=True)

class Config: # pylint: disable=too-few-public-methods
''' Model config '''
use_enum_values = True

@validator('total')
@field_validator('total')
@classmethod
def verify_total(cls, value: str) -> Union[int, float]:
''' verify total.
Expand All @@ -85,7 +83,8 @@ def verify_total(cls, value: str) -> Union[int, float]:

return int(value)

@validator('paydate')
@field_validator('paydate')
@classmethod
def verify_paydate(cls, value: str, **kwargs: Any) -> str:
''' verify paydate
Expand Down
15 changes: 4 additions & 11 deletions module/skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# pylint: disable=too-few-public-methods
from enum import Enum, IntEnum, unique

from pydantic import BaseModel, Field
from pydantic import ConfigDict, BaseModel, Field


@unique
Expand Down Expand Up @@ -121,13 +121,9 @@ class TobeVolunteerStruct(BaseModel):
skill: list[SkillEnum] = Field(
default_factory=list, description='list of skills')
hours: int = Field(default=0, description='Hours in an week')
status: StatusEnum | None = Field(description='status')
status: StatusEnum | None = Field(None, description='status')
desc: str = Field(default='', description='more description')

class Config:
''' Config '''
anystr_strip_whitespace: bool = True
use_enum_values: bool = True
model_config = ConfigDict()


class RecruitQuery(BaseModel):
Expand All @@ -145,7 +141,4 @@ class RecruitQuery(BaseModel):
default_factory=list, description='list of skills')
status: list[StatusEnum] = Field(
default_factory=list, description='list of status')

class Config:
''' Config '''
use_enum_values = True
model_config = ConfigDict(use_enum_values=True)
Loading

0 comments on commit b0b6eac

Please sign in to comment.