fix queryset check

This commit is contained in:
Iacopo Spalletti 2014-02-02 16:22:22 +01:00
parent 26a606e121
commit c152d19185

View file

@ -50,8 +50,8 @@ class TaggedFilterItem(object):
return Tag.objects.filter(id__in=self._taglist(other_model, queryset)) return Tag.objects.filter(id__in=self._taglist(other_model, queryset))
def tag_list_slug(self, other_model=None, queryset=None): def tag_list_slug(self, other_model=None, queryset=None):
qs = self.tag_list(other_model, queryset) queryset = self.tag_list(other_model, queryset)
return qs.values("slug") return queryset.values("slug")
def tag_cloud(self, other_model=None, queryset=None, published=True): def tag_cloud(self, other_model=None, queryset=None, published=True):
from taggit.models import TaggedItem from taggit.models import TaggedItem
@ -77,49 +77,48 @@ class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
end_date_field = "date_published_end" end_date_field = "date_published_end"
publish_field = "publish" publish_field = "publish"
def published(self, qs=None): def published(self, queryset=None):
qs = self.published_future(qs) queryset = self.published_future(queryset)
if self.start_date_field: if self.start_date_field:
return qs.filter( return queryset.filter(
**{"%s__lte" % self.start_date_field: datetime.datetime.now()}) **{"%s__lte" % self.start_date_field: datetime.datetime.now()})
else: else:
return qs return queryset
def published_future(self, qs=None): def published_future(self, queryset=None):
if not qs: if queryset is None:
qs = self.get_query_set().all() queryset = self.get_query_set().all()
if self.end_date_field: if self.end_date_field:
qfilter = ( qfilter = (
models.Q(**{"%s__gte" % self.end_date_field: datetime.datetime.now()}) models.Q(**{"%s__gte" % self.end_date_field: datetime.datetime.now()})
| models.Q(**{"%s__isnull" % self.end_date_field: True}) | models.Q(**{"%s__isnull" % self.end_date_field: True})
) )
qs = qs.filter(qfilter) queryset = queryset.filter(qfilter)
return qs.filter(**{self.publish_field: True}) return queryset.filter(**{self.publish_field: True})
def archived(self, qs=None): def archived(self, queryset=None):
if not qs: if queryset is None:
qs = self.get_query_set().all() queryset = self.get_query_set().all()
if self.end_date_field: if self.end_date_field:
qfilter = ( qfilter = (
models.Q(**{"%s__lte" % self.end_date_field: datetime.datetime.now()}) models.Q(**{"%s__lte" % self.end_date_field: datetime.datetime.now()})
| models.Q(**{"%s__isnull" % self.end_date_field: False}) | models.Q(**{"%s__isnull" % self.end_date_field: False})
) )
qs = qs.filter(qfilter) queryset = queryset.filter(qfilter)
return qs.filter(**{self.publish_field: True}) return queryset.filter(**{self.publish_field: True})
def available(self, qs=None): def available(self, queryset=None):
if not qs: if queryset is None:
qs = self.get_query_set().all() queryset = self.get_query_set().all()
return qs.filter(**{self.publish_field: True}) return queryset.filter(**{self.publish_field: True})
def filter_by_language(self, language): def filter_by_language(self, language):
qs = self.get_query_set() queryset = self.get_query_set()
return qs.filter(models.Q(language__isnull=True) | models.Q(language=language)) return queryset.filter(models.Q(language__isnull=True) | models.Q(language=language))
def get_months(self, queryset=None): def get_months(self, queryset=None):
"""Get months with aggregatet count (how much posts is in the month). Results are ordered by date.""" """Get months with aggregatet count (how much posts is in the month). Results are ordered by date."""
# done via naive way as django's having tough time while aggregating on date fields if queryset is None:
if not queryset:
queryset = self.get_query_set() queryset = self.get_query_set()
dates = queryset.values_list(self.start_date_field, flat=True) dates = queryset.values_list(self.start_date_field, flat=True)
dates = [(x.year, x.month) for x in dates] dates = [(x.year, x.month) for x in dates]