Passed
Push — master ( cddcf6...1d3855 )
by Alexander
01:59
created

tcms/core/utils/validations.py (1 issue)

1
# -*- coding: utf-8 -*-
2
3
import re
4
5
from django.forms import ValidationError
6
7
from tcms.testcases.models import BugSystem
8
9
10
__all__ = (
11
    'validate_bug_id',
12
)
13
14
15
# todo: scheduled for removal together with validation regexes
16
# and validateIssueID on the JavaScript side
17
def validate_bug_id(bug_id, bug_system_id):
18
    if not isinstance(bug_id, (str, list, tuple)):
19
        raise ValidationError('Type error of bug_id.')
20
21
    if not bug_system_id:
22
        raise ValidationError('bug_system_id must not be empty.')
23
24
    bug_ids = bug_id
25
    if not isinstance(bug_ids, (list, tuple)):
26
        bug_ids = (bug_ids,)
27
28
    try:
29
        bug_system = BugSystem.get_by_id(bug_system_id)
30
    except BugSystem.DoesNotExist:
31
        raise ValidationError("Bug system with PK %s doesn't exit" % bug_system_id)
32
    else:
33
        id_pattern = bug_system.validate_reg_exp
34
        if not id_pattern:
35
            return
36
        reg_exp = re.compile(id_pattern)
37
38
        def error_if_not_match(bug_id):
39
            if not reg_exp.match(bug_id):
0 ignored issues
show
The variable reg_exp does not seem to be defined for all execution paths.
Loading history...
40
                raise ValidationError(
41
                    'Please input a valid %s id. %s' % (
42
                        bug_system.name, bug_system.description))
43
44
        map(error_if_not_match, bug_ids)
45