Passed
Pull Request — master (#132)
by
unknown
03:30
created

create_dt_params_with_mData_shuffled()   A

Complexity

Conditions 4

Size

Total Lines 32
Code Lines 21

Duplication

Lines 32
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 21
dl 32
loc 32
rs 9.376
c 0
b 0
f 0
cc 4
nop 5
1
from random import shuffle
2
3
4 View Code Duplication
def create_dt_params(columns, search="", start=0, length=10, order=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5
    """Create DataTables input parameters when the data source from the rows
6
    data object/ array is not set.
7
8
    Read more about setting column data source here https://datatables.net/reference/option/columns.data"""
9
    params = {
10
        "draw": "1",
11
        "start": str(start),
12
        "length": str(length),
13
        "search[value]": str(search),
14
        "search[regex]": "false",
15
    }
16
17
    for i, item in enumerate(columns):
18
        cols = "columns[%s]" % i
19
        params["%s%s" % (cols, "[data]")] = i
20
        params["%s%s" % (cols, "[name]")] = ""
21
        params["%s%s" % (cols, "[searchable]")] = "true"
22
        params["%s%s" % (cols, "[orderable]")] = "true"
23
        params["%s%s" % (cols, "[search][value]")] = ""
24
        params["%s%s" % (cols, "[search][regex]")] = "false"
25
26
    for i, item in enumerate(order or [{"column": 0, "dir": "asc"}]):
27
        for key, value in item.items():
28
            params["order[%s][%s]" % (i, key)] = str(value)
29
30
    return params
31
32
33
# These methods would only be used when the mData param is defined in the backend
34
35
36 View Code Duplication
def create_dt_params_with_mData(columns, search="", start=0, length=10, order=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
37
    """Create DataTables input parameters when the data source from the rows
38
    data object/ array is set.
39
40
    Read more about setting column data source here https://datatables.net/reference/option/columns.data"""
41
42
    params = {
43
        "draw": "1",
44
        "start": str(start),
45
        "length": str(length),
46
        "search[value]": str(search),
47
        "search[regex]": "false",
48
    }
49
50
    for i, item in enumerate(columns):
51
        cols = "columns[%s]" % i
52
        params["%s%s" % (cols, "[data]")] = item.mData
53
        params["%s%s" % (cols, "[name]")] = ""
54
        params["%s%s" % (cols, "[searchable]")] = "true"
55
        params["%s%s" % (cols, "[orderable]")] = "true"
56
        params["%s%s" % (cols, "[search][value]")] = ""
57
        params["%s%s" % (cols, "[search][regex]")] = "false"
58
59
    for i, item in enumerate(order or [{"column": 0, "dir": "asc"}]):
60
        for key, value in item.items():
61
            params["order[%s][%s]" % (i, key)] = str(value)
62
63
    return params
64
65
66 View Code Duplication
def create_dt_params_with_mData_shuffled(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
67
    columns, search="", start=0, length=10, order=None
68
):
69
    """Create DataTables input parameters when the data source from the rows
70
    data object/ array is set. Also when the order in the frontend is not same
71
    as in the backend.
72
73
    Read more about setting column data source here https://datatables.net/reference/option/columns.data"""
74
75
    params = {
76
        "draw": "1",
77
        "start": str(start),
78
        "length": str(length),
79
        "search[value]": str(search),
80
        "search[regex]": "false",
81
    }
82
    # Shuffle the columns in place
83
    shuffle(columns)
84
    for i, item in enumerate(columns):
85
        cols = "columns[%s]" % i
86
        params["%s%s" % (cols, "[data]")] = item.mData
87
        params["%s%s" % (cols, "[name]")] = ""
88
        params["%s%s" % (cols, "[searchable]")] = "true"
89
        params["%s%s" % (cols, "[orderable]")] = "true"
90
        params["%s%s" % (cols, "[search][value]")] = ""
91
        params["%s%s" % (cols, "[search][regex]")] = "false"
92
93
    for i, item in enumerate(order or [{"column": 0, "dir": "asc"}]):
94
        for key, value in item.items():
95
            params["order[%s][%s]" % (i, key)] = str(value)
96
97
    return params
98
99
100
def create_dt_params_with_mData_with_extra_data(
101
    columns, search="", start=0, length=10, order=None
102
):
103
    """Create DataTables input parameters when the data source from the rows
104
    data object/ array is set. Also when there is an extra data source defined in
105
    the frontend just for the use in the frontend but not in the backend.
106
    An example of this is here https://editor.datatables.net/examples/bubble-editing/simple.html
107
108
    Read more about setting column data source here https://datatables.net/reference/option/columns.data"""
109
110
    params = {
111
        "draw": "1",
112
        "start": str(start),
113
        "length": str(length),
114
        "search[value]": str(search),
115
        "search[regex]": "false",
116
    }
117
    # Add the extra params for the extra data source added in the frontend but
118
    # not in the backend.
119
    params["columns[0][name]"] = ""
120
    params["columns[0][searchable]"] = "true"
121
    params["columns[0][orderable]"] = "false"
122
    params["columns[0][search][value]"] = ""
123
    params["columns[0][search][regex]"] = "false"
124
    for i, item in enumerate(columns, 1):
125
        cols = "columns[%s]" % i
126
        params["%s%s" % (cols, "[data]")] = item.mData
127
        params["%s%s" % (cols, "[name]")] = ""
128
        params["%s%s" % (cols, "[searchable]")] = "true"
129
        params["%s%s" % (cols, "[orderable]")] = "true"
130
        params["%s%s" % (cols, "[search][value]")] = ""
131
        params["%s%s" % (cols, "[search][regex]")] = "false"
132
133
    for i, item in enumerate(order or [{"column": 1, "dir": "asc"}]):
134
        for key, value in item.items():
135
            params["order[%s][%s]" % (i, key)] = str(value)
136
137
    return params
138