weblate/trans/validators.py
2013-10-11 09:53:27 +02:00

122 lines
3.4 KiB
Python
Vendored

# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2013 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <http://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from trans.checks import CHECKS
VALID_FLAGS = (
'rst-text',
'python-format',
'c-format',
'php-format',
'python-brace-format',
)
def validate_repoweb(val):
'''
Validates whether URL for repository browser is valid and
can be filled in using format string.
'''
try:
val % {'file': 'file.po', 'line': '9', 'branch': 'master'}
except Exception as e:
raise ValidationError(_('Bad format string (%s)') % str(e))
def validate_extra_file(val):
'''
Validates extra file to commit.
'''
try:
val % {'language': 'cs'}
except Exception as e:
raise ValidationError(_('Bad format string (%s)') % str(e))
def validate_commit_message(val):
'''
Validates that commit message is a valid format string.
'''
try:
val % {
'language': 'cs',
'language_name': 'Czech',
'project': 'Weblate',
'subproject': 'master',
'total': 200,
'fuzzy': 20,
'fuzzy_percent': 10.0,
'translated': 40,
'translated_percent': 20.0,
}
except Exception as e:
raise ValidationError(_('Bad format string (%s)') % str(e))
def validate_filemask(val):
'''
Validates file mask that it contains *.
'''
if not '*' in val:
raise ValidationError(
_('File mask does not contain * as a language placeholder!')
)
def validate_repo(val):
'''
Validates Git URL, and special weblate:// links.
'''
from trans.models import SubProject
try:
repo = SubProject.objects.get_linked(val)
if repo is not None and repo.is_repo_link():
raise ValidationError(_('Can not link to linked repository!'))
except (SubProject.DoesNotExist, ValueError):
raise ValidationError(
_(
'Invalid link to Weblate project, '
'use weblate://project/subproject.'
)
)
def validate_autoaccept(val):
'''
Validates correct value for autoaccept.
'''
if val == 1:
raise ValidationError(_(
'Value of 1 is not allowed for autoaccept as '
'every user gives vote to his suggestion.'
))
def validate_check_flags(val):
'''
Validates check influencing flags.
'''
for flag in val.split(','):
if flag in VALID_FLAGS:
continue
if flag.startswith('ignore-') and flag[7:] in CHECKS:
continue
raise ValidationError(_('Invalid check flag: "%s"') % flag)