Skip to content

Commit

Permalink
Fixed in v2 patten
Browse files Browse the repository at this point in the history
Signed-off-by: Toomore Chiang <[email protected]>
  • Loading branch information
toomore committed Jan 23, 2025
1 parent b0b6eac commit 7b86617
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
4 changes: 2 additions & 2 deletions module/telegram_bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
''' TelegramBot '''
from time import time
from typing import Any, Union, Optional
from typing import Any, Optional, Union
from uuid import uuid4

from requests import Response, Session
Expand Down Expand Up @@ -30,7 +30,7 @@ def send_message(self, chat_id: str, text: str,
protect_content: bool = False,
reply_to_message_id: Optional[int] = None,
) -> Response:
# pylint: disable=too-many-arguments
# pylint: disable=too-many-arguments,too-many-positional-arguments
''' Send message
Args:
Expand Down
44 changes: 31 additions & 13 deletions structs/projects.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 ConfigDict, BaseModel, EmailStr, Field, HttpUrl, validator
from pydantic import BaseModel, ConfigDict, EmailStr, Field, HttpUrl, field_validator


def skip_empty_str(value: Any) -> Any:
Expand Down Expand Up @@ -41,14 +41,16 @@ class ProjectBase(BaseModel):
action_date: datetime = Field(description='action date')
desc: str | None = Field(None, description='desc')
calendar: str | None = Field(None, description='calendar url')
gitlab_project_id: str | None = Field(None, description='gitlab project id')
gitlab_project_id: str | None = Field(
None, description='gitlab project id')
mailling_leader: EmailStr | None = Field(
None, description='mailing list of leader')
mailling_staff: EmailStr | None = Field(
None, description='mailing list of staff')
mattermost_ch_id: str | None = Field(
None, description='Mattermost main channel id')
shared_drive: HttpUrl | None = Field(None, description='Google shared drive')
shared_drive: HttpUrl | None = Field(
None, description='Google shared drive')
traffic_fee_doc: HttpUrl | None = Field(
None, description='doc fields for traffic fee')
volunteer_certificate_hours: int = Field(
Expand All @@ -59,10 +61,17 @@ class ProjectBase(BaseModel):
description='on/off the forms for available'
)

_validate_skip_empty_str = validator(
'*', pre=True, allow_reuse=True)(skip_empty_str)
_validate_convert_action_date = validator(
'action_date', pre=True, allow_reuse=True)(convert_action_date)
@field_validator("*", mode="before")
@classmethod
def valid_skip_empty_str(cls, value: Any) -> Any:
''' skip empty string '''
return skip_empty_str(value=value)

@field_validator("action_date", mode="before")
@classmethod
def valid_convert_action_date(cls, value: Any) -> Any:
''' convert `action_date` to date '''
return convert_action_date(value=value)


class ProjectBaseUpdate(BaseModel):
Expand All @@ -71,14 +80,16 @@ class ProjectBaseUpdate(BaseModel):
action_date: datetime | None = Field(None, description='action date')
desc: str | None = Field(None, description='desc')
calendar: str | None = Field(None, description='calendar url')
gitlab_project_id: str | None = Field(None, description='gitlab project id')
gitlab_project_id: str | None = Field(
None, description='gitlab project id')
mailling_leader: EmailStr | None = Field(
None, description='mailing list of leader')
mailling_staff: EmailStr | None = Field(
None, description='mailing list of staff')
mattermost_ch_id: str | None = Field(
None, description='Mattermost main channel id')
shared_drive: HttpUrl | None = Field(None, description='Google shared drive')
shared_drive: HttpUrl | None = Field(
None, description='Google shared drive')
traffic_fee_doc: HttpUrl | None = Field(
None, description='doc fields for traffic fee')
volunteer_certificate_hours: int = Field(
Expand All @@ -89,10 +100,17 @@ class ProjectBaseUpdate(BaseModel):
description='on/off the forms for available'
)

_validate_skip_empty_str = validator(
'*', pre=True, allow_reuse=True)(skip_empty_str)
_validate_convert_action_date = validator(
'action_date', pre=True, allow_reuse=True)(convert_action_date)
@field_validator("*", mode="before")
@classmethod
def valid_skip_empty_str(cls, value: Any) -> Any:
''' skip empty string '''
return skip_empty_str(value=value)

@field_validator("action_date", mode="before")
@classmethod
def valid_convert_action_date(cls, value: Any) -> Any:
''' convert `action_date` to date '''
return convert_action_date(value=value)


class ProjectTrafficLocationFeeItem(BaseModel):
Expand Down
14 changes: 9 additions & 5 deletions structs/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from uuid import uuid4

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


def convert_datetime(value: Any) -> datetime:
Expand Down Expand Up @@ -36,7 +36,11 @@ class TaskItem(BaseModel):
people: list[str] = Field(default_factory=list,
description='list of users')

_validate_convert_datetime = validator(
'created_at', 'starttime', 'endtime',
pre=True, allow_reuse=True, always=True)(convert_datetime)
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True)
model_config = ConfigDict(
str_strip_whitespace=True, validate_assignment=True)

@field_validator("created_at", "starttime", "endtime", mode="before")
@classmethod
def valid_convert_action_date(cls, value: Any) -> Any:
''' convert `action_date` to date '''
return convert_datetime(value=value)
4 changes: 2 additions & 2 deletions structs/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from time import time
from typing import Optional

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

from module.dietary_habit import DietaryHabitItemsValue

Expand Down Expand Up @@ -85,7 +85,7 @@ class UserProfleReal(UserProfleRealBase):
bank: Optional[UserBank] = Field(default_factory=UserBank)
address: Optional[UserAddress] = Field(default_factory=UserAddress)
dietary_habit: Optional[list[DietaryHabitItemsValue]] = Field(
default_factory=list)
default_factory=lambda _: [])
model_config = ConfigDict()


Expand Down
2 changes: 1 addition & 1 deletion view/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class UserInfoBase(BaseModel):
profile: dict[str, str]
oauth: dict[str, str]
is_chief: bool = Field(default=False)
chat: dict[str, str] | None = Field(default_factory=dict)
chat: dict[str, str] | None = Field(default_factory=lambda _: {})


@VIEW_TEAM.route('/<pid>/<tid>/members', methods=('GET', 'POST'))
Expand Down

0 comments on commit 7b86617

Please sign in to comment.