Permalink
Browse files

split tag queryset utils into smaller pieces

  • Loading branch information...
1 parent e5c3f9d commit e85e11a861269618a90a4c97fee1565170386c84 @ryanpitts ryanpitts committed Feb 18, 2013
Showing with 25 additions and 8 deletions.
  1. +25 −8 source/tags/utils.py
View
@@ -19,11 +19,27 @@ def filter_queryset_by_tags(queryset, tag_slugs, tags=[]):
with individual tags separated by a "+" character. The `tags` argument
should very likely be an empty list, which will end up holding a set
of model instances for the tags from `tag_slugs`.
+
+ This assumes the model for the queryset includes `tags`, `technology_tags`
+ and `concept_tags` fields.
+
+ TODO: remove support for original `tags` field
'''
_tag_slug_list = tag_slugs.split('+')
+ tags = get_validated_tag_list(_tag_slug_list, tags)
+ queryset = get_tag_filtered_queryset(queryset, _tag_slug_list)
+
+ # make sure we actually have matches for this intersection of tags
+ if not queryset:
+ raise Http404
+
+ return queryset, tags
+
+
+def get_validated_tag_list(tag_slug_list, tags):
_slugs_checked = []
- for slug in _tag_slug_list:
+ for slug in tag_slug_list:
for model in [Tag, TechnologyTag, ConceptTag]:
try:
# see if we have a matching tag
@@ -37,19 +53,20 @@ def filter_queryset_by_tags(queryset, tag_slugs, tags=[]):
pass
# make sure that we found everything we checked for
- if _slugs_checked != _tag_slug_list:
+ if _slugs_checked != tag_slug_list:
raise Http404
- for tag_slug in _tag_slug_list:
+ return tags
+
+
+def get_tag_filtered_queryset(queryset, tag_slug_list=[]):
+ for tag_slug in tag_slug_list:
# Look for matches in both types of tagfields
# TODO: Remove original `tags` query once content migrates
# to new split tagfields
queryset = queryset.filter(Q(tags__slug=tag_slug) | Q(technology_tags__slug=tag_slug) | Q(concept_tags__slug=tag_slug))
# A record might match multiple tags, but we only want it once
queryset = queryset.distinct()
+ print queryset.query
- # make sure we actually have matches for this intersection of tags
- if not queryset:
- raise Http404
-
- return queryset, tags
+ return queryset

0 comments on commit e85e11a

Please sign in to comment.