-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also improves formatting for deeply nested data.
- Loading branch information
1 parent
8c37e85
commit 5629121
Showing
11 changed files
with
466 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 23 additions & 17 deletions
40
admin_data_views/templates/admin_data_views/dict_item.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
{% load i18n static admin_data_utils %} | ||
|
||
{% if label != "" %} | ||
<label class="required adv-dict-label">{{ label|capfirst }}</label> | ||
{% endif %} | ||
<div class="adv-dict"> | ||
{% if label != "" %} | ||
<label class="required">{{ label|capfirst }}</label> | ||
{% endif %} | ||
|
||
<div style="float: left;"> | ||
{% for key, value in items.items %} | ||
{% if value|get_type == "dict" %} | ||
{% include 'admin_data_views/dict_item.html' with label=key items=value only %} | ||
{% elif value|get_type == "list" %} | ||
{% include 'admin_data_views/list_item.html' with label=key items=value only %} | ||
{% else %} | ||
<div class="fieldBox" style="float: none"> | ||
<label class="required" style="float: none;display: inline-block">{{ key|capfirst }}</label> | ||
{% include 'admin_data_views/item.html' with value=value only %} | ||
</div> | ||
{% endif %} | ||
<br> | ||
{% endfor %} | ||
<div class="adv-dict-items"> | ||
{% for key, value in items.0.items %} | ||
<div class="adv-dict-item"> | ||
{% if value.0|get_type == "dict" %} | ||
{% include 'admin_data_views/dict_item.html' with label=key items=value only %} | ||
{% elif value.0|get_type == "list" %} | ||
{% include 'admin_data_views/list_item.html' with label=key items=value only %} | ||
{% else %} | ||
<label class="required">{{ key|capfirst }}</label> | ||
{% include 'admin_data_views/item.html' with value=value only %} | ||
{% endif %} | ||
</div> | ||
<br> | ||
{% endfor %} | ||
</div> | ||
|
||
{% if items.1 %} | ||
<div class="help">{{ items.1|safe }}</div> | ||
{% endif %} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
{% if value|length > 40 %} | ||
<textarea type="text" rows="6" class="vTextField" disabled>{{ value|safe }}</textarea> | ||
{% else %} | ||
<input type="text" value="{{ value|safe }}" class="vTextField" disabled> | ||
{% endif %} | ||
<div class="adv-item"> | ||
{% if value.0|length > 40 %} | ||
<textarea type="text" rows="6" disabled>{{ value.0|safe }}</textarea> | ||
{% else %} | ||
<input type="text" value="{{ value.0|safe }}" disabled> | ||
{% endif %} | ||
|
||
{% if value.1 %} | ||
<div class="help">{{ value.1|safe }}</div> | ||
{% endif %} | ||
</div> |
44 changes: 27 additions & 17 deletions
44
admin_data_views/templates/admin_data_views/list_item.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
{% load i18n static admin_data_utils %} | ||
|
||
{% if label != "" %} | ||
<label class="required">{{ label|capfirst }}</label> | ||
{% endif %} | ||
|
||
<div style="float: left;"> | ||
{% for value in items %} | ||
{% if value|get_type == "dict" %} | ||
{% include 'admin_data_views/dict_item.html' with label="" items=value only %} | ||
{% elif value|get_type == "list" %} | ||
{% include 'admin_data_views/list_item.html' with label="" items=value only %} | ||
{% else %} | ||
<div class="fieldBox"> | ||
{% include 'admin_data_views/item.html' with value=value only %} | ||
</div> | ||
<div class="related-widget-wrapper"> | ||
<div class="adv-array"> | ||
{% if label != "" %} | ||
<label class="required">{{ label|capfirst }}</label> | ||
{% endif %} | ||
<hr> | ||
<br> | ||
{% endfor %} | ||
|
||
<ul> | ||
{% for value in items.0 %} | ||
<li> | ||
<div class="adv-array-item"> | ||
{% if value.0|get_type == "dict" %} | ||
{% include 'admin_data_views/dict_item.html' with label="" items=value only %} | ||
{% elif value.0|get_type == "list" %} | ||
{% include 'admin_data_views/list_item.html' with label="" items=value only %} | ||
{% else %} | ||
{% include 'admin_data_views/item.html' with value=value only %} | ||
{% endif %} | ||
</div> | ||
<hr style="margin: 3px 0"> | ||
</li> | ||
{% endfor %} | ||
|
||
{% if items.1 %} | ||
<div class="help">{{ items.1|safe }}</div> | ||
{% endif %} | ||
|
||
</ul> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,53 @@ | ||
from django import template | ||
|
||
from ..typing import Any, Dict, DictItems, ItemsView, NestedDict, SectionData, Union | ||
|
||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter | ||
def get_type(value) -> str: | ||
def get_type(value: Any) -> str: | ||
"""Get item type as a string.""" | ||
return type(value).__name__ | ||
|
||
|
||
@register.filter | ||
def items(value: Dict[str, Any]) -> ItemsView[str, Any]: | ||
"""Get dict items.""" | ||
return value.items() | ||
|
||
|
||
@register.filter | ||
def fields_with_help_texts(section_data: SectionData) -> Dict[str, DictItems]: | ||
"""Add help texts and iterate as dict items.""" | ||
|
||
def add_help_text(fields: NestedDict, help_texts: NestedDict) -> Dict[str, DictItems]: | ||
formatted_fields: Dict[str, DictItems] = {} | ||
|
||
for key, value in fields.items(): | ||
help_text: Union[str, NestedDict] = help_texts.get(key, "") | ||
|
||
if isinstance(value, dict): | ||
if isinstance(help_text, str): | ||
formatted_fields[key] = (add_help_text(value, {}), help_text) | ||
else: | ||
formatted_fields[key] = (add_help_text(value, help_text), "") | ||
elif isinstance(value, list): | ||
values = [] | ||
for item in value: | ||
if not isinstance(item, (dict, list)): | ||
values.append((item, "")) | ||
else: | ||
res = add_help_text(item, {} if isinstance(help_text, str) else help_text) | ||
if not isinstance(res, tuple): | ||
res = (res, "") | ||
values.append(res) | ||
|
||
formatted_fields[key] = (values, help_text if isinstance(help_text, str) else "") | ||
else: | ||
formatted_fields[key] = (value, help_text) | ||
|
||
return formatted_fields | ||
|
||
return add_help_text(section_data["fields"], section_data.get("help_texts", {})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "django-admin-data-views" | ||
version = "0.2.6" | ||
version = "0.3.0" | ||
description = "Add custom data views to django admin panel." | ||
authors = [ | ||
"Matti Lamppu <[email protected]>", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.