diff --git a/djangocms_blog/managers.py b/djangocms_blog/managers.py index 86c4214..f7e6ac9 100644 --- a/djangocms_blog/managers.py +++ b/djangocms_blog/managers.py @@ -88,16 +88,19 @@ class GenericDateQuerySet(AppHookConfigTranslatableQueryset): return self.filter(models.Q(sites__isnull=True) | models.Q(sites=site.pk)) - def published(self): - queryset = self.published_future() + def published(self, current_site=True): + queryset = self.published_future(current_site) if self.start_date_field: return queryset.filter( **{'%s__lte' % self.start_date_field: now()}) else: return queryset - def published_future(self): - queryset = self.on_site() + def published_future(self, current_site=True): + if current_site: + queryset = self.on_site() + else: + queryset = self if self.end_date_field: qfilter = ( models.Q(**{'%s__gte' % self.end_date_field: now()}) | @@ -106,8 +109,11 @@ class GenericDateQuerySet(AppHookConfigTranslatableQueryset): queryset = queryset.filter(qfilter) return queryset.filter(**{self.publish_field: True}) - def archived(self): - queryset = self.on_site() + def archived(self, current_site=True): + if current_site: + queryset = self.on_site() + else: + queryset = self if self.end_date_field: qfilter = ( models.Q(**{'%s__lte' % self.end_date_field: now()}) | @@ -116,11 +122,17 @@ class GenericDateQuerySet(AppHookConfigTranslatableQueryset): queryset = queryset.filter(qfilter) return queryset.filter(**{self.publish_field: True}) - def available(self): - return self.on_site().filter(**{self.publish_field: True}) + def available(self, current_site=True): + if current_site: + return self.on_site().filter(**{self.publish_field: True}) + else: + return self.filter(**{self.publish_field: True}) - def filter_by_language(self, language): - return self.active_translations(language_code=language).on_site() + def filter_by_language(self, language, current_site=True): + if current_site: + return self.active_translations(language_code=language).on_site() + else: + return self.active_translations(language_code=language) class GenericDateTaggedManager(TaggedFilterItem, AppHookConfigTranslatableManager): diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 2878b09..91df778 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -80,15 +80,15 @@ class BlogCategory(TranslatableModel): @cached_property def linked_posts(self): - return self.blog_posts.namespace(self.app_config.namespace).published() + return self.blog_posts.namespace(self.app_config.namespace) @cached_property def count(self): - return self.linked_posts.on_site().count() + return self.linked_posts.published().count() @cached_property def count_all_sites(self): - return self.linked_posts.count() + return self.linked_posts.published(current_site=False).count() def get_absolute_url(self, lang=None): if not lang: @@ -392,7 +392,7 @@ class BasePostPlugin(CMSPlugin): posts = posts.active_translations(language_code=language) if (published_only or not request or not getattr(request, 'toolbar', False) or not request.toolbar.edit_mode): - posts = posts.published() + posts = posts.published(current_site=self.current_site) return posts.all() @@ -456,8 +456,10 @@ class AuthorEntriesPlugin(BasePostPlugin): if self.app_config: qs = qs.namespace(self.app_config.namespace) if self.current_site: - qs = qs.on_site() - count = qs.filter(publish=True).count() + qs = qs.published() + else: + qs = qs.published(current_site=False) + count = qs.count() if count: author.count = count return authors diff --git a/tests/test_models.py b/tests/test_models.py index 9f6d9ec..dd25f07 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -438,6 +438,33 @@ class AdminTest(BaseTest): class ModelsTest(BaseTest): + def test_category_attributes(self): + posts = self.get_posts() + posts[0].publish = True + posts[0].save() + posts[1].publish = True + posts[1].save() + posts[1].sites.add(self.site_2) + new_category = BlogCategory.objects.create( + name='category 2', app_config=self.app_config_1 + ) + posts[1].categories.add(new_category) + + with self.settings(SITE_ID=2): + self.assertEqual(new_category.count, 1) + self.assertEqual(self.category_1.count, 2) + self.assertEqual(new_category.count_all_sites, 1) + self.assertEqual(self.category_1.count_all_sites, 2) + + # needed to clear cached properties + new_category = self.reload_model(new_category) + self.category_1 = self.reload_model(self.category_1) + with self.settings(SITE_ID=1): + self.assertEqual(new_category.count, 0) + self.assertEqual(self.category_1.count, 1) + self.assertEqual(new_category.count_all_sites, 1) + self.assertEqual(self.category_1.count_all_sites, 2) + def test_model_attributes(self): self.get_pages()