@@ -0,0 +1,2192 @@
+from django.db.models import Q%0Afrom django.http import Http404%0A%0Afrom .models import TechnologyTag, ConceptTag%0Afrom taggit.models import Tag%0A%0Adef filter_queryset_by_tags(queryset, tag_slugs, tags=%5B%5D):%0A    '''%0A    This takes a queryset and a set of tag slugs, and:%0A    - does the proper checks to make sure that each tag actually exists%0A    - filters the provided queryset based on those tags%0A    - returns that queryset along with a list of matched tag model instances%0A    for use in page context%0A    %0A    Because we need to match against multiple tag models, we have to do%0A    some loops I wish we didn't have to do. This is why we cache.%0A    %0A    The %60tag_slugs%60 argument should be a string captured by a url param,%0A    with individual tags separated by a %22+%22 character. The %60tags%60 argument%0A    should very likely be an empty list, which will end up holding a set%0A    of model instances for the tags from %60tag_slugs%60.%0A    '''%0A%0A    _tag_slug_list = tag_slugs.split('+')%0A    _slugs_checked = %5B%5D%0A    for slug in _tag_slug_list:%0A        for model in %5BTag, TechnologyTag, ConceptTag%5D:%0A            try:%0A                # see if we have a matching tag%0A                found_tag = model.objects.get(slug=slug)%0A                # add it to list for page context%0A                tags.append(found_tag)%0A                # remember that we've checked it%0A                _slugs_checked.append(slug)%0A                break%0A            except:%0A                pass%0A%0A    # make sure that we found everything we checked for%0A    if _slugs_checked != _tag_slug_list:%0A        print _slugs_checked, _tag_slug_list%0A        raise Http404%0A    %0A    for tag_slug in _tag_slug_list:%0A        # Look for matches in both types of tagfields%0A        # TODO: Remove original %60tags%60 query once content migrates%0A        # to new split tagfields%0A        queryset = queryset.filter(Q(tags__slug=tag_slug) %7C Q(technology_tags__slug=tag_slug) %7C Q(concept_tags__slug=tag_slug))%0A        # A record might match multiple tags, but we only want it once%0A        queryset = queryset.distinct()%0A%0A    # make sure we actually have matches for this intersection of tags%0A    if not queryset:%0A        raise Http404%0A    %0A    return queryset, tags%0A

add filter_queryset_by_tags util