Skip to content

Commit

Permalink
allow for tag intersection queries on code list page
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanpitts committed Oct 28, 2012
1 parent 56d5b3f commit 2242fe8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
6 changes: 4 additions & 2 deletions source/code/templates/code/code_list.html
@@ -1,12 +1,14 @@
{% extends "code/_base_code.html" %}

{% block page_title %}{% if tags %}Entries tagged: {% for tag in tags %}{{ tag.name }}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %} - {{ super() }}{% endblock %}

{% block content %}
<h1 class="maintopic"><a href="{{ url('code_list') }}">Code</a>{% if tag %} / <span class="category">{{ tag.name }}</span>{% endif %}</h1>

<div id="filterable-list">
{% if tag %}
{% if tags %}
<div class="grouper-block filter-block">
<h2 class="grouper-header"><span class="category">Code index entries tagged: {{ tag.name }}</span></h2>
<h2 class="grouper-header"><span class="category">Code index entries tagged: {% for tag in tags %}{{ tag.name }}{% if not loop.last %}, {% endif %}{% endfor %}</span></h2>
{% for code in object_list %}
<div class="grid-box">
<h3><a href="{{ code.get_absolute_url() }}">{{ code.name }}</a></h3>
Expand Down
6 changes: 3 additions & 3 deletions source/code/urls.py
Expand Up @@ -26,19 +26,19 @@
name = 'code_list_feed_json',
),
url(
regex = '^tags/(?P<tag_slug>[-\w]+)/$',
regex = '^tags/(?P<tag_slugs>[-\w\+]+)/$',
view = CodeList.as_view(),
kwargs = {},
name = 'code_list_by_tag',
),
url(
regex = '^tags/(?P<tag_slug>[-\w]+)/rss/$',
regex = '^tags/(?P<tag_slugs>[-\w\+]+)/rss/$',
view = cache_page(CodeFeed(), 60*15),
kwargs = {},
name = 'code_list_by_tag_feed',
),
url(
regex = '^tags/(?P<tag_slug>[-\w]+)/json/$',
regex = '^tags/(?P<tag_slugs>[-\w\+]+)/json/$',
view = cache_page(CodeList.as_view(), 60*15),
kwargs = {'render_json': True},
name = 'code_list_by_tag_feed_json',
Expand Down
24 changes: 14 additions & 10 deletions source/code/views.py
Expand Up @@ -13,28 +13,32 @@ class CodeList(ListView):

def dispatch(self, *args, **kwargs):
self.render_json = kwargs.get('render_json', False)
self.tag_slug = kwargs.get('tag_slug', None)
self.tag = None

self.tags = None
self.tag_slugs = kwargs.get('tag_slugs', None)
self.tag_slug_list = []
return super(CodeList, self).dispatch(*args, **kwargs)

def get_queryset(self):
queryset = Code.live_objects.prefetch_related('organizations')

if self.tag_slug:
self.tag = get_object_or_404(Tag, slug=self.tag_slug)
queryset = queryset.filter(tags__slug=self.kwargs['tag_slug'])
if self.tag_slugs:
self.tag_slug_list = self.tag_slugs.split('+')
# need to fail if any item in slug list references nonexistent tag
self.tags = [get_object_or_404(Tag, slug=tag_slug) for tag_slug in self.tag_slug_list]
for tag_slug in self.tag_slug_list:
queryset = queryset.filter(tags__slug=tag_slug)

return queryset

def get_context_data(self, **kwargs):
context = super(CodeList, self).get_context_data(**kwargs)
context['active_nav'] = 'Code'

if self.tag:
context['tag'] = self.tag
context['rss_link'] = reverse('code_list_by_tag_feed', kwargs={'tag_slug': self.tag_slug})
context['json_link'] = reverse('code_list_by_tag_feed_json', kwargs={'tag_slug': self.tag_slug})
if self.tags:
pass
context['tags'] = self.tags
context['rss_link'] = reverse('code_list_by_tag_feed', kwargs={'tag_slugs': self.tag_slugs})
context['json_link'] = reverse('code_list_by_tag_feed_json', kwargs={'tag_slugs': self.tag_slugs})
else:
context['rss_link'] = reverse('code_list_feed')
context['json_link'] = reverse('code_list_feed_json')
Expand Down

0 comments on commit 2242fe8

Please sign in to comment.