Skip to content

Commit

Permalink
Merge pull request #2 from cfpb/good-parenting
Browse files Browse the repository at this point in the history
Support parent selection in breadcrumb navigation and new child creation
  • Loading branch information
willbarton authored May 18, 2018
2 parents 2fee79c + 081533b commit 0345140
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 134 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Please add issues to the [issue tracker](https://github.com/cfpb/wagtail-treemod
General instructions on _how_ to contribute can be found in [CONTRIBUTING](CONTRIBUTING.md).

## Licensing

1. [TERMS](TERMS.md)
2. [LICENSE](LICENSE)
3. [CFPB Source Code Policy](https://github.com/cfpb/source-code-policy/)
Expand Down
47 changes: 46 additions & 1 deletion treemodeladmin/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
from wagtail.contrib.modeladmin.helpers import ButtonHelper
from django.contrib.admin.utils import quote
from django.utils.encoding import force_text

from wagtail.contrib.modeladmin.helpers import AdminURLHelper, ButtonHelper


class TreeAdminURLHelper(AdminURLHelper):

def get_create_url_with_parent(self, parent_field, parent_pk):
return '{create_url}?{parent_field}={parent_pk}'.format(
create_url=self.create_url,
parent_field=parent_field,
parent_pk=quote(parent_pk)
)

def get_index_url_with_parent(self, parent_field, parent_pk):
return '{index_url}?{parent_field}={parent_pk}'.format(
index_url=self.index_url,
parent_field=parent_field,
parent_pk=quote(parent_pk)
)

def crumb(self, parent_field=None, parent_instance=None):
if parent_field is not None and parent_instance is not None:
index_url = self.get_index_url_with_parent(
parent_field,
parent_instance.pk
)
else:
index_url = self.index_url

return (
index_url,
force_text(self.opts.verbose_name_plural)
)


class TreeButtonHelper(ButtonHelper):
Expand All @@ -7,3 +41,14 @@ def add_button(self, classnames_add=None, classnames_exclude=None):
return super(TreeButtonHelper, self).add_button(
classnames_add=['button-small']
)

def get_add_button_with_parent(self, parent_field, parent_pk,
classnames_add=None,
classnames_exclude=None):
add_button = self.add_button(classnames_add=classnames_add,
classnames_exclude=classnames_exclude)
add_button['url'] = self.url_helper.get_create_url_with_parent(
parent_field,
parent_pk
)
return add_button
13 changes: 11 additions & 2 deletions treemodeladmin/options.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from wagtail.contrib.modeladmin.options import ModelAdmin

from treemodeladmin.helpers import TreeButtonHelper
from treemodeladmin.views import TreeIndexView
from treemodeladmin.helpers import TreeAdminURLHelper, TreeButtonHelper
from treemodeladmin.views import (
TreeCreateView,
TreeDeleteView,
TreeEditView,
TreeIndexView,
)


class TreeModelAdmin(ModelAdmin):
Expand All @@ -10,8 +15,12 @@ class TreeModelAdmin(ModelAdmin):
child_instance = None
parent_field = None
index_view_class = TreeIndexView
create_view_class = TreeCreateView
delete_view_class = TreeDeleteView
edit_view_class = TreeEditView
index_template_name = 'treemodeladmin/index.html'
button_helper_class = TreeButtonHelper
url_helper_class = TreeAdminURLHelper

def __init__(self, parent=None):
super(TreeModelAdmin, self).__init__(parent=parent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="col">
{% block h1 %}<h1 {% if view.header_icon %}class="icon icon-{{ view.header_icon }}"{% endif %}>{{ view.get_page_title }}<span></span></h1>{% endblock %}
{% include 'modeladmin/includes/button.html' with button=view.get_parent_edit_button %}
{% include 'modeladmin/includes/button.html' with button=view.button_helper.add_button %}
{% include 'modeladmin/includes/button.html' with button=view.get_add_button_with_parent %}
</div>
{% block search %}{% search_form %}{% endblock %}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% endfor %}
{% if children.count > 0 %}
<td class="children">
<a class="icon text-replace icon-arrow-right" title="Explore {{ obj }}'s {{ view.child_name_plural }}" href="{{ child_index_url }}?{{child_filter}}">Explore {{ obj }}'s {{ view.child_name_plural }}</a>
<a class="icon text-replace icon-arrow-right" title="Explore {{ obj }}'s {{ view.child_name_plural }}" href="{{ child_index_url }}">Explore {{ obj }}'s {{ view.child_name_plural }}</a>
</td>
{% else %}
<td class="no-children">
Expand Down
96 changes: 25 additions & 71 deletions treemodeladmin/templates/treemodeladmin/index.html
Original file line number Diff line number Diff line change
@@ -1,77 +1,31 @@
{% extends "wagtailadmin/base.html" %}
{% extends "modeladmin/index.html" %}
{% load i18n modeladmin_tags treemodeladmin_tags %}

{% block titletag %}{{ view.get_meta_title }}{% endblock %}

{% block css %}
{{ block.super }}
{{ view.media.css }}
{% endblock %}

{% block extra_js %}
{{ view.media.js }}
{% block header %}
{% include "treemodeladmin/includes/header.html" %}
{% endblock %}

{% block content %}
{% block header %}
{% include "treemodeladmin/includes/header.html" %}
{% endblock %}

{% block content_main %}
<div>
<div class="row">
{% block content_cols %}

{% block filters %}
{% if view.has_filters and all_count %}
<div class="changelist-filter col3">
<h2>{% trans 'Filter' %}</h2>
{% for spec in view.filter_specs %}{% admin_list_filter view spec %}{% endfor %}
</div>
{% endif %}
{% endblock %}

<div class="result-list {% if view.has_filters and all_count %}col9{% else %}col12{% endif %}">
{% block result_list %}
{% if not all_count %}
<div class="nice-padding" style="margin-top:30px;">
{% if no_valid_parents %}
<p>{% blocktrans with view.verbose_name_plural as name %}No {{ name }} have been created yet. One of the following must be created before you can add any {{ name }}:{% endblocktrans %}</p>
<ul>
{% for type in required_parent_types %}<li><b>{{ type|title }}</b></li>{% endfor %}
</ul>
{% else %}
<p>{% blocktrans with view.verbose_name_plural as name %}No {{ name }} have been created yet.{% endblocktrans %}
{% if user_can_create %}
{% blocktrans with view.create_url as url %}
Why not <a href="{{ url }}">add one</a>?
{% endblocktrans %}
{% endif %}</p>
{% endif %}
</div>
{% elif view.has_child %}
{% tree_result_list %}
{% else %}
{% result_list %}
{% endif %}
{% endblock %}
</div>

{% block pagination %}
<div class="pagination {% if view.has_filters and all_count %}col9{% else %}col12{% endif %}">
<p>{% blocktrans with page_obj.number as current_page and paginator.num_pages as num_pages %}Page {{ current_page }} of {{ num_pages }}.{% endblocktrans %}</p>
{% if paginator.num_pages > 1 %}
<ul>
{% pagination_link_previous page_obj view %}
{% pagination_link_next page_obj view %}
</ul>
{% endif %}
</div>
{% endblock %}

{% endblock %}
</div>
{% block result_list %}
{% if not all_count %}
<div class="nice-padding" style="margin-top:30px;">
{% if no_valid_parents %}
<p>{% blocktrans with view.verbose_name_plural as name %}No {{ name }} have been created yet. One of the following must be created before you can add any {{ name }}:{% endblocktrans %}</p>
<ul>
{% for type in required_parent_types %}<li><b>{{ type|title }}</b></li>{% endfor %}
</ul>
{% else %}
<p>{% blocktrans with view.verbose_name_plural as name %}No {{ name }} have been created yet.{% endblocktrans %}
{% if user_can_create %}
{% blocktrans with view.create_url as url %}
Why not <a href="{{ url }}">add one</a>?
{% endblocktrans %}
{% endif %}</p>
{% endif %}
</div>
{% endblock %}

{% elif view.has_child %}
{% tree_result_list %}
{% else %}
{% result_list %}
{% endif %}
{% endblock %}

11 changes: 7 additions & 4 deletions treemodeladmin/templatetags/treemodeladmin_tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib.admin.utils import quote
from django.template import Library

from wagtail.contrib.modeladmin.templatetags.modeladmin_tags import (
Expand All @@ -24,13 +23,17 @@ def tree_result_row_display(context, index):
context = result_row_display(context, index)
obj = context['object_list'][index]
view = context['view']
child_url_helper = view.child_url_helper

if view.has_child_admin:
context.update({
'children': view.get_children(obj),
'child_index_url': view.child_url_helper.index_url,
'child_filter': view.get_child_filter(quote(obj.pk)),
'child_create_url': view.child_url_helper.create_url,
'child_index_url': child_url_helper.get_index_url_with_parent(
view.child_model_admin.parent_field, obj.pk
),
'child_create_url': child_url_helper.get_create_url_with_parent(
view.child_model_admin.parent_field, obj.pk
),
})

return context
Loading

0 comments on commit 0345140

Please sign in to comment.