Commit 344b4c59 authored by Michal Čihař's avatar Michal Čihař

Merge branch 'search'

Closes #317
parents 4f86708d 06d1ef74
......@@ -26,6 +26,7 @@ from django.utils.safestring import mark_safe
from django.utils.encoding import smart_unicode
from django.forms import ValidationError
from lang.models import Language
from urllib import urlencode
def escape_newline(value):
......@@ -269,6 +270,23 @@ class SearchForm(forms.Form):
return cleaned_data
def urlencode(self):
'''
Encodes query string to be used in URL.
'''
query = {
'q': self.cleaned_data['q'],
'search': self.cleaned_data['search'],
}
if self.cleaned_data['src']:
query['src'] = 'on'
if self.cleaned_data['tgt']:
query['tgt'] = 'on'
if self.cleaned_data['ctx']:
query['ctx'] = 'on'
return urlencode(query)
class MergeForm(forms.Form):
'''
......
......@@ -31,7 +31,7 @@ from django.utils.safestring import mark_safe
from trans.models import (
Project, SubProject, Translation, Check,
Dictionary, Change,
Dictionary, Change, Unit
)
from trans.requirements import get_versions, get_optional_versions
from lang.models import Language
......@@ -108,9 +108,60 @@ def home(request):
'last_changes_rss': reverse('rss'),
'last_changes_url': '',
'usertranslations': usertranslations,
'search_form': SearchForm(),
}))
def search(request):
'''
Performs sitewide search on units.
'''
search_form = SearchForm(request.GET)
context = {
'search_form': search_form,
}
if search_form.is_valid():
units = Unit.objects.search(
search_form.cleaned_data['search'],
search_form.cleaned_data['q'],
search_form.cleaned_data['src'],
search_form.cleaned_data['ctx'],
search_form.cleaned_data['tgt'],
).select_related(
'translation',
)
limit = request.GET.get('limit', 50)
page = request.GET.get('page', 1)
ignored = 'ignored' in request.GET
paginator = Paginator(units, limit)
try:
units = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
units = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
units = paginator.page(paginator.num_pages)
context['units'] = units
context['title'] = _('Search for %s') % (
search_form.cleaned_data['q']
)
context['query_string'] = search_form.urlencode()
context['search_query'] = search_form.cleaned_data['q']
else:
messages.error(request, _('Invalid search query!'))
return render_to_response(
'search.html',
RequestContext(request, context)
)
def show_engage(request, project, lang=None):
# Get project object
obj = get_project(request, project)
......
......@@ -63,6 +63,7 @@
<ul>
<li><a href="#changes">{% trans "History" %}</a></li>
<li><a href="#search">{% trans "Search" %}</a></li>
<li><a href="#translators">{% trans "Most active translators" %}</a></li>
<li><a href="#suggesters">{% trans "Most active suggesters" %}</a></li>
<li><a href="{% url 'view_activity' %}">{% trans "Activity" %}</a></li>
......@@ -73,6 +74,13 @@
{% include "last-changes.html" %}
</div>
<div id="search">
<form action="{% url 'search' %}" method="GET">
{{ search_form.as_p }}
<input type="submit" value="{% trans "Search" %}" class="button" />
</form>
</div>
<div id="translators">
<table>
<thead>
......
{% extends "base.html" %}
{% load i18n %}
{% load url from future %}
{% load translations %}
{% block breadcrumbs %}
<li><a href="{% url 'search' %}">{% trans "search" %}</a></li>
{% endblock %}
{% block content %}
<span class="navi-toolbar ui-widget-header ui-corner-all">
<span id="navi">
<a id="button-first" {% if units.number == 1 %}class="button-disabled"{% endif %} href="?page=1&amp;{{ query_string }}">{% trans "First" %}</a>
<a id="button-prev" {% if not units.has_previous %}class="button-disabled"{% else %}href="?page={{ units.previous_page_number }}&amp;{{ query_string }}"{% endif %}>{% trans "Previous" %}</a>
<a id="button-pos" class="textbutton">{% blocktrans with units.number as position and units.paginator.num_pages as total %}{{ position }} / {{ total }}{% endblocktrans %}</a>
<a id="button-next" {% if not units.has_next %}class="button-disabled"{% else %}href="?page={{ units.next_page_number }}&amp;{{ query_string }}"{% endif %}>{% trans "Next" %}</a>
<a id="button-end" {% if units.paginator.num_pages == units.number %}class="button-disabled"{% endif %} href="?page={{ units.paginator.num_pages }}&amp;{{ query_string }}">{% trans "Last" %}</a>
</span>
</span>
{% if units.object_list %}
<table>
<tbody>
{% for unit in units.object_list %}
<tr><th colspan="2">
<a href="{{ unit.translation.get_absolute_url }}">{{ unit.translation }}</a>
{{ unit.get_state_flags }}
</th></tr>
<tr>
<th class="source">{% trans "Source" %}</th>
<td class="translatetext"><a href="{{ unit.get_absolute_url }}">{{ unit.source|fmtsearchmatch:search_query }}</a></td>
</tr>
<tr>
<th class="source">{{ unit.translation.language }}</th>
<td class="translatetext"><a href="{{ unit.get_absolute_url }}">{{ unit.target|fmtsearchmatch:search_query }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>{% trans "No matching strings found!" %}</p>
{% endif %}
<h2>{% trans "Search" %}</h2>
<form action="{% url 'search' %}" method="GET">
{{ search_form.as_p }}
<input type="submit" value="{% trans "Search" %}" class="button" />
</form>
{% endblock %}
......@@ -635,4 +635,10 @@ urlpatterns = patterns(
'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}
),
url(
r'^search/$',
'trans.views.basic.search',
name="search"
),
)
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