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_creation(self):
link = self.component.get_repo_link_url()
self.assertEqual(Component.objects.filter(repo=link).count(), 0)
addon = DiscoveryAddon.create(
self.component,
configuration={
'file_format': 'po',
'match': r'(?P[^/]*)/(?P[^/]*)\.po',
'name_template': '{{ component|title }}',
'language_regex': '^(?!xx).*$',
'base_file_template': '',
'remove': True,
},
)
self.assertEqual(Component.objects.filter(repo=link).count(), 3)
addon.post_update(self.component, '')
self.assertEqual(Component.objects.filter(repo=link).count(), 3)
def clean_repo(self):
repo = self.cleaned_data.get('repo')
if not repo or not is_repo_link(repo) or '/' not in repo[10:]:
return repo
project, component = repo[10:].split('/', 1)
try:
obj = Component.objects.get(slug=component, project__slug=project)
except Component.DoesNotExist:
return repo
if not self.request.user.has_perm('component.edit', obj):
raise ValidationError(
_('You do not have permission to access this component!')
)
return repo
def get_translation(self, **options):
"""Get translation object"""
try:
component = Component.objects.get(
project__slug=options['project'],
slug=options['component'],
)
except Component.DoesNotExist:
raise CommandError('No matching translation component found!')
try:
return Translation.objects.get(
component=component,
language__code=options['language'],
)
except Translation.DoesNotExist:
if 'add' in options and options['add']:
language = Language.objects.fuzzy_get(options['language'])
if component.add_new_language(language, None):
return Translation.objects.get(
component=component,
language=language,
)
raise CommandError('No matching translation project found!')
def scratch_create_component(project, name, slug, file_format):
format_cls = FILE_FORMATS[file_format]
template = '{}.{}'.format(project.source_language.code, format_cls.extension())
fake = Component(project=project, slug=slug, name=name)
# Create VCS with empty file
LocalRepository.from_files(
fake.full_path,
{template: format_cls.get_new_file_content()}
)
# Create component
return Component.objects.create(
file_format=file_format,
filemask='*.{}'.format(format_cls.extension()),
template=template,
vcs='local',
repo='local:',
project=project,
name=name,
slug=slug,
)
def validate_render_component(value, translation=None, **kwargs):
from weblate.trans.models import Project, Component, Translation
from weblate.lang.models import Language
component = Component(
project=Project(
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',
@receiver(post_save, sender=Component)
@receiver(post_save, sender=Project)
@receiver(post_save, sender=Plan)
@disable_for_loaddata
def update_project_bill(sender, instance, **kwargs):
if isinstance(instance, Component):
instance = instance.project
for billing in instance.billing_set.iterator():
billing.check_limits()
def clean_instance(self, data):
params = copy.copy(data)
if 'discovery' in params:
params.pop('discovery')
instance = Component(**params)
instance.clean_fields(exclude=('filemask', 'file_format'))
instance.validate_unique()
instance.clean_repo()
self.instance = instance
# Create linked repos automatically
if not self.instance.is_repo_link and self.instance.vcs != 'local':
same_repo = instance.project.component_set.filter(
repo=instance.repo, vcs=instance.vcs, branch=instance.branch
)
if same_repo.exists():
component = same_repo[0]
data['repo'] = component.get_repo_link_url()
data['branch'] = ''
self.clean_instance(data)
self.register(Profile, ProfileAdmin)
self.register(VerifiedEmail, VerifiedEmailAdmin)
# Languages
self.register(Language, LanguageAdmin)
# Screenshots
self.register(Screenshot, ScreenshotAdmin)
# Fonts
self.register(Font, FontAdmin)
self.register(FontGroup, FontGroupAdmin)
# Translations
self.register(Project, ProjectAdmin)
self.register(Component, ComponentAdmin)
self.register(WhiteboardMessage, WhiteboardMessageAdmin)
self.register(ComponentList, ComponentListAdmin)
self.register(ContributorAgreement, ContributorAgreementAdmin)
# Show some controls only in debug mode
if settings.DEBUG:
self.register(Translation, TranslationAdmin)
self.register(Unit, UnitAdmin)
self.register(Suggestion, SuggestionAdmin)
self.register(Comment, CommentAdmin)
self.register(Check, CheckAdmin)
self.register(Dictionary, DictionaryAdmin)
self.register(Change, ChangeAdmin)
if settings.BILLING_ADMIN:
# Billing
match = None
for match in discovery.matched_components.values():
if match['slug'] == self.main_component:
break
if match is None or match['slug'] != self.main_component:
raise CommandError(
'Specified --main-component was not found in matches!'
)
else:
# Try if one is already there
for match in discovery.matched_components.values():
try:
component = components.get(
repo=repo, filemask=match['mask']
)
except Component.DoesNotExist:
continue
# Pick random
if component is None:
match = list(discovery.matched_components.values())[0]
try:
if component is None:
component = components.get(slug=match['slug'])
self.logger.warning(
'Component %s already exists, skipping and using it '
'as a main component',
match['slug']
)
shutil.rmtree(workdir)
except Component.DoesNotExist:
self.logger.info(
def clean_instance(self, data):
params = copy.copy(data)
if 'discovery' in params:
params.pop('discovery')
instance = Component(**params)
instance.clean_fields(exclude=('filemask',))
instance.validate_unique()
if instance.is_repo_link:
instance.push_on_commit = False
instance.clean_repo()
self.instance = instance
# Create linked repos automatically
if not self.instance.is_repo_link:
same_repo = instance.project.component_set.filter(
repo=instance.repo, vcs=instance.vcs, branch=instance.branch
)
if same_repo.exists():
component = same_repo[0]
data['repo'] = component.get_repo_link_url()
data['branch'] = ''