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

Commit

Permalink
Fix JobCategoriesPlugin counters
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykhailo Kolesnyk committed Apr 14, 2016
1 parent 677e436 commit 42704b0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
15 changes: 8 additions & 7 deletions aldryn_jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ def get_absolute_url(self, language=None):
def get_notification_emails(self):
return self.supervisors.values_list('email', flat=True)

# We keep this 'count' name for compatibility in templates:
# there used to be annotate() call with the same property name.
def count(self):
return JobOpening.objects.filter(category=self).active().count()


@version_controlled_content(follow=['category'])
@python_2_unicode_compatible
Expand Down Expand Up @@ -400,10 +405,6 @@ 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')
)
categories_qs = JobCategory.objects.namespace(
self.app_config.namespace).order_by('ordering')
return (category for category in categories_qs if category.count())
54 changes: 50 additions & 4 deletions aldryn_jobs/tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from datetime import timedelta

from django.utils.translation import override
from django.utils.timezone import now

from cms import api

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

from .base import JobsBaseTestCase

Expand Down Expand Up @@ -139,16 +143,58 @@ class TestJobCategoriesListPlugin(TestAppConfigPluginsMixin,

def setUp(self):
super(TestJobCategoriesListPlugin, self).setUp()
# 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)
self.plugin_en = self.create_plugin(
self.plugin_page, 'en', self.app_config)

with override('en'):
self.another_category = JobCategory.objects.create(
name='Another category',
app_config=self.app_config)
self.empty_category = JobCategory.objects.create(
name='Empty category',
app_config=self.app_config)

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

def test_categories_list_plugin_counters(self):
"""Test JobCategory plugin job openings count"""

with override('en'):
JobOpening.objects.create(
title='job active',
category=self.default_category)
JobOpening.objects.create(
title='job inactive',
is_active=False,
category=self.default_category)
JobOpening.objects.create(
title='job future',
publication_start=now() + timedelta(days=1),
category=self.default_category)
JobOpening.objects.create(
title='job past',
publication_start=now() - timedelta(days=2),
publication_end=now() - timedelta(days=1),
category=self.default_category)
JobOpening.objects.create(
title='job in another category',
category=self.another_category)
page_url = self.plugin_page.get_absolute_url()

response = self.client.get(page_url)

self.assertContains(response, self.default_category.name)
self.assertContains(response, self.another_category.name)
self.assertNotContains(response, self.empty_category.name)

self.assertEquals(self.default_category.count(), 1)
self.assertEquals(self.another_category.count(), 1)
self.assertEquals(self.empty_category.count(), 0)


class TestJobListPlugin(TestAppConfigPluginsMixin,
TestPluginFailuresWithDeletedAppHookMixin,
Expand Down

0 comments on commit 42704b0

Please sign in to comment.