From c429bd4d05895bfc7705fc61d05abe3e788d16a9 Mon Sep 17 00:00:00 2001 From: Dyakov Roman Date: Fri, 5 May 2023 12:33:58 +0300 Subject: [PATCH] User agent in marketing --- marketing_api/models/db.py | 3 ++- marketing_api/routes/base.py | 5 +++- migrations/versions/0ea7185ac58b_useragent.py | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/0ea7185ac58b_useragent.py diff --git a/marketing_api/models/db.py b/marketing_api/models/db.py index d0416f3..7415c0b 100644 --- a/marketing_api/models/db.py +++ b/marketing_api/models/db.py @@ -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) @@ -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) diff --git a/marketing_api/routes/base.py b/marketing_api/routes/base.py index e098ad1..964daf4 100644 --- a/marketing_api/routes/base.py +++ b/marketing_api/routes/base.py @@ -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 @@ -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 @@ -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!") diff --git a/migrations/versions/0ea7185ac58b_useragent.py b/migrations/versions/0ea7185ac58b_useragent.py new file mode 100644 index 0000000..aaa764b --- /dev/null +++ b/migrations/versions/0ea7185ac58b_useragent.py @@ -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')