diff --git a/mayan/apps/common/templatetags/common_tags.py b/mayan/apps/common/templatetags/common_tags.py index fee8bd31c6..488032667f 100644 --- a/mayan/apps/common/templatetags/common_tags.py +++ b/mayan/apps/common/templatetags/common_tags.py @@ -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) diff --git a/mayan/apps/navigation/classes.py b/mayan/apps/navigation/classes.py index 874e630b27..c378b2ed67 100644 --- a/mayan/apps/navigation/classes.py +++ b/mayan/apps/navigation/classes.py @@ -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: diff --git a/mayan/apps/navigation/templatetags/navigation_tags.py b/mayan/apps/navigation/templatetags/navigation_tags.py index 175d0d6924..45c231ed27 100644 --- a/mayan/apps/navigation/templatetags/navigation_tags.py +++ b/mayan/apps/navigation/templatetags/navigation_tags.py @@ -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()