本文整理匯總了Python中haystack.connections方法的典型用法代碼示例。如果您正苦於以下問題:Python haystack.connections方法的具體用法?Python haystack.connections怎麽用?Python haystack.connections使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類haystack
的用法示例。
在下文中一共展示了haystack.connections方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: forward_to_backends
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def forward_to_backends(self, method, *args, **kwargs):
# forwards the desired backend method to all the language backends
initial_language = translation.get_language()
# retrieve unique backend name
backends = []
for language, _ in settings.LANGUAGES:
using = '%s-%s' % (self.connection_alias, language)
# Ensure each backend is called only once
if using in backends:
continue
else:
backends.append(using)
translation.activate(language)
backend = connections[using].get_backend()
getattr(backend.parent_class, method)(backend, *args, **kwargs)
if initial_language is not None:
translation.activate(initial_language)
else:
translation.deactivate()
示例2: haystack_default_connection
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def haystack_default_connection(haystack_add_xdist_suffix_to_index_name): # pylint: disable=redefined-outer-name,unused-argument
skip_if_no_django()
backend = haystack_connections['default'].get_backend()
# Force Haystack to update the mapping for the index
backend.setup_complete = False
es = backend.conn
index_name = backend.index_name
ElasticsearchUtils.delete_index(es, index_name)
ElasticsearchUtils.create_alias_and_index(es, index_name)
ElasticsearchUtils.refresh_index(es, index_name)
yield backend
ElasticsearchUtils.delete_index(es, index_name)
示例3: update
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def update(self, index, iterable,
commit=True,
language_specific=False):
if not language_specific \
and self.connection_alias == "default":
current_language = (translation.get_language()
or settings.LANGUAGE_CODE)[:2]
for lang_code, lang_name in settings.LANGUAGES:
using = "default_%s" % lang_code
translation.activate(lang_code)
backend = connections[using].get_backend()
backend.update(index, iterable, commit,
language_specific=True)
translation.activate(current_language)
elif language_specific:
super().update(index, iterable, commit)
開發者ID:PacktPublishing,項目名稱:Django-2-Web-Development-Cookbook-Third-Edition,代碼行數:18,代碼來源:multilingual_whoosh_backend.py
示例4: remove_from_index
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def remove_from_index(paper):
"""
Remove this paper from Haystack's index
(to be called before deleting the paper for real)
"""
using_backends = haystack.connection_router.for_write(instance=paper)
for using in using_backends:
try:
index = haystack.connections[using].get_unified_index(
).get_index(Paper)
index.remove_object(paper, using=using)
except haystack.exceptions.NotHandled:
pass
示例5: get_query
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def get_query(self):
language = translation.get_language()
if not language:
language = settings.LANGUAGES[0][0][:2]
else:
language = language[:2]
using = '%s-%s' % (self.using, language)
return connections[using].get_query()
示例6: update_crossref_pubtypes
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def update_crossref_pubtypes(apps, schema_editor):
"""
This data migrations goes through all OaiRecords with source CrossRef and invalid publication type.
It adjusts the publication type in the OaiRecord and the corresponding paper and updates the index.
We do not use the most sober way with using apps given to this function.
But since we do not alter DB this fine.
The step is necessary, since updating the index requires a full object, not just the DB representation.
"""
# Only source CrossRef
c = OaiSource.objects.get(identifier='crossref')
# Our valid publication types as list
valid_pubtypes = [key for (key, value) in PAPER_TYPE_CHOICES]
# As long as there are malicious entries, correct them in 1000er batches
while OaiRecord.objects.filter(source=c).exclude(pubtype__in=valid_pubtypes).exists():
for o in OaiRecord.objects.filter(source=c).exclude(pubtype__in=valid_pubtypes).select_related('about')[:1000]:
new_pubtype = CITEPROC_PUBTYPE_TRANSLATION.get(o.pubtype, 'other')
print('Change pubtype for OaiRecord {} from {} to {}'.format(o.id, o.pubtype, new_pubtype))
o.pubtype = new_pubtype
o.save()
p = o.about
p.doctype = o.pubtype
p.save()
# Objects in DB are no fine
# Let's update the index
using_backends = haystack.connection_router.for_write(instance=p)
for using in using_backends:
try:
index = haystack.connections[using].get_unified_index(
).get_index(Paper)
index.update_object(p, using=using)
except haystack.exceptions.NotHandled as e:
print(e)
print('Done with 1000 OaiRecords')
示例7: remove_from_index
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def remove_from_index(self):
"""
Remove this paper from Haystack's index
(to be called before deleting the paper for real)
"""
using_backends = haystack.connection_router.for_write(instance=self)
for using in using_backends:
try:
index = haystack.connections[using].get_unified_index(
).get_index(Paper)
index.remove_object(self, using=using)
except haystack.exceptions.NotHandled:
pass
示例8: update_index
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def update_index(self):
"""
Updates Haystack's index for this paper
"""
using_backends = haystack.connection_router.for_write(instance=self)
for using in using_backends:
try:
index = haystack.connections[using].get_unified_index(
).get_index(Paper)
index.update_object(self, using=using)
except haystack.exceptions.NotHandled:
pass
# Rough data extracted through OAI-PMH
示例9: haystack_index
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def haystack_index(self, django_db_blocker):
with django_db_blocker.unblock():
self.r3.institution = self.i
self.r3.department = self.d
self.r3.save()
haystack.connections.reload('default')
call_command('update_index', verbosity=0)
yield
haystack.connections['default'].get_backend().clear()
示例10: setup
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def setup(self):
"""
Defers loading until needed.
"""
from haystack import connections
new_index = False
# Make sure the index is there.
if self.use_file_storage and not os.path.exists(self.path):
os.makedirs(self.path)
new_index = True
if self.use_file_storage and not os.access(self.path, os.W_OK):
raise IOError("The path to your Whoosh index '%s' is not writable for the current user/group." % self.path)
if self.use_file_storage:
self.storage = FileStorage(self.path)
else:
global LOCALS
if LOCALS.RAM_STORE is None:
LOCALS.RAM_STORE = RamStorage()
self.storage = LOCALS.RAM_STORE
self.content_field_name, self.schema = self.build_schema(connections[self.connection_alias].get_unified_index().all_searchfields())
self.parser = QueryParser(self.content_field_name, schema=self.schema)
if new_index is True:
self.index = self.storage.create_index(self.schema)
else:
try:
self.index = self.storage.open_index(schema=self.schema)
except index.EmptyIndexError:
self.index = self.storage.create_index(self.schema)
self.setup_complete = True
示例11: setup
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def setup(self):
"""
Defers loading until needed.
"""
from haystack import connections
new_index = False
# Make sure the index is there.
if self.use_file_storage and not os.path.exists(self.path):
os.makedirs(self.path)
new_index = True
if self.use_file_storage and not os.access(self.path, os.W_OK):
raise IOError("The path to your Whoosh index '%s' is not writable for the current user/group." % self.path)
if self.use_file_storage:
self.storage = FileStorage(self.path)
else:
global LOCALS
if getattr(LOCALS, 'RAM_STORE', None) is None:
LOCALS.RAM_STORE = RamStorage()
self.storage = LOCALS.RAM_STORE
self.content_field_name, self.schema = self.build_schema(
connections[self.connection_alias].get_unified_index().all_searchfields())
self.parser = QueryParser(self.content_field_name, schema=self.schema)
if new_index is True:
self.index = self.storage.create_index(self.schema)
else:
try:
self.index = self.storage.open_index(schema=self.schema)
except index.EmptyIndexError:
self.index = self.storage.create_index(self.schema)
self.setup_complete = True
示例12: handle
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def handle(self, *_args, **options):
self.backends = options.get('using')
if not self.backends:
self.backends = list(haystack_connections.connections_info.keys())
for backend_name in self.backends:
connection = haystack_connections[backend_name]
backend = connection.get_backend()
indices_client = backend.conn.indices
current_alias_name = backend.index_name
self.remove_unused_indexes(indices_client=indices_client, current_alias_name=current_alias_name)
示例13: setUp
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def setUp(self):
super(SearchIndexTestMixin, self).setUp()
self.backend = haystack_connections['default'].get_backend()
self.index_prefix = self.backend.index_name
示例14: test_build_schema_handles_aggregation_key
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def test_build_schema_handles_aggregation_key(self):
"""Verify that build_schema marks the aggregation_key field as not_analyzed."""
backend = self.get_backend()
index = haystack.connections[backend.connection_alias].get_unified_index()
fields = index.all_searchfields()
mapping = backend.build_schema(fields)[1]
assert mapping.get('aggregation_key')
assert mapping['aggregation_key']['index'] == 'not_analyzed'
assert 'analyzer' not in mapping['aggregation_key']
示例15: reindex_course_runs
# 需要導入模塊: import haystack [as 別名]
# 或者: from haystack import connections [as 別名]
def reindex_course_runs(self, course):
index = haystack_connections['default'].get_unified_index().get_index(CourseRun)
for course_run in course.course_runs.all():
index.update_object(course_run)