示例#1
0
    async def post(self):
        data = await self.request.json()

        client = Client(
            email=data.get('email'),
            password=data.get('password')
        )

        if self.request.app['db']:
            client.db = self.request.app['db']

        q = {
            "email": client.client_email,
            "password": client.password
        }

        client_exit = await client.get(**q)

        if client_exit:
            client.pk = client_exit.get('_id')

            response = {
                'client_id': "{}".format(client.pk),
                'status': True,
                'token': "{}".format(await client.token)
            }

        else:
            response = {
                'status': False,
                'error': 'client not found'
            }

        return json_response(response)
示例#2
0
    async def check_client(self):
        """
            Метод для проверяет, что данный пользователь авторизован и что он это он.
            Ищем пользователя по указанному ID в строке URL
            Затем, находтим его token и сравниваем с тем, что указан в заголовке запроса
            Если токены не совпадают - произойдет ошибка
        """
        token_in_header = self.request.__dict__.get('headers').get('AUTHORIZATION',
                                                                   'c97868d8-ccd5-43e4-914c-fe87e9438ec0')

        if not token_in_header:
            raise TokeInHeadersNotFound
        else:

            client = Client(
                pk=self.client_pk
            )

            if self.db:
                client.db = self.db

            self.client = await client.get()

            if not str(await client.token) == str(token_in_header):
                raise TokenIsNotFound
示例#3
0
    async def post(self):
        data = await self.request.json()

        client = Client(email=data.get('email'), password=data.get('password'))

        if self.request.app['db']:
            client.db = self.request.app['db']

        q = {"email": client.client_email, "password": client.password}

        client_exit = await client.get(**q)

        if client_exit:
            client.pk = client_exit.get('_id')

            response = {
                'client_id': "{}".format(client.pk),
                'status': True,
                'token': "{}".format(await client.token)
            }

        else:
            response = {'status': False, 'error': 'client not found'}

        return json_response(response)
示例#4
0
    async def post(self):
        data = await self.request.json()

        client = Client(
            email=data.get('email'),
            password=data.get('password')
        )

        if self.request.app['db']:
            client.db = self.request.app['db']

        q = {'email': client.client_email}

        search = await client.objects.find_one(q)

        if not search:
            try:
                response = {
                    'status': True,
                    'client_id': "{}".format(await client.save()),
                }

            except(Exception,) as error:
                response = {
                    'status': False,
                    'error': "{}".format(error)
                }

        else:
            response = {
                'status': False,
                'error': 'client already exist'
            }

        return json_response(response)
示例#5
0
    async def post(self):
        data = await self.request.json()

        try:
            client = Client(
                pk=ObjectId(data.get('id'))
            )

            if self.request.app['db']:
                client.db = self.request.app['db']

            if await client.get():
                await client.delete()

                response = {
                    'status': True
                }
            else:
                response = {
                    'status': False,
                    'error': 'client not found'
                }
        except(Exception,) as error:
            response = {
                'status': False,
                'error': "{}".format(error)
            }

        return json_response(response)
示例#6
0
    async def post(self):
        data = await self.request.json()

        client = Client(email=data.get('email'), password=data.get('password'))

        if self.request.app['db']:
            client.db = self.request.app['db']

        q = {'email': client.client_email}

        search = await client.objects.find_one(q)

        if not search:
            try:
                response = {
                    'status': True,
                    'client_id': "{}".format(await client.save()),
                }

            except (Exception, ) as error:
                response = {'status': False, 'error': "{}".format(error)}

        else:
            response = {'status': False, 'error': 'client already exist'}

        return json_response(response)
示例#7
0
    async def get(self):
        client_obj = Client(pk=ObjectId(self.request.match_info.get('id')))

        if self.request.app['db']:
            client_obj.db = self.request.app['db']

        client = await client_obj.get()

        result = {
            'id': "{}".format(client.get('_id')),
            'email': "{}".format(client.get('email')),
        }

        return json_response(result)
示例#8
0
    async def get(self):
        client_obj = Client(
            pk=ObjectId(self.request.match_info.get('id'))
        )

        if self.request.app['db']:
            client_obj.db = self.request.app['db']

        client = await client_obj.get()

        result = {
            'id': "{}".format(client.get('_id')),
            'email': "{}".format(client.get('email')),
        }

        return json_response(result)
示例#9
0
    async def check_receiver(self, receiver: ObjectId):
        """
        Метод проверяет, что получатель существует и находится в чате с отправителем.

        :param receiver: индификатор получателя
        """
        client = Client(pk=ObjectId(receiver))

        if self.db:
            client.db = self.db

        await client.get()

        q = {'chat': self.chat_pk, 'client': ObjectId(receiver)}

        if not await self.client_in_chat.get(**q):
            self.client_in_chat.save(**q)
示例#10
0
    async def post(self):
        data = await self.request.json()

        try:
            client = Client(pk=ObjectId(data.get('id')))

            if self.request.app['db']:
                client.db = self.request.app['db']

            if await client.get():
                await client.delete()

                response = {'status': True}
            else:
                response = {'status': False, 'error': 'client not found'}
        except (Exception, ) as error:
            response = {'status': False, 'error': "{}".format(error)}

        return json_response(response)
示例#11
0
    async def wrapper(request, *args, **kwargs):

        token_in_header = request._request.__dict__.get('headers').get('AUTHORIZATION', None)

        if token_in_header:
            try:
                token = Token()

                if request._request.app['db']:
                    token.db = request._request.app['db']

                token_is = await token.objects.find_one({
                    'token': token_in_header
                })

                if not token_is:
                    raise TokenIsNotFound

                else:
                    client = Client(
                        pk=token_is.get('client')
                    )
                    if request._request.app['db']:
                        client.db = request._request.app['db']

                    request._request.client = await client.get()

                    if not client:
                        raise ClientNotFoundViaToken
                    else:
                        return await func(request, *args, **kwargs)
            except(Exception,) as error:
                return json_response({
                    'status': False,
                    'error': '{}'.format(error)
                })

        else:
            return json_response({
                'status': False,
                'error': 'need token in headers'
            })
示例#12
0
    async def wrapper(request, *args, **kwargs):

        token_in_header = request._request.__dict__.get('headers').get(
            'AUTHORIZATION', None)

        if token_in_header:
            try:
                token = Token()

                if request._request.app['db']:
                    token.db = request._request.app['db']

                token_is = await token.objects.find_one(
                    {'token': token_in_header})

                if not token_is:
                    raise TokenIsNotFound

                else:
                    client = Client(pk=token_is.get('client'))
                    if request._request.app['db']:
                        client.db = request._request.app['db']

                    request._request.client = await client.get()

                    if not client:
                        raise ClientNotFoundViaToken
                    else:
                        return await func(request, *args, **kwargs)
            except (Exception, ) as error:
                return json_response({
                    'status': False,
                    'error': '{}'.format(error)
                })

        else:
            return json_response({
                'status': False,
                'error': 'need token in headers'
            })
示例#13
0
    async def check_receiver(self, receiver: ObjectId):
        """
        Метод проверяет, что получатель существует и находится в чате с отправителем.

        :param receiver: индификатор получателя
        """
        client = Client(
            pk=ObjectId(receiver)
        )

        if self.db:
            client.db = self.db

        await client.get()

        q = {
            'chat': self.chat_pk,
            'client': ObjectId(receiver)
        }

        if not await self.client_in_chat.get(**q):
            self.client_in_chat.save(**q)