Commit ce66f22a authored by Tuk Bredsdorff's avatar Tuk Bredsdorff

Added formatters for detail view (fixes #1487)

parent 88b3370b
...@@ -263,6 +263,16 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -263,6 +263,16 @@ class BaseModelView(BaseView, ActionsMixin):
that macros are not supported. that macros are not supported.
""" """
column_formatters_detail = None
"""
Dictionary of list view column formatters to be used for the detail view.
Defaults to column_formatters when set to None.
Functions the same way as column_formatters except
that macros are not supported.
"""
column_type_formatters = ObsoleteAttr('column_type_formatters', 'list_type_formatters', None) column_type_formatters = ObsoleteAttr('column_type_formatters', 'list_type_formatters', None)
""" """
Dictionary of value type formatters to be used in the list view. Dictionary of value type formatters to be used in the list view.
...@@ -319,6 +329,18 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -319,6 +329,18 @@ class BaseModelView(BaseView, ActionsMixin):
Functions the same way as column_type_formatters. Functions the same way as column_type_formatters.
""" """
column_type_formatters_detail = None
"""
Dictionary of value type formatters to be used in the detail view.
By default, two types are formatted:
1. ``None`` will be displayed as an empty string
2. ``list`` will be joined using ', '
Functions the same way as column_type_formatters.
"""
column_labels = ObsoleteAttr('column_labels', 'rename_columns', None) column_labels = ObsoleteAttr('column_labels', 'rename_columns', None)
""" """
Dictionary where key is column name and value is string to display. Dictionary where key is column name and value is string to display.
...@@ -889,6 +911,9 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -889,6 +911,9 @@ class BaseModelView(BaseView, ActionsMixin):
if self.column_formatters_export is None: if self.column_formatters_export is None:
self.column_formatters_export = self.column_formatters self.column_formatters_export = self.column_formatters
if self.column_formatters_detail is None:
self.column_formatters_detail = self.column_formatters
# Type formatters # Type formatters
if self.column_type_formatters is None: if self.column_type_formatters is None:
self.column_type_formatters = dict(typefmt.BASE_FORMATTERS) self.column_type_formatters = dict(typefmt.BASE_FORMATTERS)
...@@ -896,6 +921,9 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -896,6 +921,9 @@ class BaseModelView(BaseView, ActionsMixin):
if self.column_type_formatters_export is None: if self.column_type_formatters_export is None:
self.column_type_formatters_export = dict(typefmt.EXPORT_FORMATTERS) self.column_type_formatters_export = dict(typefmt.EXPORT_FORMATTERS)
if self.column_type_formatters_detail is None:
self.column_type_formatters_detail = dict(typefmt.DETAIL_FORMATTERS)
if self.column_descriptions is None: if self.column_descriptions is None:
self.column_descriptions = dict() self.column_descriptions = dict()
...@@ -1803,6 +1831,26 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -1803,6 +1831,26 @@ class BaseModelView(BaseView, ActionsMixin):
self.column_type_formatters, self.column_type_formatters,
) )
@contextfunction
def get_detail_value(self, context, model, name):
"""
Returns the value to be displayed in the detail view
:param context:
:py:class:`jinja2.runtime.Context`
:param model:
Model instance
:param name:
Field name
"""
return self._get_list_value(
context,
model,
name,
self.column_formatters_detail,
self.column_type_formatters_detail,
)
def get_export_value(self, model, name): def get_export_value(self, model, name):
""" """
Returns the value to be displayed in export. Returns the value to be displayed in export.
...@@ -2103,7 +2151,7 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -2103,7 +2151,7 @@ class BaseModelView(BaseView, ActionsMixin):
return self.render(template, return self.render(template,
model=model, model=model,
details_columns=self._details_columns, details_columns=self._details_columns,
get_value=self.get_list_value, get_value=self.get_detail_value,
return_url=return_url) return_url=return_url)
@expose('/delete/', methods=('POST',)) @expose('/delete/', methods=('POST',))
......
...@@ -84,6 +84,13 @@ EXPORT_FORMATTERS = { ...@@ -84,6 +84,13 @@ EXPORT_FORMATTERS = {
dict: dict_formatter, dict: dict_formatter,
} }
DETAIL_FORMATTERS = {
type(None): empty_formatter,
list: list_formatter,
dict: dict_formatter,
}
if Enum is not None: if Enum is not None:
BASE_FORMATTERS[Enum] = enum_formatter BASE_FORMATTERS[Enum] = enum_formatter
EXPORT_FORMATTERS[Enum] = enum_formatter EXPORT_FORMATTERS[Enum] = enum_formatter
DETAIL_FORMATTERS[Enum] = enum_formatter
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment