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

215 lines
8.6 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-03-01 17:14:25 +01:00
from django.core.servers.basehttp import FileWrapper
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-03-01 17:09:03 +01:00
from django.http import HttpResponse, HttpResponseRedirect
2012-02-29 14:08:37 +01:00
from django.contrib import messages
2012-03-05 10:39:48 +01:00
from django.contrib.auth.decorators import login_required
2012-03-05 10:43:54 +01:00
from django.contrib.auth.models import AnonymousUser
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-03-04 09:37:22 +01:00
from trans.forms import TranslationForm, UploadForm
2012-03-01 13:49:36 +01:00
from util import is_plural, split_plural, join_plural
2012-03-05 13:55:09 +01:00
from accounts.models import Profile
2012-02-29 14:12:54 +01:00
import logging
2012-03-01 17:14:25 +01:00
import os.path
2012-02-29 14:12:54 +01:00
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()
2012-03-05 14:25:27 +01:00
usertranslations = None
if request.user.is_authenticated():
profile = request.user.get_profile()
2012-03-05 14:26:17 +01:00
usertranslations = Translation.objects.filter(language__in = profile.languages.all()).order_by('subproject__project__name', 'subproject__name')
2012-03-05 14:25:27 +01:00
2012-03-05 13:55:09 +01:00
top_translations = Profile.objects.order_by('-translated')[:10]
top_suggestions = Profile.objects.order_by('-suggested')[:10]
2012-02-29 11:16:05 +01:00
return render_to_response('index.html', RequestContext(request, {
'projects': projects,
2012-03-05 13:55:09 +01:00
'top_translations': top_translations,
'top_suggestions': top_suggestions,
2012-03-05 14:25:27 +01:00
'usertranslations': usertranslations,
2012-02-29 11:16:05 +01:00
'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-03-04 09:37:22 +01:00
form = UploadForm()
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-03-04 09:37:22 +01:00
'form': form,
2012-02-27 11:47:34 +01:00
}))
2012-02-27 11:18:49 +01:00
2012-03-01 17:09:03 +01:00
def download_translation(request, project, subproject, lang):
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project)
2012-03-01 17:11:06 +01:00
store = obj.get_store()
2012-03-01 17:14:25 +01:00
srcfilename = obj.get_filename()
2012-03-01 17:09:03 +01:00
mime = store.Mimetypes[0]
ext = store.Extensions[0]
filename = '%s-%s-%s.%s' % (project, subproject, lang, ext)
2012-03-01 17:14:25 +01:00
wrapper = FileWrapper(file(srcfilename))
response = HttpResponse(wrapper, mimetype = mime)
2012-03-01 17:09:03 +01:00
response['Content-Disposition'] = 'attachment; filename=%s' % filename
2012-03-01 17:14:25 +01:00
response['Content-Length'] = os.path.getsize(srcfilename)
2012-03-01 17:09:03 +01:00
return response
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-05 10:43:54 +01:00
user = request.user
if isinstance(user, AnonymousUser):
user = None
2012-03-01 11:01:14 +01:00
Suggestion.objects.create(
2012-03-01 13:49:36 +01:00
target = join_plural(form.cleaned_data['target']),
2012-03-01 11:01:14 +01:00
checksum = unit.checksum,
language = unit.translation.language,
project = unit.translation.subproject.project,
2012-03-05 10:43:54 +01:00
user = user)
2012-03-04 09:30:12 +01:00
if request.user.is_authenticated():
2012-03-05 10:37:33 +01:00
profile = request.user.get_profile()
2012-03-04 09:30:12 +01:00
profile.suggested += 1
profile.save()
elif not request.user.is_authenticated():
messages.add_message(request, messages.ERROR, _('You need to login to be able to save translations!'))
else:
2012-03-01 13:49:36 +01:00
unit.target = join_plural(form.cleaned_data['target'])
unit.fuzzy = form.cleaned_data['fuzzy']
unit.save_backend(request)
2012-03-05 10:37:33 +01:00
profile = request.user.get_profile()
2012-03-04 09:30:12 +01:00
profile.translated += 1
profile.save()
# 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!'))
2012-03-01 11:46:30 +01:00
# Handle suggestions
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))
2012-03-01 11:46:30 +01:00
if 'accept' in request.GET:
sugid = request.GET['accept']
else:
sugid = request.GET['delete']
try:
sugid = int(sugid)
suggestion = Suggestion.objects.get(pk = sugid)
except:
suggestion = None
if suggestion is not None:
if 'accept' in request.GET:
suggestion.accept(request)
suggestion.delete()
else:
messages.add_message(request, messages.ERROR, _('Invalid suggestion!'))
2012-03-01 11:50:07 +01:00
return HttpResponseRedirect('%s?type=%s&oldpos=%d&dir=stay' % (obj.get_translate_url(), rqtype, pos))
2012-03-01 11:46:30 +01:00
# 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-03-01 11:50:07 +01:00
if direction == 'stay':
units = obj.unit_set.filter(position = pos)
elif direction == 'back':
units = obj.unit_set.filter_type(rqtype).filter(position__lt = pos).order_by('-position')
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,
2012-03-01 13:31:10 +01:00
'target': unit.get_target_plurals(),
2012-02-29 11:41:08 +01:00
'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
}))
2012-03-02 16:01:53 +01:00
def get_string(request, checksum):
units = Unit.objects.filter(checksum = checksum)
if units.count() == 0:
return HttpResponse('')
return HttpResponse(units[0].get_source_plurals()[0])
2012-03-04 09:37:22 +01:00
2012-03-05 10:39:48 +01:00
@login_required
2012-03-04 09:37:22 +01:00
def upload_translation(request, project, subproject, lang):
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project)
if request.method == 'POST':
2012-03-04 09:41:58 +01:00
form = UploadForm(request.POST, request.FILES)
2012-03-04 09:37:22 +01:00
if form.is_valid():
try:
ret = obj.merge_upload(request, request.FILES['file'], form.cleaned_data['overwrite'])
if ret:
messages.add_message(request, messages.INFO, _('File content successfully merged into translation.'))
else:
messages.add_message(request, messages.INFO, _('There were no new strings in uploaded file.'))
except Exception, e:
messages.add_message(request, messages.ERROR, _('File content merge failed: %s' % str(e)))
2012-03-04 09:37:22 +01:00
return HttpResponseRedirect(obj.get_absolute_url())