# Path: minent/utils.py
# def signature(text):
#     """
#     This helper method normalizes text and takes the SHA1 hash of it,
#     returning the base64 encoded result. The normalization method includes
#     the removal of punctuation and white space as well as making the case
#     completely lowercase. These signatures will help us discover textual
#     similarities between questions.
#     """
#     text = normalize(text).encode('utf-8')
#     return base64.b64encode(hashlib.sha1(text).digest())
# 
# def normalize_query(terms):
#     """
#     Splits the query string in individual keywords, getting rid of extra
#     spaces and grouping quoted words together.
# 
#     Example:
# 
#         >>> normalize_query(' some random  words "with   quotes " and spaces')
#         ['some', 'random', 'words', 'with quotes', 'and', 'spaces']
#     """
#     return [
#         norm_space(' ', (t[0] or t[1]).strip())
#         for t in find_terms(terms)
#     ]


from django.db import models
from minent.utils import signature, normalize_query
from django.db.models.functions import Coalesce
    def search(self, terms):
        """
        Produces an icontains lookup on the question title.
        """
        query = None # Query to search for every search term

        # Build the query with Q and icontains
        for term in normalize_query(terms):
            q = models.Q(text__icontains=term) | models.Q(details__icontains=term)
            query = q if query is None else query & q

        return self.filter(query)


##########################################################################
## Questions Manager
##########################################################################

class QuestionManager(models.Manager):

    def dedupe(self, raise_for_exceptions=False, **kwargs):
        """
        Essentially a GET or CREATE method that checks if a duplicate
        question already exists in the database by its signature. If
        raise_for_exceptions is True, then will raise a DuplicateQuestion
        exception, otherwise it will return None.

        Returns question, created where created is a Boolean
        """
        qsig = signature(kwargs['text'])

# Path: minent/utils.py
# def signature(text):
#     """
#     This helper method normalizes text and takes the SHA1 hash of it,
#     returning the base64 encoded result. The normalization method includes
#     the removal of punctuation and white space as well as making the case
#     completely lowercase. These signatures will help us discover textual
#     similarities between questions.
#     """
#     text = normalize(text).encode('utf-8')
#     return base64.b64encode(hashlib.sha1(text).digest())
# 
# def normalize_query(terms):
#     """
#     Splits the query string in individual keywords, getting rid of extra
#     spaces and grouping quoted words together.
# 
#     Example:
# 
#         >>> normalize_query(' some random  words "with   quotes " and spaces')
#         ['some', 'random', 'words', 'with quotes', 'and', 'spaces']
#     """
#     return [
#         norm_space(' ', (t[0] or t[1]).strip())
#         for t in find_terms(terms)
#     ]


# Path: fugato/managers.py
from django.db import models
from minent.utils import signature, normalize_query
from django.db.models.functions import Coalesce

    def search(self, terms):
        """
        Produces an icontains lookup on the question title.
        """
        query = None # Query to search for every search term

        # Build the query with Q and icontains
        for term in normalize_query(terms):
            q = models.Q(text__icontains=term) | models.Q(details__icontains=term)
            query = q if query is None else query & q

        return self.filter(query)


##########################################################################
## Questions Manager
##########################################################################

class QuestionManager(models.Manager):

    def dedupe(self, raise_for_exceptions=False, **kwargs):
        """
        Essentially a GET or CREATE method that checks if a duplicate
        question already exists in the database by its signature. If
        raise_for_exceptions is True, then will raise a DuplicateQuestion
        exception, otherwise it will return None.

        Returns question, created where created is a Boolean
        """
        qsig = signature(kwargs['text'])

        query = self.filter(signature=qsig)