-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
220 lines (172 loc) · 7.86 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from datetime import date
class SimpleFilter:
pass
class IdFilter(SimpleFilter):
def __init__(self, value, condition='equals'):
if condition not in ['equals', 'notequals', 'greater',
'greaterorequals', 'less', 'lessorequals']:
raise AttributeError('Атрибут condition неверный')
elif type(value) is int:
self.json = {'value': value,
'type': 'Id',
'condition': condition}
else:
raise AttributeError('Атрибут value должен быть типа int')
def __call__(self):
return self.json
class CalendarFilter(SimpleFilter):
def __init__(self, value, condition='equals'):
if condition not in ['equals', 'notequals', 'greater',
'greaterorequals', 'less', 'lessorequals']:
raise AttributeError('Атрибут condition неверный')
elif type(value) is date:
self.json = {'value': value.strftime('%Y-%m-%d'),
'type': 'calendar',
'condition': condition}
else:
raise AttributeError('Атрибут value должен быть объектом datetime.date')
def __call__(self):
return self.json
class FormInstanceFilter(SimpleFilter):
def __init__(self, value, condition='equals'):
if condition not in ['equals', 'notequals', 'greater',
'greaterorequals', 'less', 'lessorequals']:
raise AttributeError('Атрибут condition неверный')
elif type(value) is str:
self.json = {'value': value,
'type': 'formInstance',
'condition': condition}
else:
raise AttributeError('Атрибут value должен быть типа str')
def __call__(self):
return self.json
class DimensionIdFilter(SimpleFilter):
def __init__(self, value, name, condition='equals'):
if condition not in ['equals', 'notequals', 'greater',
'greaterorequals', 'less', 'lessorequals']:
raise AttributeError('Атрибут condition неверный')
elif type(value) is int and type(name) is str:
self.json = {'value': value,
'type': 'DimensionId',
'name': name,
'condition': condition}
else:
raise AttributeError('Атрибуты value и name должны быть типа int и str соответственно')
def __call__(self):
return self.json
class DimensionNameFilter(SimpleFilter):
def __init__(self, value, name, condition='equals'):
if condition not in ['equals', 'notequals', 'greater',
'greaterorequals', 'less', 'lessorequals']:
raise AttributeError('Атрибут condition неверный')
elif type(value) is int and type(name) is str:
self.json = {'value': value,
'type': 'DimensionName',
'name': name,
'condition': condition}
else:
raise AttributeError('Атрибуты value и name должны быть типа int и str соответственно')
def __call__(self):
return self.json
class MeasureIdFilter(SimpleFilter):
def __init__(self, value, name, condition='equals'):
if condition not in ['equals', 'notequals', 'greater',
'greaterorequals', 'less', 'lessorequals']:
raise AttributeError('Атрибут condition неверный')
elif type(value) is int and type(name) is str:
self.json = {'value': value,
'type': 'MeasureId',
'name': name,
'condition': condition}
else:
raise AttributeError('Атрибуты value и name должны быть типа int и str соответственно')
def __call__(self):
return self.json
class MeasureNameFilter(SimpleFilter):
def __init__(self, value, name, condition='equals'):
if condition not in ['equals', 'notequals', 'greater',
'greaterorequals', 'less', 'lessorequals']:
raise AttributeError('Атрибут condition неверный')
elif type(value) is str and type(name) is str:
self.json = {'value': value,
'type': 'MeasureName',
'name': name,
'condition': condition}
else:
raise AttributeError('Атрибуты value и name должны быть типа str')
def __call__(self):
return self.json
class AttributeFilter(SimpleFilter):
def __init__(self, value, name, condition='equals'):
if condition not in ['equals', 'notequals']:
raise AttributeError('Атрибут condition неверный')
self.json = {'value': value,
'name': name,
'type': 'attribute',
'condition': condition}
def __call__(self):
return self.json
class DictFilter(SimpleFilter):
def __init__(self, value):
self.json = value
def __call__(self):
return self.json
class ComplexFilter:
def __init__(self, operation='and'):
if operation not in ['or', 'and']:
AttributeError('Operation должен быть "or" или "and"')
self.json = {'operation': operation, 'filters': []}
def __call__(self):
return self.json
def __add__(self, other):
self.add(other)
return self
def add(self, other):
def add_element(element):
if issubclass(type(element), SimpleFilter):
self.json['filters'].append(element())
else:
raise AttributeError('Элемент не является наследником класса SimpleFilter')
if type(other) is list or type(other) is tuple:
for i in other:
add_element(i)
else:
add_element(other)
def __sub__(self, other):
self.remove(other)
return self
def remove(self, element):
if type(element) is list or type(element) is tuple:
for i in element:
self.json['filters'].remove(i())
else:
self.json['filters'].remove(element())
def set_operation(self, operation):
if operation == 'or' or operation == 'and':
self.json['operation'] = operation
else:
AttributeError('Operation должен быть "or" или "and"')
def extend(self, complex_filter):
if type(complex_filter) is not ComplexFilter:
raise AttributeError('complex_filter должен быть экземпляром класса ComplexFilter')
else:
self.json['filters'].extend(complex_filter()['filters'])
def clear(self):
self.json['filters'] = []
'''
# Пример использование комплексного фильтра
comfil = ComplexFilter('or')
id_fil = IdFilter(1234, condition='notequals')
cal_fil = CalendarFilter(date(2020, 11, 28))
comfil = comfil + [id_fil, id_fil, cal_fil, cal_fil]
comfil += [FormInstanceFilter('form13213instance'), DimensionIdFilter(1, 'DimExample')]
comfil.add(DimensionNameFilter('DimNAME', 'DimExample'))
comfil.add([MeasureIdFilter(13, 'MeaExamp'), MeasureNameFilter('MeaName', 'MeaExamp')])
print(comfil())
comfil.set_operation('and')
comfil.remove([id_fil, IdFilter(1234, condition='notequals')])
comfil -= cal_fil
print(comfil())
'''
# equals, notequals, greater, greaterorequals, less, lessorequals, contains
# = != > >= < <= in