Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
Add user telegram information (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegYurchik authored Mar 27, 2024
2 parents 823bfe2 + 3259321 commit bef8d46
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions autotests/clients/rest/users/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async def update_user(
first_name: str,
last_name: str,
about: str | None = None,
telegram: str | None = None,
main_specialization_id: uuid.UUID | None = None,
secondary_specialization_id: uuid.UUID | None = None,
) -> UserResponse:
Expand All @@ -83,6 +84,7 @@ async def update_user(
first_name=first_name,
last_name=last_name,
about=about,
telegram=telegram,
main_specialization_id=main_specialization_id,
secondary_specialization_id=secondary_specialization_id,
)
Expand Down
2 changes: 2 additions & 0 deletions autotests/clients/rest/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class JWTData(BaseModel):
class UserResponse(BaseModel):
id: uuid.UUID
email: EmailStr | None
telegram: str | None
first_name: str | None
last_name: str | None
is_activated: bool
Expand All @@ -34,6 +35,7 @@ class UserUpdateRequest(BaseModel):
first_name: str
last_name: str
about: str | None
telegram: str | None
main_specialization_id: uuid.UUID | None
secondary_specialization_id: uuid.UUID | None

Expand Down
4 changes: 4 additions & 0 deletions autotests/flow/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ async def test_update_user(
first_name = "Not-Oleg"
last_name = "Not-Yurchik"
about = "New about"
telegram = "+79123456789"

user = await oleg_users_rest_client.update_user(
user_id=oleg_id,
first_name=first_name,
last_name=last_name,
about=about,
telegram=telegram,
main_specialization_id=backend_specialization_id,
secondary_specialization_id=web_design_specialization_id,
)
Expand All @@ -88,6 +90,7 @@ async def test_update_user(
"first_name": first_name,
"last_name": last_name,
"about": about,
"telegram": telegram,
"main_specialization_id": backend_specialization_id,
"secondary_specialization_id": web_design_specialization_id,
})
Expand All @@ -97,6 +100,7 @@ async def test_update_user(
assert user.first_name == first_name
assert user.last_name == last_name
assert user.about == about
assert user.telegram == telegram
assert user.main_specialization_id == backend_specialization_id
assert user.secondary_specialization_id == web_design_specialization_id

Expand Down
2 changes: 2 additions & 0 deletions sapphire/database/migrations/versions/891640b6839e_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ def upgrade() -> None:
sa.Column("first_name", sa.String(), nullable=True),
sa.Column("last_name", sa.String(), nullable=True),
sa.Column("avatar", sa.String(), nullable=True),
sa.Column("telegram", sa.String(), nullable=True),
sa.Column("is_activated", sa.Boolean(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("avatar"),
sa.UniqueConstraint("telegram"),
sa.UniqueConstraint("email"),
)
op.create_table("chat_members",
Expand Down
7 changes: 6 additions & 1 deletion sapphire/database/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class User(Base):
first_name: Mapped[str | None]
last_name: Mapped[str | None]
avatar: Mapped[str | None] = mapped_column(unique=True)
telegram: Mapped[str | None] = mapped_column(unique=True)
is_activated: Mapped[bool] = mapped_column(default=False)

created_at: Mapped[datetime] = mapped_column(default=now)
Expand All @@ -33,7 +34,11 @@ def has_avatar(self) -> bool:
return self.avatar is not None

def activate(self) -> bool:
if (self.first_name or "").strip() and (self.last_name or "").strip():
if (
(self.first_name or "").strip() and
(self.last_name or "").strip() and
(self.telegram or "").strip()
):
self.is_activated = True

return self.is_activated
Expand Down
2 changes: 2 additions & 0 deletions sapphire/users/api/rest/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class UserResponse(BaseModel):

id: uuid.UUID
email: EmailStr | None
telegram: str | None
first_name: str | None
last_name: str | None
is_activated: bool
Expand All @@ -25,6 +26,7 @@ def from_db_model(cls, user: User, with_email: bool = True) -> "UserResponse":
return cls(
id=user.id,
email=user.email if with_email else None,
telegram=user.telegram,
first_name=user.first_name,
last_name=user.last_name,
is_activated=user.is_activated,
Expand Down
1 change: 1 addition & 0 deletions sapphire/users/api/rest/users/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async def update_user(
first_name=data.first_name,
last_name=data.last_name,
about=data.about,
telegram=data.telegram,
main_specialization_id=data.main_specialization_id,
secondary_specialization_id=data.secondary_specialization_id
)
Expand Down
1 change: 1 addition & 0 deletions sapphire/users/api/rest/users/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class UserUpdateRequest(BaseModel):
first_name: constr(strip_whitespace=True, min_length=1)
last_name: constr(strip_whitespace=True, min_length=1)
about: str | None
telegram: str | None
main_specialization_id: uuid.UUID | None
secondary_specialization_id: uuid.UUID | None
3 changes: 3 additions & 0 deletions sapphire/users/database/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async def update_user(
first_name: str | None | Type[Empty] = Empty,
last_name: str | None | Type[Empty] = Empty,
avatar: str | None | Type[Empty] = Empty,
telegram: str | None | Type[Empty] = Empty,
about: str | None | Type[Empty] = Empty,
main_specialization_id: uuid.UUID | None | Type[Empty] = Empty,
secondary_specialization_id: uuid.UUID | None | Type[Empty] = Empty,
Expand All @@ -61,6 +62,8 @@ async def update_user(
user.last_name = last_name
if avatar is not Empty:
user.avatar = avatar
if telegram is not Empty:
user.telegram = telegram
if about is not Empty:
user.profile.about = about
if main_specialization_id is not Empty:
Expand Down

0 comments on commit bef8d46

Please sign in to comment.