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. ...@@ -46,6 +46,7 @@ Released on ? 2016.
* Added XLIFF and Gettext PO export for all translations. * Added XLIFF and Gettext PO export for all translations.
* Documentation improvements. * Documentation improvements.
* Added support for configurable automatic group assignments. * Added support for configurable automatic group assignments.
* Improved adding of new translations.
weblate 2.4 weblate 2.4
----------- -----------
......
...@@ -794,7 +794,24 @@ class EnageLanguageForm(forms.Form): ...@@ -794,7 +794,24 @@ class EnageLanguageForm(forms.Form):
self.fields['lang'].choices += choices 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. Form for requesting new language.
''' '''
...@@ -803,12 +820,20 @@ class NewLanguageForm(forms.Form): ...@@ -803,12 +820,20 @@ class NewLanguageForm(forms.Form):
choices=[] choices=[]
) )
def __init__(self, *args, **kwargs): def __init__(self, component, *args, **kwargs):
super(NewLanguageForm, self).__init__(*args, **kwargs) super(NewLanguageForm, self).__init__(component, *args, **kwargs)
choices = sort_choices([('', _('Please select'))] + [ self.fields['lang'].choices = (
(l.code, force_text(l)) for l in Language.objects.all() [('', _('Please select'))] + self.fields['lang'].choices
]) )
self.fields['lang'].choices = 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): class PriorityForm(forms.Form):
......
...@@ -230,7 +230,10 @@ class ViewTestCase(RepoTestCase): ...@@ -230,7 +230,10 @@ class ViewTestCase(RepoTestCase):
class NewLangTest(ViewTestCase): class NewLangTest(ViewTestCase):
def create_subproject(self): 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): def test_none(self):
self.subproject.new_lang = 'none' self.subproject.new_lang = 'none'
...@@ -280,9 +283,6 @@ class NewLangTest(ViewTestCase): ...@@ -280,9 +283,6 @@ class NewLangTest(ViewTestCase):
) )
def test_add(self): def test_add(self):
self.subproject.new_lang = 'add'
self.subproject.save()
self.assertFalse( self.assertFalse(
self.subproject.translation_set.filter( self.subproject.translation_set.filter(
language__code='af' language__code='af'
...@@ -317,7 +317,7 @@ class NewLangTest(ViewTestCase): ...@@ -317,7 +317,7 @@ class NewLangTest(ViewTestCase):
) )
self.assertContains( self.assertContains(
response, response,
'Please choose the language' 'Invalid language chosen'
) )
# Existing language # Existing language
...@@ -328,7 +328,45 @@ class NewLangTest(ViewTestCase): ...@@ -328,7 +328,45 @@ class NewLangTest(ViewTestCase):
) )
self.assertContains( self.assertContains(
response, 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 @@ ...@@ -20,6 +20,7 @@
import datetime import datetime
from six import string_types
from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlencode
from django.shortcuts import redirect from django.shortcuts import redirect
...@@ -40,7 +41,7 @@ from weblate.requirements import get_versions, get_optional_versions ...@@ -40,7 +41,7 @@ from weblate.requirements import get_versions, get_optional_versions
from weblate.lang.models import Language from weblate.lang.models import Language
from weblate.trans.forms import ( from weblate.trans.forms import (
get_upload_form, SearchForm, SiteSearchForm, get_upload_form, SearchForm, SiteSearchForm,
AutoForm, ReviewForm, NewLanguageForm, AutoForm, ReviewForm, get_new_language_form,
UserManageForm, ReportsForm, UserManageForm, ReportsForm,
) )
from weblate.trans.permissions import can_automatic_translation from weblate.trans.permissions import can_automatic_translation
...@@ -299,7 +300,7 @@ def show_subproject(request, project, subproject): ...@@ -299,7 +300,7 @@ def show_subproject(request, project, subproject):
translation__subproject=obj translation__subproject=obj
)[:10] )[:10]
new_lang_form = NewLanguageForm() new_lang_form = get_new_language_form(request, obj)(obj)
return render( return render(
request, request,
...@@ -511,17 +512,15 @@ def data_project(request, project): ...@@ -511,17 +512,15 @@ def data_project(request, project):
def new_language(request, project, subproject): def new_language(request, project, subproject):
obj = get_subproject(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(): if form.is_valid():
language = Language.objects.get(code=form.cleaned_data['lang']) 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) same_lang = obj.translation_set.filter(language=language)
if same_lang.exists(): if obj.new_lang == 'contact':
messages.error(
request,
_('Chosen translation already exists in this project!')
)
elif obj.new_lang == 'contact':
notify_new_language(obj, language, request.user) notify_new_language(obj, language, request.user)
messages.success( messages.success(
request, request,
...@@ -535,10 +534,7 @@ def new_language(request, project, subproject): ...@@ -535,10 +534,7 @@ def new_language(request, project, subproject):
else: else:
messages.error( messages.error(
request, request,
_( _('Invalid language chosen!')
'Please choose the language into which '
'you would like to translate.'
)
) )
return redirect(obj) 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