Skip to content

Commit

Permalink
Merge pull request djeck1432#198 from mayasimi/feat/add_tg-user-field
Browse files Browse the repository at this point in the history
feat: add telegram user field
  • Loading branch information
djeck1432 authored Nov 14, 2024
2 parents 1b31436 + 8e9c093 commit b6ba999
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""add is_allowed_notification to telegram_user
Revision ID: b1fdf24eae4f
Revises: 02521fa24607
Create Date: 2024-11-13 22:46:54.671430
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.

revision = 'b1fdf24eae4f'
down_revision = '02521fa24607'
branch_labels = None
depends_on = None


def upgrade() -> None:
"""Add is_allowed_notification column to telegram_user table"""
op.add_column(
'telegram_user',
sa.Column('is_allowed_notification', sa.Boolean(), nullable=True)
)

def downgrade() -> None:
"""Remove is_allowed_notification column from telegram_user table"""
op.drop_column('telegram_user', 'is_allowed_notification')
39 changes: 39 additions & 0 deletions web_app/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,42 @@ def delete_telegram_user(self, telegram_id: str) -> None:
user = self.get_user_by_telegram_id(telegram_id)
if user:
self.delete_object(user, user.id)

def set_notification_allowed(
self,
telegram_id: str = None,
wallet_id: str = None
) -> TelegramUser:
"""
Toggles or sets is_allowed_notification for a TelegramUser,
creating a new user if none exists.
Either telegram_id or wallet_id must be provided.
:param telegram_id: str, optional
:param wallet_id: str, optional
:return: TelegramUser
"""
if not telegram_id and not wallet_id:
raise ValueError("Either telegram_id or wallet_id must be provided")

with self.Session() as session:
user = None
if telegram_id:
user = self.get_user_by_telegram_id(telegram_id)
if not user and wallet_id:
user = session.query(
TelegramUser
).filter_by(wallet_id=wallet_id).first()

if user:
user.is_allowed_notification = not user.is_allowed_notification
session.commit()
session.refresh(user)
return user
else:
user_data = {
"telegram_id": telegram_id,
"wallet_id": wallet_id,
"is_allowed_notification": True
}
return self.create_telegram_user(user_data)
1 change: 1 addition & 0 deletions web_app/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class TelegramUser(Base):
last_name = Column(String)
wallet_id = Column(String, ForeignKey("user.wallet_id"))
photo_url = Column(String)
is_allowed_notification = Column(Boolean, default=False)

created_at = Column(DateTime, nullable=False, default=func.now())
updated_at = Column(
Expand Down

0 comments on commit b6ba999

Please sign in to comment.