From a5f3937228d92592c045d4c549d43ff264253bc4 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 24 Apr 2019 04:15:40 -0400 Subject: [PATCH] Support empty values and absolute URLs Signed-off-by: Roberto Rosario --- HISTORY.rst | 5 ++- docs/releases/3.2.rst | 5 ++- .../generic_list_items_subtemplate.html | 5 +-- .../appearance/generic_list_subtemplate.html | 4 +-- mayan/apps/navigation/classes.py | 35 +++++++++++++++---- .../templatetags/navigation_tags.py | 6 +++- 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 9b48d630ff..4fa0d17202 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -131,7 +131,10 @@ * Improve TwoStateWidget to use a template. Make it compatible with the SourceColumn. * Update SourceColumn to support related attributes. - +* Add support for display for empty values for + source columns. +* Add support for source column object or attribute + absolute URLs. 3.1.11 (2019-04-XX) =================== diff --git a/docs/releases/3.2.rst b/docs/releases/3.2.rst index b1552d84ba..392914c683 100644 --- a/docs/releases/3.2.rst +++ b/docs/releases/3.2.rst @@ -163,7 +163,10 @@ Other changes * Improve TwoStateWidget to use a template. Make it compatible with the SourceColumn. * Update SourceColumn to support related attributes. - +* Add support for display for empty values for + source columns. +* Add support for source column object or attribute + absolute URLs. Removals -------- diff --git a/mayan/apps/appearance/templates/appearance/generic_list_items_subtemplate.html b/mayan/apps/appearance/templates/appearance/generic_list_items_subtemplate.html index 61dae07cfd..8029a2898c 100644 --- a/mayan/apps/appearance/templates/appearance/generic_list_items_subtemplate.html +++ b/mayan/apps/appearance/templates/appearance/generic_list_items_subtemplate.html @@ -68,8 +68,9 @@ {% else %} {% navigation_get_source_columns source=object only_identifier=True as source_column %} {% navigation_source_column_resolve column=source_column as column_value %} - {% if source_column.is_absolute_url %} - {{ column_value }} + + {% if source_column.is_attribute_absolute_url or source_column.is_object_absolute_url %} + {{ column_value }} {% else %} {{ column_value }} {% endif %} diff --git a/mayan/apps/appearance/templates/appearance/generic_list_subtemplate.html b/mayan/apps/appearance/templates/appearance/generic_list_subtemplate.html index 6efaaeb0f2..5a23ae15d5 100644 --- a/mayan/apps/appearance/templates/appearance/generic_list_subtemplate.html +++ b/mayan/apps/appearance/templates/appearance/generic_list_subtemplate.html @@ -112,8 +112,8 @@ {% navigation_source_column_resolve column=source_column as column_value %} {% if column_value %} - {% if source_column.is_absolute_url %} - {{ column_value }} + {% if source_column.is_attribute_absolute_url or source_column.is_object_absolute_url %} + {{ column_value }} {% else %} {{ column_value }} {% endif %} diff --git a/mayan/apps/navigation/classes.py b/mayan/apps/navigation/classes.py index f15cbcb458..cdd49a6198 100644 --- a/mayan/apps/navigation/classes.py +++ b/mayan/apps/navigation/classes.py @@ -7,7 +7,9 @@ from furl import furl from django.apps import apps from django.contrib.admin.utils import label_for_field -from django.core.exceptions import ImproperlyConfigured, PermissionDenied +from django.core.exceptions import ( + FieldDoesNotExist, ImproperlyConfigured, PermissionDenied +) from django.db.models.constants import LOOKUP_SEP from django.template import VariableDoesNotExist, Variable from django.template.defaulttags import URLNode @@ -619,16 +621,19 @@ class SourceColumn(object): return final_result def __init__( - self, source, label=None, attribute=None, func=None, - include_label=False, is_absolute_url=False, is_identifier=False, - is_sortable=False, - kwargs=None, order=None, sort_field=None, views=None, widget=None + self, source, attribute=None, empty_value=None, func=None, + include_label=False, is_attribute_absolute_url=False, + is_object_absolute_url=False, is_identifier=False, is_sortable=False, + kwargs=None, label=None, order=None, sort_field=None, views=None, + widget=None ): self.source = source self._label = label self.attribute = attribute + self.empty_value = empty_value self.func = func - self.is_absolute_url = is_absolute_url + self.is_attribute_absolute_url = is_attribute_absolute_url + self.is_object_absolute_url = is_object_absolute_url self.is_identifier = is_identifier self.is_sortable = is_sortable self.kwargs = kwargs or {} @@ -665,6 +670,16 @@ class SourceColumn(object): self.label = self._label + def get_absolute_url(self, obj): + if self.is_object_absolute_url: + return obj.get_absolute_url() + elif self.is_attribute_absolute_url: + result = resolve_attribute( + attribute=self.attribute, kwargs=self.kwargs, + obj=obj + ) + return result.get_absolute_url() + def get_sort_field(self): if self.sort_field: return self.sort_field @@ -710,7 +725,13 @@ class SourceColumn(object): widget_instance = self.widget() return widget_instance.render(name=self.attribute, value=result) - return result + if not result: + if self.empty_value: + return self.empty_value + else: + return result + else: + return result class Text(Link): diff --git a/mayan/apps/navigation/templatetags/navigation_tags.py b/mayan/apps/navigation/templatetags/navigation_tags.py index f4d5666a7e..8822771d49 100644 --- a/mayan/apps/navigation/templatetags/navigation_tags.py +++ b/mayan/apps/navigation/templatetags/navigation_tags.py @@ -108,6 +108,11 @@ def navigation_resolve_menus(context, names, source=None, sort_results=None): return result +@register.simple_tag() +def navigation_source_column_get_absolute_url(source_column, obj): + return source_column.get_absolute_url(obj=obj) + + @register.simple_tag(takes_context=True) def resolve_link(context, link): # This can be used to resolve links or menus too @@ -121,4 +126,3 @@ def navigation_source_column_resolve(context, column): return result else: return '' -