Commit 27b9f1cc authored by fred's avatar fred

news: add search forms

parent 1262807e
......@@ -235,6 +235,19 @@ def remove_category_facet(url, facet):
url = urlparse.urlunsplit([scheme, netloc, path, query, None])
return re.sub(r'&page=\d+', '', url)
@register.filter
def remove_news_category_facet(url, facet):
scheme, netloc, path, query, fragment = list(urlparse.urlsplit(str(url)))
facet = 'news_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='')
......@@ -249,6 +262,12 @@ def append_category_facet(url, facet):
url = url + '?'
return re.sub(r'&page=\d+', '', url + '&selected_facets=categories_exact:%s' % facet)
@register.filter
def append_news_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=news_categories_exact:%s' % facet)
@register.tag
def search_result_template(parser, token):
......
......@@ -6,7 +6,9 @@ from haystack.backends import EmptyResults
from pysolr import SolrError
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from emissions.models import Emission, Episode, NewsItem, SoundFile
......@@ -151,6 +153,8 @@ view = search_view_factory(SearchView,
class ListenArchivesForm(FacetedSearchForm):
q = forms.CharField(required=False, label=_('Search Sounds'))
def no_query_found(self):
return self.searchqueryset.all()
......@@ -180,3 +184,36 @@ class ListenArchivesView(FacetedSearchView):
return context
listenArchives = search_view_factory(ListenArchivesView)
class NewsArchivesForm(FacetedSearchForm):
q = forms.CharField(required=False, label=_('Search News'))
def no_query_found(self):
return self.searchqueryset.all()
def search(self):
sqs = super(NewsArchivesForm, self).search()
return sqs.load_all()
class NewsArchivesView(FacetedSearchView):
template = 'news/archives.html'
def __init__(self):
sqs = RelatedSearchQuerySet().models(NewsItem).facet('news_categories').facet('tags').order_by('-date')
super(NewsArchivesView, self).__init__(searchqueryset=sqs,
form_class=NewsArchivesForm, results_per_page=20)
def extra_context(self):
context = super(NewsArchivesView, self).extra_context()
context['sectionName'] = 'News'
context['selected_news_categories'] = [
x.split(':', 1)[1] for x in self.request.GET.getlist('selected_facets')
if x.startswith('news_categories_exact')]
context['selected_tags'] = [
x.split(':', 1)[1] for x in self.request.GET.getlist('selected_facets')
if x.startswith('tags_exact')]
return context
newsArchives = search_view_factory(NewsArchivesView)
......@@ -21,7 +21,7 @@ urlpatterns = patterns('',
url(r'^emissions/archives$', 'panikweb.views.emissionsArchives', name='emissionsArchives'),
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/archives/$', 'panikweb.search.newsArchives', name='newsArchives'),
url(r'^news/(?P<slug>[\w,-]+)$', 'panikweb.views.newsitemview', name='newsitem-view'),
url(r'^search/', 'panikweb.search.view', name='search'),
......
......@@ -449,26 +449,6 @@ class News(TemplateView):
news = News.as_view()
class NewsArchives(TemplateView):
template_name = 'news/archives.html'
def get_context_data(self, **kwargs):
context = super(NewsArchives, self).get_context_data(**kwargs)
context['sectionName'] = "News"
context['search_query'] = self.request.GET.get('q')
sqs = SearchQuerySet().models(NewsItem)
if context['search_query']:
# query string
sqs = sqs.filter(text=context['search_query'])
else:
sqs = sqs.order_by('-date')
sqs = sqs.load_all()
context['results'] = sqs
return context
newsArchives = NewsArchives.as_view()
class Emissions(TemplateView):
template_name = 'emissions.html'
......
......@@ -37,6 +37,14 @@
<h5 class="sectionLabel">
<span class="iconLabel">Dernières actus</span>
</h5>
<form action="{% url 'newsArchives' %}" id="search-form">
<label for="id_q">Rechercher dans les actus :</label>
<input id="id_q" name="q" type="text" {% if search_query %}value="{{ search_query }}"{% endif %}>
<button class="icon-search"></button>
</form>
<ul class="custom list newsList">
{% for content in news %}
<li class="{% if content == newsitem %}current{% endif %}">{% news_inline %}</li>
......
......@@ -44,6 +44,14 @@ principes torquatos, vix ne essent partiendo.
<span class="iconLabel">Dernières actus</span>
<a class="button icon-rss inBlock" title="{% trans 'Rss news' %}" href="{% url 'rss-feed' %}"></a>
</h5>
<form action="{% url 'newsArchives' %}" id="search-form">
<label for="id_q">Rechercher dans les actus :</label>
<input id="id_q" name="q" type="text" {% if search_query %}value="{{ search_query }}"{% endif %}>
<button class="icon-search"></button>
</form>
<ul class="custom list newsList">
{% for content in news %}
<li class="{% if content == newsitem %}current{% endif %}">{% news_inline %}</li>
......
......@@ -4,36 +4,83 @@
{% block title %}News - Archives{% endblock %}
{% block nav %}
{% news_nav with klass="archives" %}
{% if facets.fields.news_categories %}
<nav>
<ul class="custom padded">
<li><span class="mainLabel">Catégorie :</span></li>
{% for category in facets.fields.news_categories %} <li>
{% if category.0 in selected_news_categories %}
<a class="check icon-check count-{{ category.1 }}" href="{{ request.get_full_path|remove_news_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_news_category_facet:category.0 }}">{{ category.0 }} | {{ category.1 }}</a></span>
{% endif %}
</li>
{% endfor %}
</ul>
</nav>
{% endif %}
{% if facets.fields.tags %}
<nav>
<ul class="tag-facets custom padded">
<li><span class="mainLabel">Mots clés :</span></li>
{% for tag in facets.fields.tags|slice:":20" %} {# Provide only the top 20 tags #}
<li>
{% if tag.0 in selected_tags %}
<a class="check icon-check count-{{ tag.1 }}" href="{{ request.get_full_path|remove_tag_facet:tag.0 }}">
{{ tag.0 }} <span class="smooth">| {{ tag.1 }}</span>
</a>
{% else %}
<a class="check icon-check-empty count-{{ tag.1 }}" href="{{ request.get_full_path|append_tag_facet:tag.0 }}">{{ tag.0 }} <span class="smooth">| {{ tag.1 }}</span></a>
{% endif %}
</li>
{% endfor %}
</ul>
</nav>
{% endif %}
{% endblock %}
{% block main %}
<div class="wrapper {% if results %}sided{% endif %}">
<form method="get" action="." class="padded center" id="search-form">
<div class="big">
{% for category in selected_news_categories %}
<input type="hidden" name="selected_facets" value="news_categories_exact:{{category}}"/>
{% endfor %}
{% for tag in selected_tags %}
<input type="hidden" name="selected_facets" value="tags_exact:{{tag}}"/>
{% endfor %}
{{ form.as_table }}<button class="icon-search"></button>
</div>
</form>
{% if search_query and not results %}
<div class="wrapper">
{% if not page.object_list %}
<div class="big error padded center">Sorry, no result with your query!</div>
{% endif %}
{% endif %}
{% if search_query and results or results.count <= 24 %}
<ul class="custom columns list news cf">
{% for result in results %}
<li class="item">
{% with result.object as content %}{% news_inline %}{% endwith %}
</li>
{% endfor %}
</ul>
{% else %}
{% if page.has_previous %}
<div class="previous-page cf">
<a class="button big left" href="?q={{ query }}&amp;page={{ page.previous_page_number }}">&laquo; Résultats précédénts</a>
</div>
{% endif %}
<div id="Emission-container" class="emission padded">
{% if results %}
{% regroup results by date|date:"F Y"|capfirst as month_list %}
{% if page.object_list %}
{% regroup page.object_list by date|date:"F Y"|capfirst as month_list %}
<div>
{% for month in month_list %}
<div class="monthGroup {% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %}">
<div class="legend button" onclick="$('#{{ month.grouper|slugify }}').toggle();"
>{{ month.grouper }}</div>
<ul class="custom list" id="{{ month.grouper|slugify }}"
{% if forloop.counter >= 3 %}style="display:none;"{% endif %}
>
{% for result in month.list %}
<li>{% with result.object as content %}{% news_inline %}{% endwith %}</li>
......@@ -45,7 +92,12 @@
{% endif %}
</div>
{% endif %}
{% if page.has_next %}
<div class="next-page cf">
<a class="button big right" href="?q={{ query }}&amp;page={{ page.next_page_number }}">Résultats suivants &raquo;</a>
</div>
{% endif %}
</div>
......
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