Support empty values and absolute URLs

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2019-04-24 04:15:40 -04:00
parent 2e8e4588c0
commit a5f3937228
6 changed files with 46 additions and 14 deletions

View File

@@ -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)
===================

View File

@@ -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
--------

View File

@@ -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 %}
<a href="{{ column_value.get_absolute_url }}">{{ column_value }}</a>
{% if source_column.is_attribute_absolute_url or source_column.is_object_absolute_url %}
<a href="{% navigation_source_column_get_absolute_url source_column=source_column obj=object %}">{{ column_value }}</a>
{% else %}
{{ column_value }}
{% endif %}

View File

@@ -112,8 +112,8 @@
{% navigation_source_column_resolve column=source_column as column_value %}
{% if column_value %}
<td>
{% if source_column.is_absolute_url %}
<a href="{{ column_value.get_absolute_url }}">{{ column_value }}</a>
{% if source_column.is_attribute_absolute_url or source_column.is_object_absolute_url %}
<a href="{% navigation_source_column_get_absolute_url source_column=source_column obj=object %}">{{ column_value }}</a>
{% else %}
{{ column_value }}
{% endif %}

View File

@@ -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):

View File

@@ -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 ''