Skip to content

Commit

Permalink
Merge branch '146-display-calculated-usage'
Browse files Browse the repository at this point in the history
Conflicts:
	wagtail/wagtailimages/models.py
	wagtail/wagtailimages/templates/wagtailimages/images/edit.html
	wagtail/wagtailimages/tests.py
	wagtail/wagtailimages/urls.py
	wagtail/wagtailimages/views/images.py
  • Loading branch information
kaedroho committed Jul 31, 2014
2 parents ea5550c + 92e249b commit 1ac807c
Show file tree
Hide file tree
Showing 24 changed files with 501 additions and 25 deletions.
19 changes: 8 additions & 11 deletions wagtail/tests/fixtures/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@
"submit_time": "2014-01-01T12:00:00.000Z"
}
},
{
"pk": 1,
"model": "tests.AdvertPlacement",
"fields": {
"page": 2,
"advert": 1
}
},
{
"pk": 1,
"model": "tests.advert",
Expand All @@ -578,17 +586,6 @@
"created_at": "2014-01-01T12:00:00.000Z"
}
},
{
"pk": 1,
"model": "wagtailimages.Image",
"fields": {
"title": "test image",
"created_at": "2014-01-01T12:00:00.000Z",
"width": 0,
"height": 0
}
},

{
"pk": 1,
"model": "wagtailcore.pageviewrestriction",
Expand Down
4 changes: 3 additions & 1 deletion wagtail/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,11 @@ class FormPage(AbstractEmailForm):
]


# Snippets

# Snippets
class AdvertPlacement(models.Model):
page = ParentalKey('wagtailcore.Page', related_name='advert_placements')
advert = models.ForeignKey('tests.Advert', related_name='+')

@python_2_unicode_compatible
class Advert(models.Model):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% load i18n wagtailadmin_tags %}
<header class="nice-padding {% if merged %}merged{% endif %} {% if tabbed %}tab-merged{% endif %} {% if search_form %}hasform{% endif %}">
<div class="row">
<div class="left">
Expand All @@ -16,11 +17,17 @@ <h1 class="icon icon-{{ icon }}">{{ title }} <span>{{ subtitle }}</span></h1>
{% endif %}
</div>
<div class="right">
{% usage_count_enabled as uc_enabled %}
{% if uc_enabled and usage_object %}
<div class="usagecount">
<a href="{{ usage_object.usage_url }}">{% blocktrans count useage_count=usage_object.get_usage.count %}Used {{ useage_count }} time{% plural %}Used {{ useage_count }} times{% endblocktrans %}</a>
</div>
{% endif %}
{% if add_link %}
<div class="addbutton">
<a href="{% url add_link %}" class="button bicolor icon icon-plus">{{ add_text }}</a>
</div>
{% endif %}
</div>
</div>
</header>
</header>
11 changes: 11 additions & 0 deletions wagtail/wagtailadmin/templatetags/wagtailadmin_tags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

from django.conf import settings
from django import template
from django.core import urlresolvers
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -121,3 +122,13 @@ def hook_output(hook_name):
"""
snippets = [fn() for fn in hooks.get_hooks(hook_name)]
return ''.join(snippets)


@register.assignment_tag
def usage_count_enabled():
if hasattr(
settings, 'WAGTAIL_USAGE_COUNT_ENABLED'
) and settings.WAGTAIL_USAGE_COUNT_ENABLED:
return True
else:
return False
5 changes: 3 additions & 2 deletions wagtail/wagtailadmin/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.core import mail

from wagtail.tests.utils import WagtailTestUtils
from wagtail.wagtailcore.models import Page
from wagtail.wagtailadmin.tasks import send_email_task
from django.core.urlresolvers import reverse
from django.core import mail


class TestHome(TestCase, WagtailTestUtils):
Expand Down
36 changes: 36 additions & 0 deletions wagtail/wagtailadmin/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from modelcluster.fields import ParentalKey

from wagtail.wagtailcore.models import Page


def get_object_usage(obj):
"Returns a queryset of pages that link to a particular object"

pages = Page.objects.none()

# get all the relation objects for obj
relations = type(obj)._meta.get_all_related_objects(
include_hidden=True,
include_proxy_eq=True
)
for relation in relations:
# if the relation is between obj and a page, get the page
if issubclass(relation.model, Page):
pages |= Page.objects.filter(
id__in=relation.model._base_manager.filter(**{
relation.field.name: obj.id
}).values_list('id', flat=True)
)
else:
# if the relation is between obj and an object that has a page as a
# property, return the page
for f in relation.model._meta.fields:
if isinstance(f, ParentalKey) and issubclass(f.rel.to, Page):
pages |= Page.objects.filter(
id__in=relation.model._base_manager.filter(
**{
relation.field.name: obj.id
}).values_list(f.attname, flat=True)
)

return pages
1 change: 1 addition & 0 deletions wagtail/wagtaildocs/admin_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
url(r'^chooser/$', chooser.chooser, name='wagtaildocs_chooser'),
url(r'^chooser/(\d+)/$', chooser.document_chosen, name='wagtaildocs_document_chosen'),
url(r'^chooser/upload/$', chooser.chooser_upload, name='wagtaildocs_chooser_upload'),
url(r'^usage/(\d+)/$', documents.usage, name='wagtaildocs_document_usage'),
]
9 changes: 9 additions & 0 deletions wagtail/wagtaildocs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.utils.encoding import python_2_unicode_compatible

from wagtail.wagtailadmin.taggable import TagSearchable
from wagtail.wagtailadmin.utils import get_object_usage
from wagtail.wagtailsearch import indexed


Expand Down Expand Up @@ -47,6 +48,14 @@ def file_extension(self):
def url(self):
return reverse('wagtaildocs_serve', args=[self.id, self.filename])

def get_usage(self):
return get_object_usage(self)

@property
def usage_url(self):
return reverse('wagtaildocs_document_usage',
args=(self.id,))

def is_editable_by_user(self, user):
if user.has_perm('wagtaildocs.change_document'):
# user has global permission to change documents
Expand Down
4 changes: 2 additions & 2 deletions wagtail/wagtaildocs/templates/wagtaildocs/documents/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

{% block content %}
{% trans "Editing" as editing_str %}
{% include "wagtailadmin/shared/header.html" with title=editing_str subtitle=document.title icon="doc-full-inverse" %}
{% include "wagtailadmin/shared/header.html" with title=editing_str subtitle=document.title icon="doc-full-inverse" usage_object=document %}

<div class="nice-padding">
<form action="{% url 'wagtaildocs_edit_document' document.id %}" method="POST" enctype="multipart/form-data">
Expand All @@ -30,6 +30,6 @@
</ul>
</form>


</div>
{% endblock %}
49 changes: 49 additions & 0 deletions wagtail/wagtaildocs/templates/wagtaildocs/documents/usage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% extends "wagtailadmin/base.html" %}
{% load i18n %}
{% block titletag %}{% blocktrans with title=document.title %}Usage of {{ title }}{% endblocktrans %}{% endblock %}
{% block content %}
{% trans "Usage of" as usage_str %}
{% include "wagtailadmin/shared/header.html" with title=usage_str subtitle=document.title %}

<div class="nice-padding">
<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 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 %}
</tbody>
</table>
</div>
{% include "wagtailadmin/shared/pagination_nav.html" with items=used_by linkurl="-" %}
{% endblock %}
101 changes: 101 additions & 0 deletions wagtail/wagtaildocs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
from django.contrib.auth.models import Group, Permission
from django.core.urlresolvers import reverse
from django.core.files.base import ContentFile
from django.test.utils import override_settings

from wagtail.tests.utils import WagtailTestUtils
from wagtail.wagtailcore.models import Page

from wagtail.tests.models import EventPage, EventPageRelatedLink
from wagtail.wagtaildocs.models import Document

from wagtail.wagtaildocs import models

Expand Down Expand Up @@ -316,3 +321,99 @@ def test_file_extension(self):
def tearDown(self):
self.document.delete()
self.extensionless_document.delete()


class TestUsageCount(TestCase, WagtailTestUtils):
fixtures = ['wagtail/tests/fixtures/test.json']

def setUp(self):
self.login()

@override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
def test_unused_document_usage_count(self):
doc = Document.objects.get(id=1)
self.assertEqual(doc.get_usage().count(), 0)

@override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
def test_used_document_usage_count(self):
doc = Document.objects.get(id=1)
page = EventPage.objects.get(id=4)
event_page_related_link = EventPageRelatedLink()
event_page_related_link.page = page
event_page_related_link.link_document = doc
event_page_related_link.save()
self.assertEqual(doc.get_usage().count(), 1)

def test_usage_count_does_not_appear(self):
doc = Document.objects.get(id=1)
page = EventPage.objects.get(id=4)
event_page_related_link = EventPageRelatedLink()
event_page_related_link.page = page
event_page_related_link.link_document = doc
event_page_related_link.save()
response = self.client.get(reverse('wagtaildocs_edit_document',
args=(1,)))
self.assertNotContains(response, 'Used 1 time')

@override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
def test_usage_count_appears(self):
doc = Document.objects.get(id=1)
page = EventPage.objects.get(id=4)
event_page_related_link = EventPageRelatedLink()
event_page_related_link.page = page
event_page_related_link.link_document = doc
event_page_related_link.save()
response = self.client.get(reverse('wagtaildocs_edit_document',
args=(1,)))
self.assertContains(response, 'Used 1 time')

@override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
def test_usage_count_zero_appears(self):
response = self.client.get(reverse('wagtaildocs_edit_document',
args=(1,)))
self.assertContains(response, 'Used 0 times')


class TestGetUsage(TestCase, WagtailTestUtils):
fixtures = ['wagtail/tests/fixtures/test.json']

def setUp(self):
self.login()

def test_document_get_usage_not_enabled(self):
doc = Document.objects.get(id=1)
self.assertEqual(list(doc.get_usage()), [])

@override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
def test_unused_document_get_usage(self):
doc = Document.objects.get(id=1)
self.assertEqual(list(doc.get_usage()), [])

@override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
def test_used_document_get_usage(self):
doc = Document.objects.get(id=1)
page = EventPage.objects.get(id=4)
event_page_related_link = EventPageRelatedLink()
event_page_related_link.page = page
event_page_related_link.link_document = doc
event_page_related_link.save()
self.assertTrue(issubclass(Page, type(doc.get_usage()[0])))

@override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
def test_usage_page(self):
doc = Document.objects.get(id=1)
page = EventPage.objects.get(id=4)
event_page_related_link = EventPageRelatedLink()
event_page_related_link.page = page
event_page_related_link.link_document = doc
event_page_related_link.save()
response = self.client.get(reverse('wagtaildocs_document_usage',
args=(1,)))
self.assertContains(response, 'Christmas')

@override_settings(WAGTAIL_USAGE_COUNT_ENABLED=True)
def test_usage_page_no_usage(self):
response = self.client.get(reverse('wagtaildocs_document_usage',
args=(1,)))
# There's no usage so there should be no table rows
self.assertRegex(response.content, b'<tbody>(\s|\n)*</tbody>')
24 changes: 23 additions & 1 deletion wagtail/wagtaildocs/views/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.exceptions import PermissionDenied
from django.utils.translation import ugettext as _
from django.views.decorators.vary import vary_on_headers
from django.core.urlresolvers import reverse

from wagtail.wagtailadmin.forms import SearchForm

Expand Down Expand Up @@ -120,7 +121,7 @@ def edit(request, document_id):

return render(request, "wagtaildocs/documents/edit.html", {
'document': doc,
'form': form,
'form': form
})


Expand All @@ -139,3 +140,24 @@ def delete(request, document_id):
return render(request, "wagtaildocs/documents/confirm_delete.html", {
'document': doc,
})


@permission_required('wagtailadmin.access_admin')
def usage(request, document_id):
doc = get_object_or_404(Document, id=document_id)

# Pagination
p = request.GET.get('p', 1)
paginator = Paginator(doc.get_usage(), 20)

try:
used_by = paginator.page(p)
except PageNotAnInteger:
used_by = paginator.page(1)
except EmptyPage:
used_by = paginator.page(paginator.num_pages)

return render(request, "wagtaildocs/documents/usage.html", {
'document': doc,
'used_by': used_by
})
Loading

0 comments on commit 1ac807c

Please sign in to comment.