# Path: sesql/config.py
# CONFIG_PATH_FALLBACK = 'sesql_config'
#     CONFIG_PATH = getattr(settings, 'SESQL_CONFIG_PATH', CONFIG_PATH_FALLBACK)
#     CONFIG_PATH = CONFIG_PATH_FALLBACK


from django.db.models.sql import Query
from django.db.models import Q
from sesql import config
        from sesql import longquery
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with SeSQL.  If not, see <http://www.gnu.org/licenses/>.
"""
This monkey patch will enable to use sesql in the Django admin
It's not fully optimal yet, but functionnal
"""



def make_add_filter(original):
    def sesql_amdin_query_add_filter(self, filter_expr, *args, **kwargs):
        name, value = filter_expr
        if not name.startswith("sesql:"):
            return original(self, filter_expr, *args, **kwargs)

        # Ok, a SeSQL filter ? Hum hum
        name = name.split(':', 1)[1]
        if "__" in name:
            name = name.split('__', 1)[0]
        name += "__containswords"
        query = longquery.longquery(Q(classname = self.model) &
                                    Q(**{ name: value }))
        ids = [ oid for klass, oid in query.objs ]
        return original(self, ('id__in', ids), *args, **kwargs)

    return sesql_amdin_query_add_filter


# Path: sesql/config.py
# CONFIG_PATH_FALLBACK = 'sesql_config'
#     CONFIG_PATH = getattr(settings, 'SESQL_CONFIG_PATH', CONFIG_PATH_FALLBACK)
#     CONFIG_PATH = CONFIG_PATH_FALLBACK


# Path: sesql/orm/django/sesql_admin.py
from django.db.models.sql import Query
from django.db.models import Q
from sesql import config
        from sesql import longquery

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with SeSQL.  If not, see <http://www.gnu.org/licenses/>.
"""
This monkey patch will enable to use sesql in the Django admin
It's not fully optimal yet, but functionnal
"""



def make_add_filter(original):
    def sesql_amdin_query_add_filter(self, filter_expr, *args, **kwargs):
        name, value = filter_expr
        if not name.startswith("sesql:"):
            return original(self, filter_expr, *args, **kwargs)

        # Ok, a SeSQL filter ? Hum hum
        name = name.split(':', 1)[1]
        if "__" in name:
            name = name.split('__', 1)[0]
        name += "__containswords"
        query = longquery.longquery(Q(classname = self.model) &
                                    Q(**{ name: value }))
        ids = [ oid for klass, oid in query.objs ]
        return original(self, ('id__in', ids), *args, **kwargs)

    return sesql_amdin_query_add_filter


if getattr(config, 'ENABLE_SESQL_ADMIN', False):