mirror of
https://github.com/Hopiu/django-watson.git
synced 2026-05-02 10:54:44 +00:00
No longer using tuple for WATSON_MIDDLEWARE_FLAG
This commit is contained in:
parent
b854336b51
commit
7642e3645f
1 changed files with 13 additions and 9 deletions
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from watson.registration import search_context_manager
|
||||
|
||||
|
||||
|
|
@ -9,26 +11,28 @@ WATSON_MIDDLEWARE_FLAG = "watson.search_context_middleware_active"
|
|||
|
||||
|
||||
class SearchContextMiddleware(object):
|
||||
|
||||
|
||||
"""Wraps the entire request in a search context."""
|
||||
|
||||
|
||||
def process_request(self, request):
|
||||
"""Starts a new search context."""
|
||||
request.META[(WATSON_MIDDLEWARE_FLAG, self)] = True
|
||||
if request.META.get(WATSON_MIDDLEWARE_FLAG, False):
|
||||
raise ImproperlyConfigured("SearchContextMiddleware can only be included in MIDDLEWARE_CLASSES once.")
|
||||
request.META[WATSON_MIDDLEWARE_FLAG] = True
|
||||
search_context_manager.start()
|
||||
|
||||
|
||||
def _close_search_context(self, request):
|
||||
"""Closes the search context."""
|
||||
if request.META.get((WATSON_MIDDLEWARE_FLAG, self), False):
|
||||
del request.META[(WATSON_MIDDLEWARE_FLAG, self)]
|
||||
if request.META.get(WATSON_MIDDLEWARE_FLAG, False):
|
||||
del request.META[WATSON_MIDDLEWARE_FLAG]
|
||||
search_context_manager.end()
|
||||
|
||||
|
||||
def process_response(self, request, response):
|
||||
"""Closes the search context."""
|
||||
self._close_search_context(request)
|
||||
return response
|
||||
|
||||
|
||||
def process_exception(self, request, exception):
|
||||
"""Closes the search context."""
|
||||
search_context_manager.invalidate()
|
||||
search_context_manager.invalidate()
|
||||
self._close_search_context(request)
|
||||
|
|
|
|||
Loading…
Reference in a new issue