Commit 5f45f9f6 authored by Michal Čihař's avatar Michal Čihař

Improved adding of new translations

- owners or superusers can choose multiple languages at once
- the selection no longer offers already existing languages
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent aa4cd5c5
......@@ -46,6 +46,7 @@ Released on ? 2016.
* Added XLIFF and Gettext PO export for all translations.
* Documentation improvements.
* Added support for configurable automatic group assignments.
* Improved adding of new translations.
weblate 2.4
-----------
......
......@@ -794,7 +794,24 @@ class EnageLanguageForm(forms.Form):
self.fields['lang'].choices += choices
class NewLanguageForm(forms.Form):
class NewLanguageOwnerForm(forms.Form):
'''
Form for requesting new language.
'''
lang = forms.MultipleChoiceField(
label=_('Languages'),
choices=[]
)
def __init__(self, component, *args, **kwargs):
super(NewLanguageOwnerForm, self).__init__(*args, **kwargs)
languages = Language.objects.exclude(translation__subproject=component)
self.fields['lang'].choices = sort_choices([
(l.code, force_text(l)) for l in languages
])
class NewLanguageForm(NewLanguageOwnerForm):
'''
Form for requesting new language.
'''
......@@ -803,12 +820,20 @@ class NewLanguageForm(forms.Form):
choices=[]
)
def __init__(self, *args, **kwargs):
super(NewLanguageForm, self).__init__(*args, **kwargs)
choices = sort_choices([('', _('Please select'))] + [
(l.code, force_text(l)) for l in Language.objects.all()
])
self.fields['lang'].choices = choices
def __init__(self, component, *args, **kwargs):
super(NewLanguageForm, self).__init__(component, *args, **kwargs)
self.fields['lang'].choices = (
[('', _('Please select'))] + self.fields['lang'].choices
)
def get_new_language_form(request, component):
"""Returns new language form for user"""
if request.user.is_superuser:
return NewLanguageOwnerForm
if component.project.owners.filter(id=request.user.id).exists():
return NewLanguageOwnerForm
return NewLanguageForm
class PriorityForm(forms.Form):
......
......@@ -230,7 +230,10 @@ class ViewTestCase(RepoTestCase):
class NewLangTest(ViewTestCase):
def create_subproject(self):
return self.create_po_new_base()
subproject = self.create_po_new_base()
subproject.new_lang = 'add'
subproject.save()
return subproject
def test_none(self):
self.subproject.new_lang = 'none'
......@@ -280,9 +283,6 @@ class NewLangTest(ViewTestCase):
)
def test_add(self):
self.subproject.new_lang = 'add'
self.subproject.save()
self.assertFalse(
self.subproject.translation_set.filter(
language__code='af'
......@@ -317,7 +317,7 @@ class NewLangTest(ViewTestCase):
)
self.assertContains(
response,
'Please choose the language'
'Invalid language chosen'
)
# Existing language
......@@ -328,7 +328,45 @@ class NewLangTest(ViewTestCase):
)
self.assertContains(
response,
'Chosen translation already exists'
'Invalid language chosen'
)
def test_add_owner(self):
self.subproject.project.owners.add(self.user)
# None chosen
response = self.client.post(
reverse('new-language', kwargs=self.kw_subproject),
follow=True
)
self.assertContains(
response,
'Invalid language chosen'
)
# One chosen
response = self.client.post(
reverse('new-language', kwargs=self.kw_subproject),
{'lang': 'af'},
follow=True
)
self.assertNotContains(
response,
'Invalid language chosen'
)
# More chosen
response = self.client.post(
reverse('new-language', kwargs=self.kw_subproject),
{'lang': ['nl', 'fr', 'uk']},
follow=True
)
self.assertNotContains(
response,
'Invalid language chosen'
)
self.assertEqual(
self.subproject.translation_set.filter(
language__code__in=('af', 'nl', 'fr', 'uk')
).count(),
4
)
......
......@@ -20,6 +20,7 @@
import datetime
from six import string_types
from six.moves.urllib.parse import urlencode
from django.shortcuts import redirect
......@@ -40,7 +41,7 @@ from weblate.requirements import get_versions, get_optional_versions
from weblate.lang.models import Language
from weblate.trans.forms import (
get_upload_form, SearchForm, SiteSearchForm,
AutoForm, ReviewForm, NewLanguageForm,
AutoForm, ReviewForm, get_new_language_form,
UserManageForm, ReportsForm,
)
from weblate.trans.permissions import can_automatic_translation
......@@ -299,7 +300,7 @@ def show_subproject(request, project, subproject):
translation__subproject=obj
)[:10]
new_lang_form = NewLanguageForm()
new_lang_form = get_new_language_form(request, obj)(obj)
return render(
request,
......@@ -511,34 +512,29 @@ def data_project(request, project):
def new_language(request, project, subproject):
obj = get_subproject(request, project, subproject)
form = NewLanguageForm(request.POST)
form = get_new_language_form(request, obj)(obj, request.POST)
if form.is_valid():
language = Language.objects.get(code=form.cleaned_data['lang'])
same_lang = obj.translation_set.filter(language=language)
if same_lang.exists():
messages.error(
request,
_('Chosen translation already exists in this project!')
)
elif obj.new_lang == 'contact':
notify_new_language(obj, language, request.user)
messages.success(
request,
_(
"A request for a new translation has been "
"sent to the project's maintainers."
langs = form.cleaned_data['lang']
if isinstance(langs, string_types):
langs = [langs]
for language in Language.objects.filter(code__in=langs):
same_lang = obj.translation_set.filter(language=language)
if obj.new_lang == 'contact':
notify_new_language(obj, language, request.user)
messages.success(
request,
_(
"A request for a new translation has been "
"sent to the project's maintainers."
)
)
)
elif obj.new_lang == 'add':
obj.add_new_language(language, request)
elif obj.new_lang == 'add':
obj.add_new_language(language, request)
else:
messages.error(
request,
_(
'Please choose the language into which '
'you would like to translate.'
)
_('Invalid language chosen!')
)
return redirect(obj)
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