Commit 79e8a63d63d15645d05b01a6bf7f2bbec5a9a7a1

Authored by Luan
1 parent 5350572c

Adding ordering possibility to search

src/colab/custom_settings.py
... ... @@ -15,6 +15,24 @@ LANGUAGES = (
15 15  
16 16 LANGUAGE_CODE = 'pt-br'
17 17  
  18 +# ORDERING_DATA receives the options to order for as it's keys and a dict as
  19 +# value, if you want to order for the last name, you can use something like:
  20 +# 'last_name': {'name': 'Last Name', 'fields': 'last_name'} inside the dict,
  21 +# you pass two major keys (name, fields)
  22 +# The major key name is the name to appear on the template
  23 +# the major key fields it show receive the name of the fields to order for in
  24 +# the indexes
  25 +
  26 +ORDERING_DATA = {
  27 + 'latest': {
  28 + 'name': gettext(u'Recent activity'),
  29 + 'fields': ('-modified', '-created'),
  30 + },
  31 + 'hottest': {
  32 + 'name': gettext(u'Relevance'),
  33 + 'fields': None,
  34 + },
  35 +}
18 36  
19 37 # the following variable define how many characters should be shown before
20 38 # a highlighted word, to make sure that the highlighted word will appear
... ...
src/search/forms.py
... ... @@ -3,6 +3,7 @@
3 3 import unicodedata
4 4  
5 5 from django import forms
  6 +from django.conf import settings
6 7 from django.utils.translation import ugettext_lazy as _
7 8 from haystack.forms import SearchForm
8 9  
... ... @@ -12,6 +13,7 @@ from super_archives.models import Message
12 13  
13 14 class ColabSearchForm(SearchForm):
14 15 q = forms.CharField(label=_('Search'))
  16 + order = forms.CharField(widget=forms.HiddenInput(), required=False)
15 17 type = forms.CharField(required=False, label=_(u'Type'))
16 18  
17 19 def search(self):
... ... @@ -27,7 +29,16 @@ class ColabSearchForm(SearchForm):
27 29 sqs = self.searchqueryset.all()
28 30  
29 31 if self.cleaned_data['type']:
30   - sqs = sqs.filter(type=self.cleaned_data['type'])
  32 + "It will consider other types with a whitespace"
  33 + types = self.cleaned_data['type']
  34 + sqs = sqs.filter(type__in=types.split())
  35 +
  36 +
  37 + if self.cleaned_data['order']:
  38 + for option, dict_order in settings.ORDERING_DATA.items():
  39 + if self.cleaned_data['order'] == option:
  40 + if dict_order['fields']:
  41 + sqs = sqs.order_by(*dict_order['fields'])
31 42 # if self.cleaned_data['type'] == 'user':
32 43 # sqs = self.searchqueryset.models(User)
33 44 # elif self.cleaned_data['type'] in ['message', 'thread']:
... ...
src/super_archives/templates/message-list.html
... ... @@ -12,11 +12,17 @@
12 12  
13 13 <h4>{% trans "Sort by" %}</h4>
14 14 <ul class="unstyled-list">
15   - <li {% ifequal order_by "hottest" %} title="{% trans "Remove filter" %}" {% endifequal %}>
16   - <span class="glyphicon glyphicon-chevron-right"></span> <a href="{% ifequal order_by "hottest" %} {% append_to_get order="" p=1 %} {% else %} {% append_to_get order='hottest' p=1 %} {% endifequal %}">{% trans "Relevance" %}</a></li>
17   - <li {% ifequal order_by "latest" %} title="{% trans "Remove filter" %}" {% endifequal %}>
18   - <span class="glyphicon glyphicon-chevron-right"></span> <a href="{% ifequal order_by "latest" %} {% append_to_get order="" p=1 %} {% else %} {% append_to_get order='latest' p=1 %} {% endifequal %}">
19   - {% trans "Recent activity" %}</a></li>
  15 + {% for option, dict_order in order_data.items %}
  16 + <li>
  17 + <span class="glyphicon glyphicon-chevron-right"></span>
  18 + <a href="{% append_to_get order=option p=1 %}">
  19 + {% ifequal request.GET.order option %}
  20 + {% blocktrans with name=dict_order.name %}<strong>{{ name }}</strong>{% endblocktrans %}</a>
  21 + {% else %}
  22 + {% blocktrans with name=dict_order.name %}{{ name }}{% endblocktrans %}</a>
  23 + {% endifequal %}
  24 + </li>
  25 + {% endfor %}
20 26 </ul>
21 27  
22 28 <h4>{% trans "Lists" %}</h4>
... ...
src/super_archives/templatetags/append_to_get.py
... ... @@ -1,16 +0,0 @@
1   -# -*- coding: utf-8 -*-
2   -
3   -from django import template
4   -
5   -from super_archives.utils import url
6   -
7   -register = template.Library()
8   -
9   -
10   -@register.simple_tag(takes_context=True)
11   -def append_to_get(context, **kwargs):
12   - return url.append_to_get(
13   - context['request'].META['PATH_INFO'],
14   - context['request'].META['QUERY_STRING'],
15   - **kwargs
16   - )
src/super_archives/templatetags/urlutils.py 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +# -*- coding: utf-8 -*-
  2 +
  3 +from django import template
  4 +
  5 +from super_archives.utils import url
  6 +
  7 +register = template.Library()
  8 +
  9 +
  10 +@register.simple_tag(takes_context=True)
  11 +def append_to_get(context, **kwargs):
  12 + return url.append_to_get(
  13 + context['request'].META['PATH_INFO'],
  14 + context['request'].META['QUERY_STRING'],
  15 + **kwargs
  16 + )
  17 +
  18 +@register.simple_tag(takes_context=True)
  19 +def pop_from_get(context, **kwargs):
  20 + return url.pop_from_get(
  21 + context['request'].META['PATH_INFO'],
  22 + context['request'].META['QUERY_STRING'],
  23 + **kwargs
  24 + )
... ...
src/super_archives/utils/url.py
... ... @@ -58,3 +58,26 @@ def append_to_get(path, query=None, **kwargs):
58 58 if current_url[-1] == '&':
59 59 return current_url[:-1]
60 60 return current_url
  61 +
  62 +
  63 +def pop_from_get(path, query=None, **kwargs):
  64 + # Getting the path with the query
  65 + print query
  66 +
  67 + current_url = u'{}?{}'.format(
  68 + path,
  69 + query,
  70 + )
  71 + for key, value in kwargs.items():
  72 + popitem = u'{}={}'.format(key, value)
  73 + if query == popitem:
  74 + return path
  75 +
  76 + if key not in current_url:
  77 + return current_url
  78 +
  79 + first_path, end_path = current_url.split(key)
  80 + end_path_without_element = end_path.split(value, 1)
  81 + path_list = first_path + end_path_without_element
  82 + print path_list
  83 + return u''.join(path_list)
... ...
src/super_archives/views.py
... ... @@ -3,6 +3,7 @@
3 3 import smtplib
4 4  
5 5 from django import http
  6 +from django.conf import settings
6 7 from django.contrib import messages
7 8 from django.db import IntegrityError
8 9 from django.views.generic import View
... ... @@ -85,7 +86,7 @@ def list_messages(request):
85 86 'n_results': paginator.count,
86 87 'threads': threads,
87 88 'selected_list': selected_list,
88   - 'order_by': order_by,
  89 + 'order_data': settings.ORDERING_DATA,
89 90 }
90 91 return render(request, 'message-list.html', template_data)
91 92  
... ...
src/templates/search/search.html
1 1 {% extends "base.html" %}
2 2 {% load i18n %}
3   -{% load append_to_get %}
  3 +{% load urlutils %}
4 4 {% load highlight %}
5 5  
6 6 {% block main-content %}
... ... @@ -16,9 +16,25 @@
16 16 <hr/>
17 17  
18 18 <div class="row">
19   - <div class="col-lg-2">
  19 + <div id="filters" class="hidden-xs hidden-sm col-md-2 col-lg-2">
20 20 <h3>{% trans "Filters" %}</h3>
21 21  
  22 + <h4>{% trans "Sort by" %}</h4>
  23 + <ul class="unstyled-list">
  24 + {% for option, dict_order in order_data.items %}
  25 + <li>
  26 + <span class="glyphicon glyphicon-chevron-right"></span>
  27 + <a href="{% append_to_get order=option p=1 %}">
  28 + {% ifequal request.GET.order option %}
  29 + {% blocktrans with name=dict_order.name %}<strong>{{ name }}</strong>{% endblocktrans %}
  30 + {% else %}
  31 + {% blocktrans with name=dict_order.name %}{{ name }}{% endblocktrans %}
  32 + {% endifequal %}
  33 + </a>
  34 + </li>
  35 + {% endfor %}
  36 + </ul>
  37 +
22 38 <h4>{% trans "Types" %}</h4>
23 39  
24 40 <ul class="unstyled-list">
... ...