Improve request object resolution

Add an additional method to obtain the request when it is
not available from the context.

Add support for SourceColumn resolution of inherited
sub models.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2018-12-26 06:06:07 -04:00
parent c9ce90ea31
commit 57269ca7f9
3 changed files with 53 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
from __future__ import unicode_literals
from django.template import Context, Library
from django.template import Context, Library, VariableDoesNotExist, Variable
from django.template.defaultfilters import truncatechars
from django.template.loader import get_template
from django.utils.encoding import force_text
@@ -63,8 +63,21 @@ def get_list_mode_icon(context):
@register.simple_tag(takes_context=True)
def get_list_mode_querystring(context):
try:
request = context.request
except AttributeError:
# Simple request extraction failed. Might not be a view context.
# Try alternate method.
try:
request = Variable('request').resolve(context)
except VariableDoesNotExist:
# There is no request variable, most probable a 500 in a test
# view. Don't return any resolved request.
logger.warning('No request variable, aborting request resolution')
return ''
# We do this to get an mutable copy we can modify
querystring = context.request.GET.copy()
querystring = request.GET.copy()
list_as_items = context.get('list_as_items', False)

View File

@@ -501,26 +501,32 @@ class SourceColumn(object):
@classmethod
def get_for_source(cls, context, source, exclude_identifier=False, only_identifier=False):
try:
result = SourceColumn.sort(columns=cls._registry[source])
result = cls._registry[source]
except KeyError:
try:
# Try it as a queryset
result = SourceColumn.sort(columns=cls._registry[source.model])
except AttributeError:
# Might be an instance, try its class
result = cls._registry[source.__class__]
except KeyError:
try:
# It seems to be an instance, try its class
result = SourceColumn.sort(columns=cls._registry[source.__class__])
except KeyError:
# Might be an inherited class insance, try its source class
result = cls._registry[source.source_ptr.__class__]
except (KeyError, AttributeError):
try:
# Special case for queryset items produced from
# .defer() or .only() optimizations
result = SourceColumn.sort(columns=cls._registry[list(source._meta.parents.items())[0][0]])
except (AttributeError, KeyError, IndexError):
result = ()
# Try it as a queryset
result = cls._registry[source.model]
except AttributeError:
try:
# Special case for queryset items produced from
# .defer() or .only() optimizations
result = cls._registry[list(source._meta.parents.items())[0][0]]
except (AttributeError, KeyError, IndexError):
result = ()
except TypeError:
# unhashable type: list
result = ()
result = SourceColumn.sort(columns=result)
if exclude_identifier:
result = [item for item in result if not item.is_identifier]
else:
@@ -531,7 +537,23 @@ class SourceColumn(object):
return None
final_result = []
current_view_name = get_current_view_name(request=context.request)
try:
request = context.request
except AttributeError:
# Simple request extraction failed. Might not be a view context.
# Try alternate method.
try:
request = Variable('request').resolve(context)
except VariableDoesNotExist:
# There is no request variable, most probable a 500 in a test
# view. Don't return any resolved request.
logger.warning(
'No request variable, aborting request resolution'
)
return result
current_view_name = get_current_view_name(request=request)
for item in result:
if item.views:
if current_view_name in item.views:

View File

@@ -1,9 +1,12 @@
from __future__ import unicode_literals
import logging
from django.template import Library
from ..classes import Link, Menu, SourceColumn
logger = logging.getLogger(__name__)
register = Library()