Commit 0e9f806e authored by fred's avatar fred

add categories to sound page

parent f204be4e
......@@ -2,6 +2,7 @@ import email.utils
import datetime
import re
import time
import urlparse
import urllib2
import uuid
......@@ -175,18 +176,27 @@ def remove_tag_facet(url, facet):
@register.filter
def remove_category_facet(url, facet):
facet = urllib2.quote(facet.encode('utf-8'), safe='')
return re.sub(r'&page=\d+', '', url.replace('&selected_facets=categories_exact:%s' % facet, ''))
scheme, netloc, path, query, fragment = list(urlparse.urlsplit(str(url)))
facet = 'categories_exact:%s' % facet.encode('utf-8')
query_string = urlparse.parse_qsl(query)
query_string = [x for x in query_string if not (
x[0] == 'selected_facets' and x[1] == facet)]
query = '&'.join(['%s=%s' % x for x in query_string])
url = urlparse.urlunsplit([scheme, netloc, path, query, None])
return re.sub(r'&page=\d+', '', url)
@register.filter
def append_tag_facet(url, facet):
facet = urllib2.quote(facet.encode('utf-8'), safe='')
if not '?' in url:
url = url + '?'
return re.sub(r'&page=\d+', '', url + '&selected_facets=tags_exact:%s' % facet)
@register.filter
def append_category_facet(url, facet):
facet = urllib2.quote(facet.encode('utf-8'), safe='')
if not '?' in url:
url = url + '?'
return re.sub(r'&page=\d+', '', url + '&selected_facets=categories_exact:%s' % facet)
......
......@@ -150,7 +150,7 @@ view = search_view_factory(SearchView,
class ListenArchivesForm(SearchForm):
class ListenArchivesForm(FacetedSearchForm):
def no_query_found(self):
return self.searchqueryset.all()
......@@ -159,17 +159,21 @@ class ListenArchivesForm(SearchForm):
return sqs.load_all()
class ListenArchivesView(haystack.views.SearchView):
class ListenArchivesView(FacetedSearchView):
template = 'listen/archives.html'
def __init__(self):
sqs = RelatedSearchQuerySet().filter(django_ct='emissions.soundfile').order_by('-date')
sqs = RelatedSearchQuerySet().models(SoundFile).facet('categories').order_by('-date')
super(ListenArchivesView, self).__init__(searchqueryset=sqs,
form_class=ListenArchivesForm, results_per_page=20)
def extra_context(self):
context = super(ListenArchivesView, self).extra_context()
context['sectionName'] = "Listen"
context['selected_categories'] = [
x.split(':', 1)[1] for x in self.request.GET.getlist('selected_facets')
if x.startswith('categories_exact')]
context['facets']['fields']['categories'].sort()
return context
listenArchives = search_view_factory(ListenArchivesView)
......@@ -19,8 +19,7 @@ urlpatterns = patterns('',
url(r'^emissions/(?P<slug>[\w,-]+)/$', 'panikweb.views.emission', name='emission-view'),
url(r'^ckeditor/', include('ckeditor.urls')),
url(r'^emissions/archives$', 'panikweb.views.emissionsArchives', name='emissionsArchives'),
url(r'^listen/$', 'panikweb.views.listen', name='listen'),
url(r'^listen/archives/$', 'panikweb.search.listenArchives', name='listenArchives'),
url(r'^listen/$', 'panikweb.search.listenArchives', name='listen'),
url(r'^news/$', 'panikweb.views.news', name='news'),
url(r'^news/archives/$', 'panikweb.views.newsArchives', name='newsArchives'),
url(r'^news/(?P<slug>[\w,-]+)$', 'panikweb.views.newsitemview', name='news-view'),
......
......@@ -2,12 +2,31 @@
{% load paniktags %}
{% block title %}Listen - Archives{% endblock %}
{% block nav %}
{% listen_nav with klass="archives" %}
<nav>
{% if facets.fields.categories %}
<ul class="custom padded">
{% for category in facets.fields.categories %} <li>
{% if category.0 in selected_categories %}
<a class="check icon-check count-{{ category.1 }}" href="{{ request.get_full_path|remove_category_facet:category.0 }}">
{{ category.0 }} | {{ category.1 }}
</a>
{% else %}
<a class="check icon-check-empty count-{{ category.1 }}" href="{{ request.get_full_path|append_category_facet:category.0 }}">{{ category.0 }} | {{ category.1 }}</a></span>
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
</nav>
{% endblock %}
{% block main %}
<form method="get" action="." class="padded center" id="search-form">
<div class="big">
{% for category in selected_categories %}
<input type="hidden" name="selected_facets" value="categories_exact:{{category}}"/>
{% endfor %}
{{ form.as_table }}<button class="icon-search"></button>
</div>
</form>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment