Skip to content

Commit

Permalink
User agent in marketing
Browse files Browse the repository at this point in the history
  • Loading branch information
dyakovri committed May 5, 2023
1 parent ffdb0dd commit c429bd4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion marketing_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Actions(str, enum.Enum):
class User(Base):
id = Column(sa.Integer, primary_key=True)
union_number = Column(sa.String, nullable=True)
user_agent = Column(sa.String, nullable=True)
auth_user_id = Column(sa.Integer, nullable=True)
modify_ts = Column(sa.DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
create_ts = Column(sa.DateTime, nullable=False, default=datetime.utcnow)
Expand All @@ -37,7 +38,7 @@ class ActionsInfo(Base):
"""Actions from user"""

id = Column(sa.Integer, primary_key=True)
user_id = Column(sa.Integer, nullable=False)
user_id = Column(sa.Integer, nullable=True)
action = Column(sa.String, nullable=False)
path_from = Column(sa.String, nullable=True)
path_to = Column(sa.String, nullable=True)
Expand Down
5 changes: 4 additions & 1 deletion marketing_api/routes/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from typing import Annotated

from auth_lib.fastapi import UnionAuth
from fastapi import Depends, FastAPI
from fastapi import Depends, FastAPI, Header
from fastapi.exceptions import HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi_sqlalchemy import DBSessionMiddleware, db
Expand Down Expand Up @@ -33,6 +34,7 @@
async def write_action(
user_action_info: ActionInfo,
user=Depends(UnionAuth(auto_error=False, allow_none=True)),
user_agent: Annotated[str | None, Header()] = None,
):
"""Создать действие"""
user_id = user.get("id") if user else None
Expand All @@ -43,6 +45,7 @@ async def write_action(
if ai.user:
logger.debug(ai.user)
ai.user.auth_user_id = user_id
ai.user.user_agent = user_agent
db.session.flush()
else:
logger.warning(f"write_action with user {user_action_info.user_id} not exists!")
Expand Down
24 changes: 24 additions & 0 deletions migrations/versions/0ea7185ac58b_useragent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Useragent
Revision ID: 0ea7185ac58b
Revises: d1136ec942ac
Create Date: 2023-05-05 12:25:03.383848
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0ea7185ac58b'
down_revision = 'd1136ec942ac'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('user', sa.Column('user_agent', sa.String(), nullable=True))


def downgrade():
op.drop_column('user', 'user_agent')

0 comments on commit c429bd4

Please sign in to comment.