From 7ea8e4095ccdd00222b13b78db60fa39f3cda22f Mon Sep 17 00:00:00 2001 From: Levon Gegamyan Date: Fri, 2 Jun 2023 14:11:09 +0300 Subject: [PATCH 1/2] add /external_user_connection/ endpoint --- app/auth/__init__.py | 3 + app/auth/external_users_connection.py | 90 +++++++++++++++++++++++++++ app/auth/swagger_auth.py | 2 + 3 files changed, 95 insertions(+) create mode 100644 app/auth/external_users_connection.py diff --git a/app/auth/__init__.py b/app/auth/__init__.py index 9b71fb62..09f95d4d 100644 --- a/app/auth/__init__.py +++ b/app/auth/__init__.py @@ -5,6 +5,7 @@ auth_bp = Blueprint('auth_bp', __name__, url_prefix='/api/v1/auth') auth_api = Api(auth_bp) +from . import external_users_connection from . import external_users_registration from . import login from . import password_reset_confirm @@ -15,6 +16,8 @@ from . import token_checker +auth_api.add_resource(external_users_connection.ExternalUserConnection, + '/external_user_connection/') auth_api.add_resource(external_users_registration.ExternalUserRegistration, '/external_user_registration/') auth_api.add_resource(login.Login, '/login/') diff --git a/app/auth/external_users_connection.py b/app/auth/external_users_connection.py new file mode 100644 index 00000000..538ef55f --- /dev/null +++ b/app/auth/external_users_connection.py @@ -0,0 +1,90 @@ +from flask import jsonify, make_response +from flask_apispec import doc, use_kwargs +from flask_apispec.views import MethodResource +from flask_jwt_extended import jwt_required +from flask_restful import Resource +from marshmallow import Schema, fields +from sqlalchemy.exc import SQLAlchemyError + +from app import config +from app.database import db_session +from app.logger import app_logger as logger +from app.models import User + + +class UserConnectionSchema(Schema): + telegram_id = fields.Integer(required=True) + external_id = fields.Integer(required=True) + + +class ExternalUserConnection(Resource, MethodResource): + + @doc(description='Set external_id to user by his telegrma_id.' + 'Requires "telegram_id" and "external_id" parameters.', + summary='Set external_id to user', + tags=['User Registration'], + responses={ + 200: {'description': 'external_id has been assigned to the user'}, + 400: {'description': 'The telegram_id can not be empty'}, + }, + params={ + 'telegram_id': { + 'description': ( + 'Set external_id to a user with that telegram id' + ), + 'in': 'query', + 'type': 'integer', + 'required': True + }, + 'external_id': { + 'description': ( + 'Set that external_id to user.' + ), + 'in': 'query', + 'type': 'integer', + 'required': True + }, + 'Authorization': config.PARAM_HEADER_AUTH, + } + ) + @use_kwargs(UserConnectionSchema) + @jwt_required() + def post(self, **kwargs): + telegram_id = kwargs.get('telegram_id') + external_id = kwargs.get('external_id') + + if not external_id or not telegram_id: + logger.info( + 'Messages: The and ' + 'parameters have not been passed' + ) + return make_response( + jsonify( + result=( + 'Необходимо указать параметры ' + ' and .' + ) + ), 400 + ) + + try: + user = User.query.filter_by(telegram_id=telegram_id).first() + user.external_id = external_id + db_session.commit() + except SQLAlchemyError as ex: + logger.error(f'Messages: Database commit error "{str(ex)}"') + db_session.rollback() + return make_response( + jsonify(message=f'Bad request: {str(ex)}'), 400 + ) + + logger.info( + f'External user registration: The user with "{telegram_id}" ' + f'was successfully assigned the external_id "{external_id}".' + ) + + return make_response( + jsonify( + result="Пользователю успешно присвоен external_id." + ), 200 + ) diff --git a/app/auth/swagger_auth.py b/app/auth/swagger_auth.py index 71124091..0f6aafdd 100644 --- a/app/auth/swagger_auth.py +++ b/app/auth/swagger_auth.py @@ -3,6 +3,7 @@ from app.auth.login import Login from app.auth.password_reset import PasswordReset from app.auth.registration import UserRegister +from app.auth.external_users_connection import ExternalUserConnection from app.auth.external_users_registration import ExternalUserRegistration from app.auth.token_checker import TokenChecker from app.auth.send_registration_invite import SendRegistrationInvite @@ -13,6 +14,7 @@ docs.register(UserRegister, blueprint='auth_bp') docs.register(PasswordReset, blueprint='auth_bp') docs.register(PasswordResetConfirm, blueprint='auth_bp') +docs.register(ExternalUserConnection, blueprint='auth_bp') docs.register(ExternalUserRegistration, blueprint='auth_bp') docs.register(SendRegistrationInvite, blueprint='auth_bp') docs.register(TokenChecker, blueprint='auth_bp') From d7d00a988c270cf517f60df30cd96665ec03d715 Mon Sep 17 00:00:00 2001 From: Levon Gegamyan Date: Fri, 2 Jun 2023 16:32:36 +0300 Subject: [PATCH 2/2] fix typo --- app/auth/external_users_connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/auth/external_users_connection.py b/app/auth/external_users_connection.py index 538ef55f..0c0fe61a 100644 --- a/app/auth/external_users_connection.py +++ b/app/auth/external_users_connection.py @@ -19,7 +19,7 @@ class UserConnectionSchema(Schema): class ExternalUserConnection(Resource, MethodResource): - @doc(description='Set external_id to user by his telegrma_id.' + @doc(description='Set external_id to user by his telegram_id.' 'Requires "telegram_id" and "external_id" parameters.', summary='Set external_id to user', tags=['User Registration'],