Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Fix category list plugin openings count, +test case, fix plugin test cases #117

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
{% endif %}
{% else %}
<div class="list-group">
{% for category in instance.categories %}
<a href="{% namespace_url "category-job-opening-list" category.slug namespace=instance.app_config.namespace %}" class="list-group-item">
{{ category.name }}
<span class="badge pull-right">{{ category.count }}</span>
{% for data in instance.categories %}
<a href="{% namespace_url "category-job-opening-list" data.category.slug namespace=instance.app_config.namespace %}" class="list-group-item">
{{ data.category.name }}
<span class="badge pull-right">{{ data.count }}</span>
</a>
{% empty %}
<div class="list-group-item">{% trans "No items available" %}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
{% endif %}
{% else %}
<ul class="list-unstyled">
{% for category in instance.categories %}
{% for data in instance.categories %}
<li>
<span class="badge">{{ category.count }}</span>
<a href="{% namespace_url "category-job-opening-list" category.slug namespace=instance.app_config.namespace %}">{{ category.name }}</a>
<span class="badge">{{ data.count }}</span>
<a href="{% namespace_url "category-job-opening-list" data.category.slug namespace=instance.app_config.namespace %}">{{ data.category.name }}</a>
</li>
{% endfor %}
</ul>
Expand Down
28 changes: 21 additions & 7 deletions aldryn_jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,24 @@ def __str__(self):

@property
def categories(self):
return (
JobCategory.objects
.namespace(self.app_config.namespace)
.annotate(count=models.Count('jobs'))
.filter(count__gte=1)
.order_by('ordering', '-count')
)
"""
Prepare list (respect ordering) of dict with 'category' and 'count' of
category vacancies (active with respect to namespace, language and
fall backs configuration.
"""
categories_qs = (
JobCategory.objects.namespace(self.app_config.namespace)
.active_translations(self.language)
.order_by('ordering'))
openings_qs = (
JobOpening.objects.namespace(self.app_config.namespace)
.language(self.language)
.active_translations(self.language)
.active())
categories = []
for category in categories_qs:
categories.append({
'category': category,
'count': openings_qs.filter(category=category).count()
})
return categories
70 changes: 64 additions & 6 deletions aldryn_jobs/tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.conf import settings
from django.utils.translation import override
from cms import api

from ..models import JobCategory, JobsConfig
from ..models import JobCategory, JobsConfig, JobOpening

from .base import JobsBaseTestCase

Expand Down Expand Up @@ -35,13 +36,10 @@ def _create_plugin(self, page, language, app_config, **plugin_params):
Assumes that page has that translation.
"""
placeholder = page.placeholders.all()[0]
api.add_plugin(
plugin = api.add_plugin(
placeholder, self.plugin_to_test, language,
app_config=app_config, **plugin_params)
plugin = placeholder.get_plugins().filter(
language=language)[0].get_plugin_instance()[0]
plugin.save()
page.publish(self.language)
page.publish(language)
return plugin


Expand Down Expand Up @@ -145,13 +143,73 @@ def setUp(self):
# create plugin for both languages
self.create_plugin(self.plugin_page, 'en', self.app_config)
self.create_plugin(self.plugin_page, 'de', self.app_config)
# prepare boilerplate stuff to know what to look for
self.boilerplate = getattr(settings,
'ALDRYN_BOILERPLATE_NAME',
'legacy')
self.lookup_strings = {
'bootstrap3': '<span class="badge pull-right">{0}</span>',
'legacy': '<span class="badge">{0}</span>'
}
self.lookup_string = self.lookup_strings.get(self.boilerplate)

def create_plugin(self, page, language, app_config,
**plugin_params):
plugin = self._create_plugin(
page, language, app_config, **plugin_params)
return plugin

def test_plugin_shows_correct_opening_counts_per_language(self):
# we need to check against de translation, because if there is fallbacks
# then even though opening exists only with en translation it would be
# shown anyway.
data = self.default_job_values['de']
data['category'] = self.default_category

with override('de'):
JobOpening.objects.create(**data)
page_url = self.plugin_page.get_absolute_url()
response = self.client.get(page_url)
# de language contains 1 opening
self.assertContains(response, self.lookup_string.format(1))

with override('en'):
page_url = self.plugin_page.get_absolute_url()
response = self.client.get(page_url)
# en language should contain 0 openings
self.assertContains(response, self.lookup_string.format(0))

def test_plugin_shows_correct_openings_counts_per_config(self):
self.create_default_job_opening(translated=True)
# prepare other apphook category and opening.
new_config = JobsConfig.objects.create(namespace='different_namespace')

# prepare apphook and other category opening, they won't be used
# directly
self.create_page(
title='new apphook', slug='new-apphook',
namespace=new_config.namespace)
new_category = JobCategory.objects.create(
name='Completely different namespace',
slug='completely-different-namespace',
app_config=new_config)
self.create_new_job_opening(
self.prepare_data(1, category=new_category))

# test the default category, should contain only openings that belong
# to it
with override('de'):
page_url = self.plugin_page.get_absolute_url()
response = self.client.get(page_url)
# de language contains 1 opening
self.assertContains(response, self.lookup_string.format(1))

with override('en'):
page_url = self.plugin_page.get_absolute_url()
response = self.client.get(page_url)
# en language should contain 1 opening
self.assertContains(response, self.lookup_string.format(1))


class TestJobListPlugin(TestAppConfigPluginsMixin,
TestPluginFailuresWithDeletedAppHookMixin,
Expand Down