Skip to content
This repository has been archived by the owner on Jun 17, 2018. It is now read-only.

Changed the TagManager.usage_for_queryset method. #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion tagging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ def usage_for_model(self, model, counts=False, min_count=None, filters=None):

return usage

def usage_for_queryset(self, queryset, counts=False, min_count=None):
def usage_for_queryset(self, queryset, counts=False, min_count=None, tag_queryset=None):
"""
Obtain a list of tags associated with instances of a model
contained in the given queryset.

The parameter tag_queryset allowes to filter the result tags list.

If ``counts`` is True, a ``count`` attribute will be added to
each tag, indicating how many times it has been used against
the Model class in question.
Expand All @@ -178,6 +180,26 @@ def usage_for_queryset(self, queryset, counts=False, min_count=None):
extra_criteria = 'AND %s' % where
else:
extra_criteria = ''

if tag_queryset is not None:
if getattr(tag_queryset.query, 'get_compiler', None):
# Django 1.2+
compiler = tag_queryset.query.get_compiler(using='default')
extra_joins += ' ' + ' '.join(compiler.get_from_clause()[0][1:])
tag_where, tag_params = tag_queryset.query.where.as_sql(
compiler.quote_name_unless_alias, compiler.connection
)
else:
# Django pre-1.2
extra_joins += ' ' + ' '.join(tag_queryset.query.get_from_clause()[0][1:])
tag_where, tag_params = tag_queryset.query.where.as_sql()

for p in tag_params:
params.append(p)

if tag_where:
extra_criteria += ' AND %s' % tag_where

return self._get_usage(queryset.model, counts, min_count, extra_joins, extra_criteria, params)

def related_for_model(self, tags, model, counts=False, min_count=None):
Expand Down