Commit c411ec0b authored by Michal Čihař's avatar Michal Čihař

Move all template validation to single block

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent d3975afa
...@@ -848,26 +848,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -848,26 +848,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
_('Export URL is not used when repository is linked!') _('Export URL is not used when repository is linked!')
) )
def clean_template(self):
'''
Validates whether template can be loaded.
'''
full_path = os.path.join(self.get_path(), self.template)
if not os.path.exists(full_path):
raise ValidationError(_('Template file not found!'))
try:
self.load_template_store()
except ValueError:
raise ValidationError(
_('Format of translation base file could not be recognized.')
)
except Exception as exc:
raise ValidationError(
_('Failed to parse translation base file: %s') % str(exc)
)
def clean_lang_codes(self, matches): def clean_lang_codes(self, matches):
''' '''
Validates that there are no double language codes found in the files. Validates that there are no double language codes found in the files.
...@@ -951,6 +931,44 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -951,6 +931,44 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
'project settings.' 'project settings.'
)) ))
def clean_template(self):
"""
Validates template value.
"""
# Test for unexpected template usage
if self.template != '' and self.file_format_cls.monolingual is False:
raise ValidationError(
_('You can not base file with bilingual translation!')
)
# Special case for Gettext
if self.template.endswith('.pot') and self.filemask.endswith('.po'):
raise ValidationError(
_('You can not base file with bilingual translation!')
)
# Validate template loading
if self.has_template():
full_path = os.path.join(self.get_path(), self.template)
if not os.path.exists(full_path):
raise ValidationError(_('Template file not found!'))
try:
self.load_template_store()
except ValueError:
raise ValidationError(
_('Format of translation base file could not be recognized.')
)
except Exception as exc:
raise ValidationError(
_('Failed to parse translation base file: %s') % str(exc)
)
elif self.file_format_cls.monolingual:
raise ValidationError(
_('You can not use monolingual translation without base file!')
)
def clean(self): def clean(self):
''' '''
Validator fetches repository and tries to find translation files. Validator fetches repository and tries to find translation files.
...@@ -983,25 +1001,8 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -983,25 +1001,8 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
# Try parsing files # Try parsing files
self.clean_files(matches) self.clean_files(matches)
# Test for unexpected template usage # Template validation
if self.template != '' and self.file_format_cls.monolingual is False:
raise ValidationError(
_('You can not base file with bilingual translation!')
)
# Special case for Gettext
if self.template.endswith('.pot') and self.filemask.endswith('.po'):
raise ValidationError(
_('You can not base file with bilingual translation!')
)
# Validate template
if self.has_template():
self.clean_template() self.clean_template()
elif self.file_format_cls.monolingual:
raise ValidationError(
_('You can not use monolingual translation without base file!')
)
# New language options # New language options
self.clean_new_lang() self.clean_new_lang()
......
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