Commit 63138da8 authored by Michal Čihař's avatar Michal Čihař

Implemented searching

parent 7779d579
......@@ -16,17 +16,23 @@
<p>{% blocktrans with unit.position as position %}Showing string {{ position }} out of {{ total }}.{% endblocktrans %}
<span class="navi">
<a class="button-first" href="{{ unit.translation.get_translate_url }}?type={{ type }}&amp;oldpos=1&amp;dir=stay">{% trans "First" %}</a>
<a class="button-prev" href="{{ unit.translation.get_translate_url }}?type={{ type }}&amp;oldpos={{ unit.position }}&amp;dir=back">{% trans "Previous" %}</a>
<a class="button-next" href="{{ unit.translation.get_translate_url }}?type={{ type }}&amp;oldpos={{ unit.position }}">{% trans "Next" %}</a>
<a class="button-end" href="{{ unit.translation.get_translate_url }}?type={{ type }}&amp;oldpos={{ total }}&amp;dir=stay">{% trans "Last" %}</a>
<a class="button-first" href="{{ unit.translation.get_translate_url }}?type={{ type }}&amp;oldpos=1&amp;dir=stay{{ search_url }}">{% trans "First" %}</a>
<a class="button-prev" href="{{ unit.translation.get_translate_url }}?type={{ type }}&amp;oldpos={{ unit.position }}&amp;dir=back{{ search_url }}">{% trans "Previous" %}</a>
<a class="button-next" href="{{ unit.translation.get_translate_url }}?type={{ type }}&amp;oldpos={{ unit.position }}{{ search_url }}">{% trans "Next" %}</a>
<a class="button-end" href="{{ unit.translation.get_translate_url }}?type={{ type }}&amp;oldpos={{ total }}&amp;dir=stay{{ search_url }}">{% trans "Last" %}</a>
</span>
</p>
<p>{% blocktrans %}Searching for "{{ search_query }}".{% endblocktrans %}</p>
<form action="{{ unit.translation.get_translate_url }}" method="post">
{% csrf_token %}
<input type="hidden" name="type" value="{{ type }}" />
<input type="hidden" name="oldpos" value="{{ unit.position }}" />
<input type="hidden" name="q" value="{{ search_query }}" />
<input type="hidden" name="src" value="{{ search_source }}" />
<input type="hidden" name="tgt" value="{{ search_target }}" />
<input type="hidden" name="ctx" value="{{ search_context }}" />
<table class="translator">
<tr>
<th class="source">{% trans "Source" %}</th>
......@@ -60,8 +66,8 @@
{% endif %}
{% if user.is_authenticated %}
<div class="suggestionactions">
<a href="{{ suggestion.get_translate_url }}?type={{ type }}&amp;oldpos={{ unit.position }}&amp;accept={{ suggestion.id }}" class="sug-accept">Accept</a>
<a href="{{ suggestion.get_translate_url }}?type={{ type }}&amp;oldpos={{ unit.position }}&amp;delete={{ suggestion.id }}" class="sug-delete">Delete</a>
<a href="{{ suggestion.get_translate_url }}?type={{ type }}&amp;oldpos={{ unit.position }}&amp;accept={{ suggestion.id }}{{ search_url }}" class="sug-accept">Accept</a>
<a href="{{ suggestion.get_translate_url }}?type={{ type }}&amp;oldpos={{ unit.position }}&amp;delete={{ suggestion.id }}{{ search_url }}" class="sug-delete">Delete</a>
</div>
{% endif %}
</td>
......
......@@ -23,6 +23,13 @@
{% endif %}
{% endwith %}
<h2>{% trans "Search" %}</h2>
<form action="{{ object.get_translate_url }}" method="GET">
{{ search_form.as_p }}
<input type="submit" value="{% trans "Search" %}" class="button" />
</form>
<h2>{% trans "Download" %}</h2>
{% with object.get_download_url as download_url %}
......
......@@ -47,3 +47,9 @@ class TranslationForm(forms.Form):
class UploadForm(forms.Form):
file = forms.FileField(label = _('File'))
overwrite = forms.BooleanField(label = _('Overwrite existing translations'), required = False)
class SearchForm(forms.Form):
q = forms.CharField(label = _('Query'))
src = forms.BooleanField(label = _('Search in source strings'), required = False, initial = True)
tgt = forms.BooleanField(label = _('Search in target strings'), required = False, initial = True)
ctx = forms.BooleanField(label = _('Search in context strings'), required = False, initial = True)
......@@ -7,9 +7,10 @@ from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import AnonymousUser
from django.db.models import Q
from trans.models import Project, SubProject, Translation, Unit, Suggestion
from trans.forms import TranslationForm, UploadForm
from trans.forms import TranslationForm, UploadForm, SearchForm
from util import is_plural, split_plural, join_plural
from accounts.models import Profile
import logging
......@@ -56,11 +57,13 @@ def show_subproject(request, project, subproject):
def show_translation(request, project, subproject, lang):
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project)
form = UploadForm()
search_form = SearchForm()
return render_to_response('translation.html', RequestContext(request, {
'object': obj,
'title': '%s @ %s' % (obj.__unicode__(), settings.SITE_TITLE),
'form': form,
'search_form': search_form,
}))
def download_translation(request, project, subproject, lang):
......@@ -80,6 +83,11 @@ def download_translation(request, project, subproject, lang):
return response
def bool2str(val):
if val:
return 'on'
return ''
def translate(request, project, subproject, lang):
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project)
......@@ -94,6 +102,25 @@ def translate(request, project, subproject, lang):
unit = None
s = SearchForm(request.GET)
if s.is_valid():
search_query = s.cleaned_data['q']
search_source = s.cleaned_data['src']
search_target = s.cleaned_data['tgt']
search_context = s.cleaned_data['ctx']
search_url = '&q=%s&src=%s&tgt=%s&ctx=%s' % (
search_query,
bool2str(search_source),
bool2str(search_target),
bool2str(search_context)
)
else:
search_query = ''
search_source = True
search_target = True
search_context = True
search_url = ''
# Any form submitted?
if request.method == 'POST':
form = TranslationForm(request.POST)
......@@ -126,7 +153,12 @@ def translate(request, project, subproject, lang):
profile.save()
# Check and save
return HttpResponseRedirect('%s?type=%s&oldpos=%d' % (obj.get_translate_url(), rqtype, pos))
return HttpResponseRedirect('%s?type=%s&oldpos=%d%s' % (
obj.get_translate_url(),
rqtype,
pos,
search_url
))
except Unit.DoesNotExist:
logger.error('message %s disappeared!', form.cleaned_data['checksum'])
messages.add_message(request, messages.ERROR, _('Message you wanted to translate is no longer available!'))
......@@ -135,7 +167,12 @@ def translate(request, project, subproject, lang):
if 'accept' in request.GET or 'delete' in request.GET:
if not request.user.is_authenticated():
messages.add_message(request, messages.ERROR, _('You need to login to be able to manage suggestions!'))
return HttpResponseRedirect('%s?type=%s&oldpos=%d&dir=stay' % (obj.get_translate_url(), rqtype, pos))
return HttpResponseRedirect('%s?type=%s&oldpos=%d&dir=stay%s' % (
obj.get_translate_url(),
rqtype,
pos,
search_url
))
if 'accept' in request.GET:
sugid = request.GET['accept']
else:
......@@ -152,7 +189,12 @@ def translate(request, project, subproject, lang):
suggestion.delete()
else:
messages.add_message(request, messages.ERROR, _('Invalid suggestion!'))
return HttpResponseRedirect('%s?type=%s&oldpos=%d&dir=stay' % (obj.get_translate_url(), rqtype, pos))
return HttpResponseRedirect('%s?type=%s&oldpos=%d&dir=stay%s' % (
obj.get_translate_url(),
rqtype,
pos,
search_url
))
# If we failed to get unit above or on no POST
if unit is None:
......@@ -163,6 +205,11 @@ def translate(request, project, subproject, lang):
units = obj.unit_set.filter_type(rqtype).filter(position__lt = pos).order_by('-position')
else:
units = obj.unit_set.filter_type(rqtype).filter(position__gt = pos)
if search_query != '':
query = Q()
if search_source:
query |= Q(source__icontains = search_query)
units = units.filter(query)
try:
unit = units[0]
......@@ -186,6 +233,12 @@ def translate(request, project, subproject, lang):
'total': total,
'type': rqtype,
'form': form,
'search_query': search_query,
'search_url': search_url,
'search_query': search_query,
'search_source': bool2str(search_source),
'search_target': bool2str(search_target),
'search_context': bool2str(search_context),
}))
def get_string(request, checksum):
......
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