Ejemplo n.º 1
0
async def get_client_users(client_id):
    from helpers import db
    from helpers.models.users import User
    click.echo(
        click.style(str(User.get_count(where_=[User.client_id == client_id])),
                    fg='green'))
    click.echo(await
               db.scalar(await db.autoconfig(),
                         User.get_count(where_=[User.client_id == client_id])))
Ejemplo n.º 2
0
    async def get_list(self, request):
        """
        Получить список пользователй
        :param request:
        :return: - всегда список
        """
        db = request.app['db']
        auth = request.auth
        if auth.get('group') != 1:
            return web.HTTPUnauthorized(
            )  # только админ может видеть пользователей своих

        # замечено, что иногда при некоторых связях реактадмину надо еще один роут

        parameters = request.rel_url.query
        search = request.query.get('q', None)
        group_id = request.query.get('group_id', None)

        if 'id' in parameters:
            ids = parameters.getall('id')
            users = await many(
                db,
                User.get_full_in_ids(ids,
                                     auth.get('client'),
                                     where_=[User.id != auth.get('id')]))
            resp = group_from_labels_many(users)
            return web.json_response(
                resp.get('data'),
                dumps=self.__encode_helper,
                headers={'X-Total-Count': resp.get('total')})

        # собираем все штуки для сортировки и пагинации
        limit = request.query.get('_end', LIMIT)
        offset = request.query.get('_start', '0')

        if int(limit) - int(offset) > 0:
            limit = int(limit) - int(offset)
            limit = str(limit)

        sort = request.query.get('_sort', 'id')
        order = request.query.get('_order', 'DESC')
        if search:
            query = User.get_full(request.auth.get('client'),
                                  limit,
                                  offset,
                                  sort,
                                  order,
                                  where_=[
                                      User.id != auth.get('id'),
                                      User.name.like("%" + search + "%")
                                  ])

            users_total = await scalar(
                db,
                User.get_count(where_=[
                    User.id != auth.get('id'),
                    User.name.like("%" + search + "%"), User.client_id ==
                    request.auth.get('client')
                ]))

        elif group_id:
            query = User.get_full(request.auth.get('client'),
                                  limit,
                                  offset,
                                  sort,
                                  order,
                                  where_=[
                                      User.id != auth.get('id'),
                                      User.group_id == group_id,
                                  ])

            users_total = await scalar(
                db,
                User.get_count(where_=[
                    User.id != auth.get('id'), User.group_id == group_id,
                    User.client_id == request.auth.get('client')
                ]))
        else:
            users_total = await scalar(
                db,
                User.get_count(where_=[
                    User.id != auth.get('id'), User.client_id ==
                    request.auth.get('client')
                ]))

            query = User.get_full(request.auth.get('client'),
                                  limit,
                                  offset,
                                  sort,
                                  order,
                                  where_=[User.id != auth.get('id')])

        users = await many(db, query)

        resp = group_from_labels_many(users)

        # статистика
        stat = {
            'user_id': str(auth.get('id')),
            'code': '14',
            'msg': 'Users list fetched [' + str(users_total) + ']'
        }
        await insert(db, Statistic.add(stat))

        return web.json_response(resp.get('data'),
                                 dumps=self.__encode_helper,
                                 headers={'X-Total-Count': str(users_total)})