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

from django.db import connection
from django.db.models import Q
from django.contrib.auth.models import User
from common.util import no_history


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


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


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