Skip to content

Commit

Permalink
Refactor manager creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
akszydelko committed Dec 17, 2017
1 parent 18fac62 commit 0b27a42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
13 changes: 4 additions & 9 deletions pgsearch/managers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.db.models import Manager
from django.db.models.manager import BaseManager

from .query import SearchQuerySet


class ReadOnlyManager(Manager):
class ReadOnlyManager(object):
def bulk_create(self, *args, **kwargs):
raise NotImplementedError

Expand All @@ -23,17 +23,16 @@ def update(self, *args, **kwargs):
raise NotImplementedError


class ReadOnlySearchManager(ReadOnlyManager):
class ReadOnlySearchManager(ReadOnlyManager, BaseManager.from_queryset(SearchQuerySet)):
"""
Model manager which block editing operations and adds useful search method.
Model manager which block editing operations and adds search method.
E.g. Model.objects.search("Foo bar")
"""

def __init__(self, search_field, title_field=None):
super(ReadOnlySearchManager, self).__init__()
self.search_field = search_field
self.title_field = title_field
self._queryset_class = SearchQuerySet

def contribute_to_class(self, cls, name):
"""Called automatically by Django when setting up the model class."""
Expand All @@ -43,7 +42,3 @@ def contribute_to_class(self, cls, name):
cls._fts_manager = self

super(ReadOnlySearchManager, self).contribute_to_class(cls, name)

def search(self, search_term, **kwargs):
"""Pass through function to search function of SearchQuerySet class."""
return self.get_queryset().search(search_term, **kwargs)
9 changes: 9 additions & 0 deletions pgsearch/query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re

from django.db.models import QuerySet
from psycopg2._psycopg import adapt

Expand All @@ -17,6 +18,8 @@ def qn(name):
return name # Quoting once is enough.
return '"{}"'.format(name)

qn.queryset_only = True


class SearchQuerySet(BaseQuerySet):
@property
Expand All @@ -26,9 +29,13 @@ def manager(self):
def __startswith(self, word_list):
return ['{}:*'.format(x.strip()) for x in filter(lambda x: x.strip(), word_list)]

__startswith.queryset_only = True

def __unaccent(self, term):
return 'unaccent({})'.format(term)

__unaccent.queryset_only = True

def __get_tsquery(self, term, config, join_by, startswith, unaccent=True):
operator = ' {} '.format(join_by)
query = self.__startswith(term) if startswith else term
Expand All @@ -38,6 +45,8 @@ def __get_tsquery(self, term, config, join_by, startswith, unaccent=True):
self.__unaccent(query) if unaccent else query
))

__get_tsquery.queryset_only = True

def search(self, search_term, using=None, rank_ordering=True, rank_function='ts_rank', rank_normalization=32,
fts_language="english", include_simple=True, join_by="&", startswith=True, divide_by_title_length=False,
headline_field=None, headline_lang_simple=False):
Expand Down

0 comments on commit 0b27a42

Please sign in to comment.