# Path: protector/query.py
# class Query(object):
#     """
#         Class helps to construct a custom SQL query
#         It holds params to pass to ORM in array
#     """
#     conditions = None
#     params = None
#     fields = None
#     tables = None
# 
#     def __init__(self, conditions=None, params=None, fields=None, tables=None):
#         self.conditions = conditions or []
#         self.params = params or {}
#         self.fields = fields or []
#         self.tables = tables or []
# 
#     def get_query(self):
#         conditions_string = ") AND (".join(self.conditions)
#         tables_string = ", ".join(self.tables)
#         fields_string = ", ".join(self.fields)
#         result = "SELECT {fields!s} FROM {tables!s}".format(
#             fields=fields_string,
#             tables=tables_string
#         )
#         if self.conditions:
#             result += " WHERE({conditions!s})".format(conditions=conditions_string)
#         return result
# 
#     def get_raw_query(self):
#         return self.get_query().format(**self.params)


from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import connection
from protector.query import Query
                    gl.content_type_id = {ctype_id_field!s} AND
                    gug.group_id = {obj_id_field!s}
                )
            )
        """
        return condition.format(
            user_id=user_id, perm_id=perm_id,
            obj_id_field=obj_id_field,
            ctype_id_field=ctype_id_field
        )


def _get_restriction_filter(qset, user_id, perm_id):
    if hasattr(qset, 'get_restriction_id_field'):
        obj_id_field = qset.get_restriction_id_field()
    else:
        obj_id_field = "{table_name!s}.restriction_id".format(
            table_name=qset.model._meta.db_table)
    if hasattr(qset, 'get_restriction_ctype_id_field'):
        ctype_id_field = qset.get_restriction_ctype_id_field()
    else:
        ctype_id_field = "{table_name!s}.restriction_content_type_id".format(
            table_name=qset.model._meta.db_table
        )
    result = _get_filter_by_perm_condition(
        qset, user_id, perm_id, obj_id_field, ctype_id_field)
    return result


def _get_permissions_query(obj=None):

# Path: protector/query.py
# class Query(object):
#     """
#         Class helps to construct a custom SQL query
#         It holds params to pass to ORM in array
#     """
#     conditions = None
#     params = None
#     fields = None
#     tables = None
# 
#     def __init__(self, conditions=None, params=None, fields=None, tables=None):
#         self.conditions = conditions or []
#         self.params = params or {}
#         self.fields = fields or []
#         self.tables = tables or []
# 
#     def get_query(self):
#         conditions_string = ") AND (".join(self.conditions)
#         tables_string = ", ".join(self.tables)
#         fields_string = ", ".join(self.fields)
#         result = "SELECT {fields!s} FROM {tables!s}".format(
#             fields=fields_string,
#             tables=tables_string
#         )
#         if self.conditions:
#             result += " WHERE({conditions!s})".format(conditions=conditions_string)
#         return result
# 
#     def get_raw_query(self):
#         return self.get_query().format(**self.params)


# Path: protector/internals.py
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import connection
from protector.query import Query

                    gl.content_type_id = {ctype_id_field!s} AND
                    gug.group_id = {obj_id_field!s}
                )
            )
        """
        return condition.format(
            user_id=user_id, perm_id=perm_id,
            obj_id_field=obj_id_field,
            ctype_id_field=ctype_id_field
        )


def _get_restriction_filter(qset, user_id, perm_id):
    if hasattr(qset, 'get_restriction_id_field'):
        obj_id_field = qset.get_restriction_id_field()
    else:
        obj_id_field = "{table_name!s}.restriction_id".format(
            table_name=qset.model._meta.db_table)
    if hasattr(qset, 'get_restriction_ctype_id_field'):
        ctype_id_field = qset.get_restriction_ctype_id_field()
    else:
        ctype_id_field = "{table_name!s}.restriction_content_type_id".format(
            table_name=qset.model._meta.db_table
        )
    result = _get_filter_by_perm_condition(
        qset, user_id, perm_id, obj_id_field, ctype_id_field)
    return result


def _get_permissions_query(obj=None):

    query = Query(