More multisites tweaks

This commit is contained in:
Iacopo Spalletti 2016-06-26 18:11:37 +02:00
parent 67e3fa0128
commit 8b248e7b0f
No known key found for this signature in database
GPG key ID: BDCBC2EB289F60C6
3 changed files with 57 additions and 16 deletions

View file

@ -88,16 +88,19 @@ class GenericDateQuerySet(AppHookConfigTranslatableQueryset):
return self.filter(models.Q(sites__isnull=True) | return self.filter(models.Q(sites__isnull=True) |
models.Q(sites=site.pk)) models.Q(sites=site.pk))
def published(self): def published(self, current_site=True):
queryset = self.published_future() queryset = self.published_future(current_site)
if self.start_date_field: if self.start_date_field:
return queryset.filter( return queryset.filter(
**{'%s__lte' % self.start_date_field: now()}) **{'%s__lte' % self.start_date_field: now()})
else: else:
return queryset return queryset
def published_future(self): def published_future(self, current_site=True):
queryset = self.on_site() if current_site:
queryset = self.on_site()
else:
queryset = self
if self.end_date_field: if self.end_date_field:
qfilter = ( qfilter = (
models.Q(**{'%s__gte' % self.end_date_field: now()}) | models.Q(**{'%s__gte' % self.end_date_field: now()}) |
@ -106,8 +109,11 @@ class GenericDateQuerySet(AppHookConfigTranslatableQueryset):
queryset = queryset.filter(qfilter) queryset = queryset.filter(qfilter)
return queryset.filter(**{self.publish_field: True}) return queryset.filter(**{self.publish_field: True})
def archived(self): def archived(self, current_site=True):
queryset = self.on_site() if current_site:
queryset = self.on_site()
else:
queryset = self
if self.end_date_field: if self.end_date_field:
qfilter = ( qfilter = (
models.Q(**{'%s__lte' % self.end_date_field: now()}) | models.Q(**{'%s__lte' % self.end_date_field: now()}) |
@ -116,11 +122,17 @@ class GenericDateQuerySet(AppHookConfigTranslatableQueryset):
queryset = queryset.filter(qfilter) queryset = queryset.filter(qfilter)
return queryset.filter(**{self.publish_field: True}) return queryset.filter(**{self.publish_field: True})
def available(self): def available(self, current_site=True):
return self.on_site().filter(**{self.publish_field: 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): def filter_by_language(self, language, current_site=True):
return self.active_translations(language_code=language).on_site() if current_site:
return self.active_translations(language_code=language).on_site()
else:
return self.active_translations(language_code=language)
class GenericDateTaggedManager(TaggedFilterItem, AppHookConfigTranslatableManager): class GenericDateTaggedManager(TaggedFilterItem, AppHookConfigTranslatableManager):

View file

@ -80,15 +80,15 @@ class BlogCategory(TranslatableModel):
@cached_property @cached_property
def linked_posts(self): 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 @cached_property
def count(self): def count(self):
return self.linked_posts.on_site().count() return self.linked_posts.published().count()
@cached_property @cached_property
def count_all_sites(self): 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): def get_absolute_url(self, lang=None):
if not lang: if not lang:
@ -392,7 +392,7 @@ class BasePostPlugin(CMSPlugin):
posts = posts.active_translations(language_code=language) posts = posts.active_translations(language_code=language)
if (published_only or not request or not getattr(request, 'toolbar', False) or if (published_only or not request or not getattr(request, 'toolbar', False) or
not request.toolbar.edit_mode): not request.toolbar.edit_mode):
posts = posts.published() posts = posts.published(current_site=self.current_site)
return posts.all() return posts.all()
@ -456,8 +456,10 @@ class AuthorEntriesPlugin(BasePostPlugin):
if self.app_config: if self.app_config:
qs = qs.namespace(self.app_config.namespace) qs = qs.namespace(self.app_config.namespace)
if self.current_site: if self.current_site:
qs = qs.on_site() qs = qs.published()
count = qs.filter(publish=True).count() else:
qs = qs.published(current_site=False)
count = qs.count()
if count: if count:
author.count = count author.count = count
return authors return authors

View file

@ -438,6 +438,33 @@ class AdminTest(BaseTest):
class ModelsTest(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): def test_model_attributes(self):
self.get_pages() self.get_pages()