Manager refactorization
This commit is contained in:
parent
ae4d42573c
commit
403036600c
2 changed files with 45 additions and 26 deletions
|
@ -6,7 +6,7 @@ except ImportError:
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
from parler.managers import TranslationManager
|
from parler.managers import TranslationManager, TranslatableQuerySet
|
||||||
|
|
||||||
|
|
||||||
class TaggedFilterItem(object):
|
class TaggedFilterItem(object):
|
||||||
|
@ -68,29 +68,21 @@ class TaggedFilterItem(object):
|
||||||
return sorted(tags, key=lambda x: -x.count)
|
return sorted(tags, key=lambda x: -x.count)
|
||||||
|
|
||||||
|
|
||||||
class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
|
class GenericDateQuerySet(TranslatableQuerySet):
|
||||||
use_for_related_fields = True
|
|
||||||
start_date_field = 'date_published'
|
start_date_field = 'date_published'
|
||||||
end_date_field = 'date_published_end'
|
end_date_field = 'date_published_end'
|
||||||
publish_field = 'publish'
|
publish_field = 'publish'
|
||||||
|
|
||||||
def get_queryset(self, *args, **kwargs):
|
def published(self):
|
||||||
try:
|
queryset = self.published_future()
|
||||||
return super(GenericDateTaggedManager, self).get_queryset(*args, **kwargs)
|
|
||||||
except AttributeError:
|
|
||||||
return super(GenericDateTaggedManager, self).get_query_set(*args, **kwargs)
|
|
||||||
|
|
||||||
def published(self, queryset=None):
|
|
||||||
queryset = self.published_future(queryset)
|
|
||||||
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, queryset=None):
|
def published_future(self):
|
||||||
if queryset is None:
|
queryset = self
|
||||||
queryset = self.get_queryset().all()
|
|
||||||
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()})
|
||||||
|
@ -99,9 +91,8 @@ class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
|
||||||
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, queryset=None):
|
def archived(self):
|
||||||
if queryset is None:
|
queryset = self
|
||||||
queryset = self.get_queryset().all()
|
|
||||||
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()})
|
||||||
|
@ -110,13 +101,41 @@ class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
|
||||||
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, queryset=None):
|
def available(self):
|
||||||
if queryset is None:
|
return self.filter(**{self.publish_field: True})
|
||||||
queryset = self.get_queryset().all()
|
|
||||||
return queryset.filter(**{self.publish_field: True})
|
|
||||||
|
|
||||||
def filter_by_language(self, language):
|
def filter_by_language(self, language):
|
||||||
return self.get_queryset().active_translations(language_code=language)
|
return self.active_translations(language_code=language)
|
||||||
|
|
||||||
|
|
||||||
|
class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
|
||||||
|
use_for_related_fields = True
|
||||||
|
|
||||||
|
queryset_class = GenericDateQuerySet
|
||||||
|
|
||||||
|
def get_queryset(self, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
return super(GenericDateTaggedManager, self).get_queryset(*args, **kwargs)
|
||||||
|
except AttributeError:
|
||||||
|
return super(GenericDateTaggedManager, self).get_query_set(*args, **kwargs)
|
||||||
|
|
||||||
|
def get_query_set(self, *args, **kwargs):
|
||||||
|
return self.get_queryset(*args, **kwargs)
|
||||||
|
|
||||||
|
def published(self):
|
||||||
|
return self.get_queryset().published()
|
||||||
|
|
||||||
|
def available(self):
|
||||||
|
return self.get_queryset().available()
|
||||||
|
|
||||||
|
def archived(self):
|
||||||
|
return self.get_queryset().archived()
|
||||||
|
|
||||||
|
def published_future(self):
|
||||||
|
return self.get_queryset().published_future()
|
||||||
|
|
||||||
|
def filter_by_language(self, language):
|
||||||
|
return self.get_queryset().filter_by_language(language)
|
||||||
|
|
||||||
def get_months(self, queryset=None):
|
def get_months(self, queryset=None):
|
||||||
"""
|
"""
|
||||||
|
@ -124,7 +143,7 @@ class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
|
||||||
"""
|
"""
|
||||||
if queryset is None:
|
if queryset is None:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
dates = queryset.values_list(self.start_date_field, flat=True)
|
dates = queryset.values_list(queryset.start_date_field, flat=True)
|
||||||
dates = [(x.year, x.month) for x in dates]
|
dates = [(x.year, x.month) for x in dates]
|
||||||
date_counter = Counter(dates)
|
date_counter = Counter(dates)
|
||||||
dates = set(dates)
|
dates = set(dates)
|
||||||
|
|
|
@ -18,10 +18,10 @@ class BaseBlogView(ViewUrlMixin):
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
language = get_language()
|
language = get_language()
|
||||||
manager = self.model._default_manager.active_translations(language_code=language)
|
queryset = self.model._default_manager.active_translations(language_code=language)
|
||||||
if not self.request.user.is_staff:
|
if not self.request.user.is_staff:
|
||||||
manager = manager.filter(publish=True)
|
queryset = queryset.published()
|
||||||
return manager
|
return queryset
|
||||||
|
|
||||||
def render_to_response(self, context, **response_kwargs):
|
def render_to_response(self, context, **response_kwargs):
|
||||||
response_kwargs['current_app'] = resolve(self.request.path).namespace
|
response_kwargs['current_app'] = resolve(self.request.path).namespace
|
||||||
|
|
Loading…
Reference in a new issue