util.py 577 Bytes
#-*- coding:utf-8 -*-

from django.db import connection
from django.db.models import Q
from django.contrib.auth.models import User


def set_history(user, is_local=True):
    cursor = connection.cursor()
    cursor.execute("SELECT set_config('var.user_id', %s, %s)", [str(user.id), "true" if is_local else "false"])


def bot_history():
    set_history(User.objects.get(username=u'Kuźniobot'), is_local=False)


def users_with_perm(perm):
    return User.objects.filter(
        Q(groups__permissions=perm) | Q(user_permissions=perm) |
        Q(is_superuser=True)).distinct()