xmpp.chapril.org-conversejs/trans/views.py

118 lines
4.4 KiB
Python
Raw Normal View History

2012-02-27 11:18:49 +01:00
from django.shortcuts import render_to_response, get_object_or_404
2012-02-29 14:08:37 +01:00
from django.utils.translation import ugettext_lazy, ugettext as _
2012-02-27 11:18:49 +01:00
from django.template import RequestContext
2012-02-29 11:16:05 +01:00
from django.conf import settings
2012-02-29 11:41:08 +01:00
from django.http import HttpResponseRedirect
2012-02-29 14:08:37 +01:00
from django.contrib import messages
2012-02-27 11:18:49 +01:00
2012-03-01 11:01:14 +01:00
from trans.models import Project, SubProject, Translation, Unit, Suggestion
2012-02-29 11:41:08 +01:00
from trans.forms import TranslationForm
2012-02-29 14:12:54 +01:00
import logging
logger = logging.getLogger('weblate')
2012-02-27 11:18:49 +01:00
2012-02-29 11:16:05 +01:00
def home(request):
projects = Project.objects.all()
return render_to_response('index.html', RequestContext(request, {
'projects': projects,
'title': settings.SITE_TITLE,
}))
2012-02-27 11:18:49 +01:00
def show_project(request, project):
obj = get_object_or_404(Project, slug = project)
2012-02-27 11:58:55 +01:00
return render_to_response('project.html', RequestContext(request, {
2012-02-27 11:18:49 +01:00
'object': obj,
2012-02-29 11:17:32 +01:00
'title': '%s @ %s' % (obj.__unicode__(), settings.SITE_TITLE),
2012-02-27 11:47:34 +01:00
}))
2012-02-27 11:18:49 +01:00
def show_subproject(request, project, subproject):
obj = get_object_or_404(SubProject, slug = subproject, project__slug = project)
2012-02-27 11:58:55 +01:00
return render_to_response('subproject.html', RequestContext(request, {
2012-02-27 11:18:49 +01:00
'object': obj,
2012-02-29 11:17:32 +01:00
'title': '%s @ %s' % (obj.__unicode__(), settings.SITE_TITLE),
2012-02-27 11:47:34 +01:00
}))
2012-02-27 11:18:49 +01:00
def show_translation(request, project, subproject, lang):
2012-02-28 10:31:10 +01:00
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project)
2012-02-27 11:18:49 +01:00
2012-02-27 11:58:55 +01:00
return render_to_response('translation.html', RequestContext(request, {
2012-02-27 11:18:49 +01:00
'object': obj,
2012-02-29 11:17:32 +01:00
'title': '%s @ %s' % (obj.__unicode__(), settings.SITE_TITLE),
2012-02-27 11:47:34 +01:00
}))
2012-02-27 11:18:49 +01:00
2012-02-28 14:55:34 +01:00
def translate(request, project, subproject, lang):
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project)
2012-02-29 11:41:08 +01:00
# Check where we are
2012-02-28 15:00:23 +01:00
rqtype = request.REQUEST.get('type', 'all')
2012-02-29 13:02:43 +01:00
direction = request.REQUEST.get('dir', 'forward')
2012-02-28 15:16:02 +01:00
pos = request.REQUEST.get('oldpos', '-1')
2012-02-28 15:00:23 +01:00
try:
2012-02-28 15:16:02 +01:00
pos = int(pos)
2012-02-28 15:00:23 +01:00
except:
2012-02-28 15:16:02 +01:00
pos = -1
2012-02-29 11:41:08 +01:00
unit = None
2012-02-29 11:41:08 +01:00
# Any form submitted?
if request.method == 'POST':
form = TranslationForm(request.POST)
if form.is_valid():
obj.check_sync()
try:
unit = Unit.objects.get(checksum = form.cleaned_data['checksum'], translation = obj)
if 'suggest' in request.POST:
2012-03-01 11:01:14 +01:00
Suggestion.objects.create(
target = form.cleaned_data['target'],
checksum = unit.checksum,
language = unit.translation.language,
project = unit.translation.subproject.project,
user = request.user)
elif not request.user.is_authenticated():
messages.add_message(request, messages.ERROR, _('You need to login to be able to save translations!'))
else:
unit.target = form.cleaned_data['target']
unit.fuzzy = form.cleaned_data['fuzzy']
unit.save_backend(request)
# Check and save
return HttpResponseRedirect('%s?type=%s&oldpos=%d' % (obj.get_translate_url(), rqtype, pos))
except Unit.DoesNotExist:
2012-02-29 14:12:54 +01:00
logger.error('message %s disappeared!', form.cleaned_data['checksum'])
messages.add_message(request, messages.ERROR, _('Message you wanted to translate is no longer available!'))
# If we failed to get unit above or on no POST
if unit is None:
2012-02-29 11:41:08 +01:00
# What unit to show
2012-02-29 13:02:43 +01:00
if direction == 'back':
2012-02-29 13:56:58 +01:00
units = obj.unit_set.filter_type(rqtype).filter(position__lt = pos)
2012-02-29 13:02:43 +01:00
else:
2012-02-29 13:56:58 +01:00
units = obj.unit_set.filter_type(rqtype).filter(position__gt = pos)
try:
unit = units[0]
except IndexError:
2012-02-29 14:08:37 +01:00
messages.add_message(request, messages.INFO, _('You have reached end of translating.'))
2012-02-29 13:56:58 +01:00
return HttpResponseRedirect(obj.get_absolute_url())
2012-02-29 11:41:08 +01:00
# Prepare form
form = TranslationForm(initial = {
'checksum': unit.checksum,
'target': unit.target,
'fuzzy': unit.fuzzy,
})
2012-02-28 15:16:02 +01:00
total = obj.unit_set.all().count()
2012-02-28 14:55:34 +01:00
return render_to_response('translate.html', RequestContext(request, {
'object': obj,
2012-02-29 11:17:32 +01:00
'title': '%s @ %s' % (obj.__unicode__(), settings.SITE_TITLE),
2012-02-28 15:16:02 +01:00
'unit': unit,
2012-02-28 15:09:03 +01:00
'total': total,
2012-02-28 15:40:08 +01:00
'type': rqtype,
2012-02-29 11:41:08 +01:00
'form': form,
2012-02-28 14:55:34 +01:00
}))