'Usage' page now displays a table of pages. Add new 'USAGE_COUNT' setting.

This commit is contained in:
Tom Talbot 2014-07-10 11:23:18 +01:00
parent f4fd3ab88f
commit ae0d26e71b
4 changed files with 133 additions and 32 deletions

View file

@ -1,21 +1,13 @@
from wagtail.wagtailcore.models import Page
def usage_count(self):
"""The number of times that an obect has been used"""
count = 0
relations = self._meta.get_all_related_objects(
include_hidden=True,
include_proxy_eq=True
)
for relation in relations:
count += relation.model._base_manager.filter(
**{relation.field.name: self.id}
).count()
return count
from django.conf import settings
def used_by(self):
"""Returns the pages that an object was used in."""
if not hasattr(settings, 'USAGE_COUNT') or not settings.USAGE_COUNT:
return []
related_objects = []
result = []
@ -34,3 +26,12 @@ def used_by(self):
result.append(r.page)
return result
def usage_count(self):
"""Returns the number of times that an obect has been used in a page"""
if not hasattr(settings, 'USAGE_COUNT') or not settings.USAGE_COUNT:
return None
return len(used_by(self))

View file

@ -6,12 +6,45 @@
{% include "wagtailadmin/shared/header.html" with title="Usage" %}
<div class="nice-padding">
<ul class="listing">
{% for u in document.used_by %}
<li>
<a href="{{ u.url }}">{{ u.title }}</a>
</li>
<section>
<table class="listing">
<col />
<col width="30%"/>
<col width="15%"/>
<col width="15%"/>
<thead>
<tr>
<th class="title">{% trans "Title" %}</th>
<th>{% trans "Parent" %}</th>
<th>{% trans "Type" %}</th>
<th>{% trans "Status" %}</th>
</tr>
</thead>
<tbody>
{% for page in document.used_by %}
<tr>
<td class="title" valign="top">
<h2><a href="{% url 'wagtailadmin_pages_edit' page.id %}" title="{% trans 'Edit this page' %}">{{ page.title }}</a></h2>
</td>
<td>
{% if page.get_parent %}
<a href="{% url 'wagtailadmin_explore' page.get_parent.id %}">{{ page.get_parent.title }}</a>
{% endif %}
</td>
<td>
{{ page.content_type.model_class.get_verbose_name }}
</td>
<td>
{% if page.live %}
<a href="{{ page.url }}" target="_blank" class="status-tag {% if page.status_string != "draft" %}primary{% endif %}">{{ page.status_string }}</a>
{% else %}
<span class="status-tag {% if page.status_string != "draft" %}primary{% endif %}">{{ page.status_string }}</span>
{% endif %}
</td>
</tr>
{% endfor %}
</ul>
</tbody>
</table>
</section>
</div>
{% endblock %}

View file

@ -6,12 +6,45 @@
{% include "wagtailadmin/shared/header.html" with title="Usage" %}
<div class="nice-padding">
<ul class="listing">
{% for u in image.used_by %}
<li>
<a href="{{ u.url }}">{{ u.title }}</a>
</li>
<section>
<table class="listing">
<col />
<col width="30%"/>
<col width="15%"/>
<col width="15%"/>
<thead>
<tr>
<th class="title">{% trans "Title" %}</th>
<th>{% trans "Parent" %}</th>
<th>{% trans "Type" %}</th>
<th>{% trans "Status" %}</th>
</tr>
</thead>
<tbody>
{% for page in image.used_by %}
<tr>
<td class="title" valign="top">
<h2><a href="{% url 'wagtailadmin_pages_edit' page.id %}" title="{% trans 'Edit this page' %}">{{ page.title }}</a></h2>
</td>
<td>
{% if page.get_parent %}
<a href="{% url 'wagtailadmin_explore' page.get_parent.id %}">{{ page.get_parent.title }}</a>
{% endif %}
</td>
<td>
{{ page.content_type.model_class.get_verbose_name }}
</td>
<td>
{% if page.live %}
<a href="{{ page.url }}" target="_blank" class="status-tag {% if page.status_string != "draft" %}primary{% endif %}">{{ page.status_string }}</a>
{% else %}
<span class="status-tag {% if page.status_string != "draft" %}primary{% endif %}">{{ page.status_string }}</span>
{% endif %}
</td>
</tr>
{% endfor %}
</ul>
</tbody>
</table>
</section>
</div>
{% endblock %}

View file

@ -6,12 +6,46 @@
{% include "wagtailadmin/shared/header.html" with title="Usage" %}
<div class="nice-padding">
<ul class="listing">
{% for u in document.used_by %}
<li>
<a href="{{ u.url }}">{{ u.title }}</a>
</li>
<section>
<table class="listing">
<col />
<col width="30%"/>
<col width="15%"/>
<col width="15%"/>
<thead>
<tr>
<th class="title">{% trans "Title" %}</th>
<th>{% trans "Parent" %}</th>
<th>{% trans "Type" %}</th>
<th>{% trans "Status" %}</th>
</tr>
</thead>
<tbody>
{% for page in instance.used_by %}
<tr>
<td class="title" valign="top">
<h2><a href="{% url 'wagtailadmin_pages_edit' page.id %}" title="{% trans 'Edit this page' %}">{{ page.title }}</a></h2>
</td>
<td>
{% if page.get_parent %}
<a href="{% url 'wagtailadmin_explore' page.get_parent.id %}">{{ page.get_parent.title }}</a>
{% endif %}
</td>
<td>
{{ page.content_type.model_class.get_verbose_name }}
</td>
<td>
{% if page.live %}
<a href="{{ page.url }}" target="_blank" class="status-tag {% if page.status_string != "draft" %}primary{% endif %}">{{ page.status_string }}</a>
{% else %}
<span class="status-tag {% if page.status_string != "draft" %}primary{% endif %}">{{ page.status_string }}</span>
{% endif %}
</td>
</tr>
{% endfor %}
</ul>
</tbody>
</table>
</section>
</div>
{% endblock %}