Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_edit_plural(self):
language = Language.objects.get(code='cs')
self.user.is_superuser = True
self.user.save()
response = self.client.post(
reverse('edit-plural', kwargs={'pk': language.plural.pk}),
{'number': '2', 'equation': 'n != 1'},
)
self.assertRedirects(
response, reverse('show_language', kwargs={'lang': 'cs'}) + '#information'
)
if self.action == self.ACTION_ACCESS_EDIT:
for number, name in Project.ACCESS_CHOICES:
if number == self.details['access_control']:
return name
return 'Unknonwn {}'.format(self.details['access_control'])
elif self.action in user_actions:
if 'group' in self.details:
return '{username} ({group})'.format(**self.details)
return self.details['username']
elif self.action in (
self.ACTION_ADDED_LANGUAGE,
self.ACTION_REQUESTED_LANGUAGE,
): # noqa: E501
try:
return Language.objects.get(code=self.details['language'])
except Language.DoesNotExist:
return self.details['language']
elif self.action == self.ACTION_ALERT:
try:
return ALERTS[self.details['alert']].verbose
except KeyError:
return self.details['alert']
elif self.action == self.ACTION_PARSE_ERROR:
return '{filename}: {error_message}'.format(**self.details)
elif self.action == self.ACTION_HOOK:
return '{service_long_name}: {repo_url}, {branch}'.format(**self.details)
elif self.action == self.ACTION_COMMENT and 'comment' in self.details:
return render_markdown(self.details['comment'])
return ''
def save_related(self, request, form, formsets, change):
super(LanguageAdmin, self).save_related(request, form, formsets, change)
lang = form.instance
if lang.plural_set.exists():
return
# Automatically create plurals if language does not have one
try:
baselang = Language.objects.get(code=lang.base_code)
baseplural = baselang.plural
lang.plural_set.create(
source=Plural.SOURCE_DEFAULT,
number=baseplural.number,
equation=baseplural.equation,
)
except (Language.DoesNotExist, IndexError):
lang.plural_set.create(
source=Plural.SOURCE_DEFAULT, number=2, equation='n != 1'
)
def _get_queryset_language(self):
"""Filtering by language"""
if self.translation is None and 'lang' in self.request.GET:
try:
self.language = Language.objects.get(
code=self.request.GET['lang']
)
except Language.DoesNotExist:
messages.error(
self.request,
_('Failed to find matching language!')
)
def translations(self, request, **kwargs):
obj = self.get_object()
if request.method == 'POST':
if not request.user.has_perm('translation.add', obj):
self.permission_denied(request, message='Can not create translation')
if 'language_code' not in request.data:
raise ParseError("Missing 'language_code' parameter")
language_code = request.data['language_code']
try:
language = Language.objects.get(code=language_code)
except Language.DoesNotExist:
raise Http404("No language code '%s' found!" % language_code)
obj.add_new_language(language, request)
page = obj.translation_set.get(language=language)
serializer = TranslationSerializer(
page, context={'request': request}, remove_fields=('component',),
)
return Response(
data={
'data': serializer.data,
},
status=status.HTTP_201_CREATED
)
def widgets(request, project):
obj = get_project(request, project)
# Parse possible language selection
form = EngageForm(obj, request.GET)
lang = None
component = None
if form.is_valid():
if form.cleaned_data['lang']:
lang = Language.objects.get(code=form.cleaned_data['lang']).code
if form.cleaned_data['component']:
component = Component.objects.get(
slug=form.cleaned_data['component'],
project=obj
).slug
kwargs = {'project': obj.slug}
if lang is not None:
kwargs['lang'] = lang
engage_url = get_site_url(reverse('engage', kwargs=kwargs))
engage_url_track = '{0}?utm_source=widget'.format(engage_url)
engage_link = mark_safe(
'<a id="engage-link" href="{0}">{0}</a>'.format(escape(engage_url))
)
widget_base_url = get_site_url(
reverse('widgets', kwargs={'project': obj.slug})
def _get_queryset_language(self):
"""Filtering by language"""
if self.translation is None and self.request.GET.get('lang'):
try:
self.language = Language.objects.get(code=self.request.GET['lang'])
except Language.DoesNotExist:
messages.error(self.request, _('Failed to find matching language!'))
def convert_language(self, language):
return Language.objects.get(code=language)
def show_engage(request, project, lang = None):
# Get project object
obj = get_object_or_404(Project, slug = project)
# Handle language parameter
language = None
if lang is not None:
try:
language = Language.objects.get(code = lang)
django.utils.translation.activate(lang)
except Language.DoesNotExist:
pass
return render_to_response('engage.html', RequestContext(request, {
'object': obj,
'project': obj.name,
'languages': obj.get_language_count(),
'total': obj.get_total(),
'percent': obj.get_translated_percent(language),
'url': obj.get_absolute_url(),
'language_name': language,
}))
def language_name(code):
'''
Gets language name based on it's code.
'''
return Language.objects.get(code=code).__unicode__()