-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanager_mixins.py
More file actions
266 lines (199 loc) · 8.13 KB
/
manager_mixins.py
File metadata and controls
266 lines (199 loc) · 8.13 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# -*- coding: utf-8 -*-
from six import wraps
from syncano.exceptions import SyncanoValueError
def clone(func):
"""Decorator which will ensure that we are working on copy of ``self``.
"""
@wraps(func)
def inner(self, *args, **kwargs):
self = self._clone()
return func(self, *args, **kwargs)
return inner
class IncrementMixin(object):
@clone
def increment(self, field_name, value, **kwargs):
"""
A manager method which increments given field with given value.
Usage::
data_object = Object.please.increment(
field_name='argA',
value=10,
class_name='testclass',
id=1715
)
:param field_name: the field name to increment;
:param value: the increment value;
:param kwargs: class_name and id usually;
:return: the processed (incremented) data object;
"""
self.properties.update(kwargs)
model = self.model.get_subclass_model(**self.properties)
self.validate(field_name, value, model)
return self.process(field_name, value, **kwargs)
@clone
def decrement(self, field_name, value, **kwargs):
"""
A manager method which decrements given field with given value.
Usage::
data_object = Object.please.decrement(
field_name='argA',
value=10,
class_name='testclass',
id=1715
)
:param field_name: the field name to decrement;
:param value: the decrement value;
:param kwargs: class_name and id usually;
:return: the processed (incremented) data object;
"""
self.properties.update(kwargs)
model = self.model.get_subclass_model(**self.properties)
self.validate(field_name, value, model, operation_type='decrement')
return self.process(field_name, value, operation_type='decrement', **kwargs)
def process(self, field_name, value, operation_type='increment', **kwargs):
self.endpoint = 'detail'
self.method = self.get_allowed_method('PATCH', 'PUT', 'POST')
self.data = kwargs.copy()
if operation_type == 'increment':
increment_data = {'_increment': value}
elif operation_type == 'decrement':
increment_data = {'_increment': -value}
else:
raise SyncanoValueError('Operation not supported')
self.data.update(
{field_name: increment_data}
)
response = self.request()
return response
@classmethod
def validate(cls, field_name, value, model, operation_type='increment'):
if not isinstance(value, (int, float)):
raise SyncanoValueError('Provide an integer or float as a {} value.'.format(operation_type))
if not value >= 0:
raise SyncanoValueError('Value should be positive.')
if not cls._check_field_type_for_increment(model, field_name):
raise SyncanoValueError('{} works only on integer and float fields.'.format(operation_type.capitalize()))
@classmethod
def _check_field_type_for_increment(cls, model, field_name):
fields = {}
for field in model._meta.fields:
fields[field.name] = field.allow_increment
if field_name not in fields:
raise SyncanoValueError('Object has not specified field.')
if fields[field_name]:
return True
return False
class ArrayOperationsMixin(object):
@clone
def add(self, field_name, value, **kwargs):
"""
A manager method that will add a values to the array field.
Usage::
data_object = Object.please.add(
field_name='array',
value=[10],
class_name='arr_test',
id=155
)
Consider example:
data_object.array = [1]
after running::
data_object = Object.please.add(
field_name='array',
value=[3],
id=data_object.id,
)
data_object.array will be equal: [1, 3]
and after::
data_object = Object.please.add(
field_name='array',
value=[1],
id=data_object.id,
)
data_object.array will be equal: [1, 3, 1]
:param field_name: the array field name to which elements will be added;
:param value: the list of values to add;
:param kwargs: class_name and id usually;
:return: the processed data object;
"""
self.properties.update(kwargs)
model = self.model.get_subclass_model(**self.properties)
self.array_validate(field_name, value, model)
return self.array_process(field_name, value, operation_type='add')
def remove(self, field_name, value, **kwargs):
"""
A manager method that will remove a values from the array field.
Usage::
data_object = Object.please.remove(
field_name='array',
value=[10],
class_name='arr_test',
id=155
)
:param field_name: the array field name from which elements will be removed;
:param value: the list of values to remove;
:param kwargs: class_name and id usually;
:return: the processed data object;
"""
self.properties.update(kwargs)
model = self.model.get_subclass_model(**self.properties)
self.array_validate(field_name, value, model)
return self.array_process(field_name, value, operation_type='remove')
def add_unique(self, field_name, value, **kwargs):
"""
A manager method that will add an unique values to the array field.
Usage::
data_object = Object.please.add_unique(
field_name='array',
value=[10],
class_name='arr_test',
id=155
)
The main distinction between add and add unique is that: add unique will not repeat elements.
Consider example::
data_object.array = [1]
after running::
data_object = Object.please.add_unique(
field_name='array',
value=[1],
id=data_object.id,
)
data_object.array will be equal: [1]
But if only add will be run the result will be as follow:
data_object.array will be equal: [1, 1]
:param field_name: field_name: the array field name to which elements will be added unique;
:param value: the list of values to add unique;
:param kwargs: class_name and id usually;
:return: the processed data object;
"""
self.properties.update(kwargs)
model = self.model.get_subclass_model(**self.properties)
self.array_validate(field_name, value, model)
return self.array_process(field_name, value, operation_type='add_unique')
@classmethod
def array_validate(cls, field_name, value, model):
fields = {field.name: field for field in model._meta.fields}
if field_name not in fields:
raise SyncanoValueError('Object has not specified field.')
from syncano.models import ArrayField
if not isinstance(fields[field_name], ArrayField):
raise SyncanoValueError('Field must be of array type')
if not isinstance(value, list):
raise SyncanoValueError('List of values expected')
def array_process(self, field_name, value, operation_type, **kwargs):
self.endpoint = 'detail'
self.method = self.get_allowed_method('PATCH', 'PUT', 'POST')
self.data = kwargs.copy()
if operation_type == 'add':
array_data = {'_add': value}
elif operation_type == 'remove':
array_data = {'_remove': value}
elif operation_type == 'add_unique':
array_data = {'_addunique': value}
else:
raise SyncanoValueError('Operation not supported')
self.data.update(
{field_name: array_data}
)
response = self.request()
return response