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

tcms.core.utils   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 20
eloc 45
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A form_errors_to_list() 0 9 2
B clean_request() 0 20 7
A request_host_link() 0 14 5
B string_to_list() 0 14 6
1
# -*- coding: utf-8 -*-
2
#  pylint: disable=too-few-public-methods
3
4
import sys
5
6
7
def string_to_list(strs, spliter=','):
8
    """Convert the string to list"""
9
    if isinstance(strs, list):
10
        str_list = (str(item).strip() for item in strs)
0 ignored issues
show
Comprehensibility Best Practice introduced by Mr. Senko
The variable item does not seem to be defined.
Loading history...
11
    elif isinstance(strs, str) and strs.find(spliter):
12
        str_list = (str(item).strip() for item in strs.split(spliter))
13
    else:
14
        str_list = (strs,)
15
16
    lst = []
17
    for string in str_list:
18
        if string:
19
            lst.append(string)
20
    return lst
21
22
23
def form_errors_to_list(form):
24
    """
25
    Convert errors of form to list
26
    Use for Ajax.Request response
27
    """
28
    errors = []
29
    for key, value in form.errors.items():
30
        errors.append((key, value[0]))
31
    return errors
32
33
34
def request_host_link(request, domain_name=None):
35
    protocol = 'https://'
36
    if 'runserver' in sys.argv:
37
        protocol = 'http://'
38
39
    if request:
40
        if not domain_name:
41
            domain_name = request.get_host()
42
        # default to https if in production and we don't know
43
        protocol = 'https://'
44
        if not request.is_secure():
45
            protocol = 'http://'
46
47
    return protocol + domain_name
48
49
50
# todo: remove this
51
def clean_request(request, keys=None):
52
    """
53
    Clean the request strings
54
    """
55
    request_contents = request.GET.copy()
56
    if not keys:
57
        keys = request_contents.keys()
58
    cleaned_request = {}
59
    for key in keys:
60
        key = str(key)
61
        if request_contents.get(key):
62
            if key in ('order_by', 'from_plan'):
63
                continue
64
65
            value = request.GET[key]
66
            # Convert the value to be list if it's __in filter.
67
            if key.endswith('__in') and isinstance(value, str):
68
                value = string_to_list(value)
69
            cleaned_request[key] = value
70
    return cleaned_request
71