forked from django-haystack/django-haystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
78 lines (54 loc) · 2.28 KB
/
__init__.py
File metadata and controls
78 lines (54 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from __future__ import unicode_literals
import re
from django.conf import settings
from django.utils import six
from haystack.constants import ID, DJANGO_CT, DJANGO_ID
from haystack.utils.highlighting import Highlighter
try:
from django.utils import importlib
except ImportError:
import importlib
IDENTIFIER_REGEX = re.compile('^[\w\d_]+\.[\w\d_]+\.\d+$')
def default_get_identifier(obj_or_string):
"""
Get an unique identifier for the object or a string representing the
object.
If not overridden, uses <app_label>.<object_name>.<pk>.
"""
if isinstance(obj_or_string, six.string_types):
if not IDENTIFIER_REGEX.match(obj_or_string):
raise AttributeError(u"Provided string '%s' is not a valid identifier." % obj_or_string)
return obj_or_string
return u"%s.%s.%s" % (
obj_or_string._meta.app_label,
obj_or_string._meta.module_name,
obj_or_string._get_pk_val()
)
def _lookup_identifier_method():
"""
If the user has set HAYSTACK_IDENTIFIER_METHOD, import it and return the method uncalled.
If HAYSTACK_IDENTIFIER_METHOD is not defined, return haystack.utils.default_get_identifier.
This always runs at module import time. We keep the code in a function
so that it can be called from unit tests, in order to simulate the re-loading
of this module.
"""
if not hasattr(settings, 'HAYSTACK_IDENTIFIER_METHOD'):
return default_get_identifier
module_path, method_name = settings.HAYSTACK_IDENTIFIER_METHOD.rsplit(".", 1)
try:
module = importlib.import_module(module_path)
except ImportError:
raise ImportError(u"Unable to import module '%s' provided for HAYSTACK_IDENTIFIER_METHOD." % module_path)
identifier_method = getattr(module, method_name, None)
if not identifier_method:
raise AttributeError(
u"Provided method '%s' for HAYSTACK_IDENTIFIER_METHOD does not exist in '%s'." % (method_name, module_path)
)
return identifier_method
get_identifier = _lookup_identifier_method()
def get_model_ct(model):
return "%s.%s" % (model._meta.app_label, model._meta.module_name)
def get_facet_field_name(fieldname):
if fieldname in [ID, DJANGO_ID, DJANGO_CT]:
return fieldname
return "%s_exact" % fieldname