Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_create(self, original, expected, direction, plural, name, create):
"""Test that auto create correctly handles languages"""
# Lookup language
lang = Language.objects.auto_get_or_create(original, create=False)
self.assertEqual(
create,
not bool(lang.pk),
'Failed to assert creation for {}: {}'.format(original, create),
)
# Create language
lang = Language.objects.auto_get_or_create(original)
# Check language code
self.assertEqual(
lang.code, expected, 'Invalid code for {0}: {1}'.format(original, lang.code)
)
# Check direction
self.assertEqual(
lang.direction, direction, 'Invalid direction for {0}'.format(original)
)
# Check plurals
def auto_create(self, code, create=True):
"""Automatically create new language based on code and best guess
of parameters.
"""
# Create standard language
name = '{0} (generated)'.format(code)
if create:
lang = self.get_or_create(code=code, defaults={'name': name})[0]
else:
lang = Language(code=code, name=name)
baselang = None
# Check for different variant
if baselang is None and '@' in code:
parts = code.split('@')
baselang = self.fuzzy_get(code=parts[0], strict=True)
# Check for different country
if baselang is None and '_' in code or '-' in code:
parts = code.replace('-', '_').split('_')
baselang = self.fuzzy_get(code=parts[0], strict=True)
if baselang is not None:
lang.name = baselang.name
lang.direction = baselang.direction
def daily(self, component):
self.ensure_all_have(
component.project,
Language.objects.filter(translation__component=component)
)
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 language_name(code):
"""Get language name based on its code."""
return escape(force_text(Language.objects.get(code=code)))
def show_engage(request, project, lang=None):
# Get project object, skipping ACL
obj = get_project(request, project, skip_acl=True)
# Handle language parameter
if lang is not None:
language = get_object_or_404(Language, code=lang)
else:
language = None
full_stats = obj.stats
if language:
try_set_language(lang)
stats_obj = full_stats.get_single_language_stats(language)
else:
stats_obj = full_stats
return render(
request,
'engage.html',
{
'allow_index': True,
'object': obj,
'project': obj,
def get_languages(self):
"""Returns list of all languages used in project."""
return Language.objects.filter(
translation__subproject__project=self
).distinct()
@python_2_unicode_compatible
class Profile(models.Model):
"""User profiles storage."""
user = models.OneToOneField(
User, unique=True, editable=False, on_delete=models.deletion.CASCADE
)
language = models.CharField(
verbose_name=_('Interface Language'),
max_length=10,
blank=True,
choices=settings.LANGUAGES,
)
languages = models.ManyToManyField(
Language,
verbose_name=_('Translated languages'),
blank=True,
help_text=_(
'Choose the languages you can translate to. '
'These will be offered to you on the dashboard '
'for easier access to your chosen translations.'
),
)
secondary_languages = models.ManyToManyField(
Language,
verbose_name=_('Secondary languages'),
help_text=_(
'Choose languages you can understand, strings in those languages '
'will be shown in addition to the source string.'
),
related_name='secondary_profile_set',
name='project',
slug='project',
id=-1,
),
name='component',
slug='component',
branch='master',
vcs='git',
id=-1,
)
if translation:
kwargs['translation'] = Translation(
id=-1,
component=component,
language_code='xx',
language=Language(name='xxx', code='xx'),
)
else:
kwargs['component'] = component
validate_render(value, **kwargs)