From 79e8a63d63d15645d05b01a6bf7f2bbec5a9a7a1 Mon Sep 17 00:00:00 2001 From: Luan Date: Thu, 17 Oct 2013 16:16:47 -0300 Subject: [PATCH] Adding ordering possibility to search --- src/colab/custom_settings.py | 18 ++++++++++++++++++ src/search/forms.py | 13 ++++++++++++- src/super_archives/templates/message-list.html | 16 +++++++++++----- src/super_archives/templatetags/append_to_get.py | 16 ---------------- src/super_archives/templatetags/urlutils.py | 24 ++++++++++++++++++++++++ src/super_archives/utils/url.py | 23 +++++++++++++++++++++++ src/super_archives/views.py | 3 ++- src/templates/search/search.html | 20 ++++++++++++++++++-- 8 files changed, 108 insertions(+), 25 deletions(-) delete mode 100644 src/super_archives/templatetags/append_to_get.py create mode 100644 src/super_archives/templatetags/urlutils.py diff --git a/src/colab/custom_settings.py b/src/colab/custom_settings.py index 03b3ed0..a4bfaac 100644 --- a/src/colab/custom_settings.py +++ b/src/colab/custom_settings.py @@ -15,6 +15,24 @@ LANGUAGES = ( LANGUAGE_CODE = 'pt-br' +# ORDERING_DATA receives the options to order for as it's keys and a dict as +# value, if you want to order for the last name, you can use something like: +# 'last_name': {'name': 'Last Name', 'fields': 'last_name'} inside the dict, +# you pass two major keys (name, fields) +# The major key name is the name to appear on the template +# the major key fields it show receive the name of the fields to order for in +# the indexes + +ORDERING_DATA = { + 'latest': { + 'name': gettext(u'Recent activity'), + 'fields': ('-modified', '-created'), + }, + 'hottest': { + 'name': gettext(u'Relevance'), + 'fields': None, + }, +} # the following variable define how many characters should be shown before # a highlighted word, to make sure that the highlighted word will appear diff --git a/src/search/forms.py b/src/search/forms.py index 2df482d..95b37df 100644 --- a/src/search/forms.py +++ b/src/search/forms.py @@ -3,6 +3,7 @@ import unicodedata from django import forms +from django.conf import settings from django.utils.translation import ugettext_lazy as _ from haystack.forms import SearchForm @@ -12,6 +13,7 @@ from super_archives.models import Message class ColabSearchForm(SearchForm): q = forms.CharField(label=_('Search')) + order = forms.CharField(widget=forms.HiddenInput(), required=False) type = forms.CharField(required=False, label=_(u'Type')) def search(self): @@ -27,7 +29,16 @@ class ColabSearchForm(SearchForm): sqs = self.searchqueryset.all() if self.cleaned_data['type']: - sqs = sqs.filter(type=self.cleaned_data['type']) + "It will consider other types with a whitespace" + types = self.cleaned_data['type'] + sqs = sqs.filter(type__in=types.split()) + + + if self.cleaned_data['order']: + for option, dict_order in settings.ORDERING_DATA.items(): + if self.cleaned_data['order'] == option: + if dict_order['fields']: + sqs = sqs.order_by(*dict_order['fields']) # if self.cleaned_data['type'] == 'user': # sqs = self.searchqueryset.models(User) # elif self.cleaned_data['type'] in ['message', 'thread']: diff --git a/src/super_archives/templates/message-list.html b/src/super_archives/templates/message-list.html index ead8566..c089e69 100644 --- a/src/super_archives/templates/message-list.html +++ b/src/super_archives/templates/message-list.html @@ -12,11 +12,17 @@

{% trans "Sort by" %}

{% trans "Lists" %}

diff --git a/src/super_archives/templatetags/append_to_get.py b/src/super_archives/templatetags/append_to_get.py deleted file mode 100644 index a88e234..0000000 --- a/src/super_archives/templatetags/append_to_get.py +++ /dev/null @@ -1,16 +0,0 @@ -# -*- coding: utf-8 -*- - -from django import template - -from super_archives.utils import url - -register = template.Library() - - -@register.simple_tag(takes_context=True) -def append_to_get(context, **kwargs): - return url.append_to_get( - context['request'].META['PATH_INFO'], - context['request'].META['QUERY_STRING'], - **kwargs - ) diff --git a/src/super_archives/templatetags/urlutils.py b/src/super_archives/templatetags/urlutils.py new file mode 100644 index 0000000..e049ae9 --- /dev/null +++ b/src/super_archives/templatetags/urlutils.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +from django import template + +from super_archives.utils import url + +register = template.Library() + + +@register.simple_tag(takes_context=True) +def append_to_get(context, **kwargs): + return url.append_to_get( + context['request'].META['PATH_INFO'], + context['request'].META['QUERY_STRING'], + **kwargs + ) + +@register.simple_tag(takes_context=True) +def pop_from_get(context, **kwargs): + return url.pop_from_get( + context['request'].META['PATH_INFO'], + context['request'].META['QUERY_STRING'], + **kwargs + ) diff --git a/src/super_archives/utils/url.py b/src/super_archives/utils/url.py index c1dbd89..0464b04 100644 --- a/src/super_archives/utils/url.py +++ b/src/super_archives/utils/url.py @@ -58,3 +58,26 @@ def append_to_get(path, query=None, **kwargs): if current_url[-1] == '&': return current_url[:-1] return current_url + + +def pop_from_get(path, query=None, **kwargs): + # Getting the path with the query + print query + + current_url = u'{}?{}'.format( + path, + query, + ) + for key, value in kwargs.items(): + popitem = u'{}={}'.format(key, value) + if query == popitem: + return path + + if key not in current_url: + return current_url + + first_path, end_path = current_url.split(key) + end_path_without_element = end_path.split(value, 1) + path_list = first_path + end_path_without_element + print path_list + return u''.join(path_list) diff --git a/src/super_archives/views.py b/src/super_archives/views.py index 0333685..21b0b53 100644 --- a/src/super_archives/views.py +++ b/src/super_archives/views.py @@ -3,6 +3,7 @@ import smtplib from django import http +from django.conf import settings from django.contrib import messages from django.db import IntegrityError from django.views.generic import View @@ -85,7 +86,7 @@ def list_messages(request): 'n_results': paginator.count, 'threads': threads, 'selected_list': selected_list, - 'order_by': order_by, + 'order_data': settings.ORDERING_DATA, } return render(request, 'message-list.html', template_data) diff --git a/src/templates/search/search.html b/src/templates/search/search.html index 4403158..645f77f 100644 --- a/src/templates/search/search.html +++ b/src/templates/search/search.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% load i18n %} -{% load append_to_get %} +{% load urlutils %} {% load highlight %} {% block main-content %} @@ -16,9 +16,25 @@
-
+