channel-app 0.0.157a10__py3-none-any.whl → 0.0.157a11__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- channel_app/app/order/service.py +598 -364
- channel_app/app/product/service.py +392 -238
- channel_app/app/product_image/service.py +214 -134
- channel_app/app/product_price/service.py +390 -240
- channel_app/app/product_stock/service.py +285 -203
- channel_app/app/setup/service.py +133 -79
- {channel_app-0.0.157a10.dist-info → channel_app-0.0.157a11.dist-info}/METADATA +1 -1
- {channel_app-0.0.157a10.dist-info → channel_app-0.0.157a11.dist-info}/RECORD +10 -10
- {channel_app-0.0.157a10.dist-info → channel_app-0.0.157a11.dist-info}/WHEEL +0 -0
- {channel_app-0.0.157a10.dist-info → channel_app-0.0.157a11.dist-info}/top_level.txt +0 -0
channel_app/app/order/service.py
CHANGED
@@ -4,31 +4,44 @@ from typing import List, Generator, Union
|
|
4
4
|
|
5
5
|
from requests import exceptions as requests_exceptions
|
6
6
|
|
7
|
-
from omnisdk.omnitron.models import (
|
8
|
-
|
7
|
+
from omnisdk.omnitron.models import (
|
8
|
+
Customer,
|
9
|
+
Address,
|
10
|
+
CargoCompany,
|
11
|
+
Order,
|
12
|
+
BatchRequest,
|
13
|
+
CancellationRequest,
|
14
|
+
)
|
9
15
|
from omnisdk.omnitron.endpoints import ChannelIntegrationActionEndpoint
|
10
16
|
|
11
17
|
from channel_app.core import settings
|
12
|
-
from channel_app.core.data import (
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
from channel_app.core.data import (
|
19
|
+
BatchRequestResponseDto,
|
20
|
+
CancellationRequestDto,
|
21
|
+
ChannelCancellationRequestDto,
|
22
|
+
OmnitronCreateOrderDto,
|
23
|
+
OmnitronOrderDto,
|
24
|
+
ChannelCreateOrderDto,
|
25
|
+
ErrorReportDto,
|
26
|
+
OrderBatchRequestResponseDto,
|
27
|
+
CancelOrderDto,
|
28
|
+
ChannelUpdateOrderItemDto,
|
29
|
+
)
|
22
30
|
from channel_app.core.settings import OmnitronIntegration, ChannelIntegration
|
23
31
|
from channel_app.logs.services import LogService
|
24
32
|
from channel_app.omnitron.batch_request import ClientBatchRequest
|
25
|
-
from channel_app.omnitron.constants import (
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
from channel_app.omnitron.constants import (
|
34
|
+
BatchRequestStatus,
|
35
|
+
ContentType,
|
36
|
+
FailedReasonType,
|
37
|
+
)
|
38
|
+
from channel_app.omnitron.exceptions import (
|
39
|
+
CityException,
|
40
|
+
TownshipException,
|
41
|
+
DistrictException,
|
42
|
+
CargoCompanyException,
|
43
|
+
OrderException,
|
44
|
+
)
|
32
45
|
|
33
46
|
|
34
47
|
class OrderService(object):
|
@@ -45,13 +58,13 @@ class OrderService(object):
|
|
45
58
|
try:
|
46
59
|
with log_service.step("fetch_orders"):
|
47
60
|
with OmnitronIntegration(
|
48
|
-
|
49
|
-
|
61
|
+
content_type=ContentType.order.value
|
62
|
+
) as omnitron_integration:
|
50
63
|
|
51
64
|
with log_service.step("get_orders"):
|
52
65
|
get_orders = ChannelIntegration().do_action(
|
53
|
-
key=
|
54
|
-
batch_request=omnitron_integration.batch_request
|
66
|
+
key="get_orders",
|
67
|
+
batch_request=omnitron_integration.batch_request,
|
55
68
|
)
|
56
69
|
|
57
70
|
get_orders: Generator
|
@@ -64,42 +77,44 @@ class OrderService(object):
|
|
64
77
|
|
65
78
|
# tips
|
66
79
|
channel_create_order: ChannelCreateOrderDto
|
67
|
-
metadata = {
|
68
|
-
"order_number": channel_create_order.order.number
|
69
|
-
}
|
80
|
+
metadata = {"order_number": channel_create_order.order.number}
|
70
81
|
|
71
82
|
report_list: List[ErrorReportDto]
|
72
83
|
for report in report_list:
|
73
84
|
if is_success_log or not report.is_ok:
|
74
|
-
report.error_code =
|
75
|
-
f"{omnitron_integration.batch_request.local_batch_id}"
|
85
|
+
report.error_code = (
|
86
|
+
f"{omnitron_integration.batch_request.local_batch_id}"
|
76
87
|
f"-Channel-GetOrders_{channel_create_order.order.number}"
|
88
|
+
)
|
77
89
|
try:
|
78
|
-
|
79
|
-
with log_service.step(
|
90
|
+
|
91
|
+
with log_service.step(
|
92
|
+
"create_error_report", metadata=metadata
|
93
|
+
):
|
80
94
|
omnitron_integration.do_action(
|
81
|
-
key=
|
82
|
-
objects=report
|
95
|
+
key="create_error_report", objects=report
|
83
96
|
)
|
84
97
|
except Exception as err:
|
85
98
|
log_service.add_exception(err)
|
86
99
|
raise
|
87
|
-
|
100
|
+
|
88
101
|
try:
|
89
102
|
with log_service.step("create_order", metadata=metadata):
|
90
103
|
order = self.create_order(
|
91
104
|
omnitron_integration=omnitron_integration,
|
92
|
-
channel_order=channel_create_order
|
105
|
+
channel_order=channel_create_order,
|
93
106
|
)
|
94
107
|
except Exception as err:
|
95
108
|
log_service.add_exception(err)
|
96
109
|
raise
|
97
110
|
|
98
111
|
if order and omnitron_integration.batch_request.objects:
|
99
|
-
order_batch_objects.extend(
|
112
|
+
order_batch_objects.extend(
|
113
|
+
omnitron_integration.batch_request.objects
|
114
|
+
)
|
100
115
|
|
101
116
|
omnitron_integration.batch_request.objects = order_batch_objects
|
102
|
-
|
117
|
+
|
103
118
|
with log_service.step("batch_to_done"):
|
104
119
|
try:
|
105
120
|
self.batch_service(settings.OMNITRON_CHANNEL_ID).to_done(
|
@@ -107,7 +122,10 @@ class OrderService(object):
|
|
107
122
|
)
|
108
123
|
except requests_exceptions.HTTPError as exc:
|
109
124
|
log_service.add_exception(exc)
|
110
|
-
if
|
125
|
+
if (
|
126
|
+
exc.response.status_code == 406
|
127
|
+
and "batch_request_status_100_1" in exc.response.text
|
128
|
+
):
|
111
129
|
pass
|
112
130
|
else:
|
113
131
|
raise exc
|
@@ -117,227 +135,354 @@ class OrderService(object):
|
|
117
135
|
finally:
|
118
136
|
log_service.save()
|
119
137
|
|
120
|
-
def create_order(
|
121
|
-
|
122
|
-
|
123
|
-
|
138
|
+
def create_order(
|
139
|
+
self,
|
140
|
+
omnitron_integration: OmnitronIntegration,
|
141
|
+
channel_order: ChannelCreateOrderDto,
|
142
|
+
) -> Union[Order, None]:
|
143
|
+
log_service = LogService()
|
144
|
+
tx_id = uuid.uuid4()
|
145
|
+
log_service.create_flow(name="Create Order", transaction_id=tx_id)
|
124
146
|
|
125
147
|
try:
|
126
|
-
|
127
|
-
key='get_or_create_customer',
|
128
|
-
objects=order.customer)[0]
|
129
|
-
except Exception:
|
130
|
-
return
|
148
|
+
order = channel_order.order
|
131
149
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
150
|
+
with log_service.step("get_or_create_customer"):
|
151
|
+
try:
|
152
|
+
customer = omnitron_integration.do_action(
|
153
|
+
key="get_or_create_customer", objects=order.customer
|
154
|
+
)[0]
|
155
|
+
except Exception as exc:
|
156
|
+
log_service.add_exception(exc)
|
157
|
+
return
|
158
|
+
|
159
|
+
customer: Customer
|
160
|
+
with log_service.step("combine_addresses"):
|
161
|
+
try:
|
162
|
+
shipping_address_data = {
|
163
|
+
"customer": customer,
|
164
|
+
"address": order.shipping_address,
|
165
|
+
}
|
166
|
+
|
167
|
+
with log_service.step("get_or_create_address (shipping)"):
|
168
|
+
shipping_address = omnitron_integration.do_action(
|
169
|
+
key="get_or_create_address", objects=shipping_address_data
|
170
|
+
)[0]
|
171
|
+
|
172
|
+
billing_address_data = {
|
173
|
+
"customer": customer,
|
174
|
+
"address": order.billing_address,
|
175
|
+
}
|
176
|
+
|
177
|
+
with log_service.step("get_or_create_address (billing)"):
|
178
|
+
billing_address = omnitron_integration.do_action(
|
179
|
+
key="get_or_create_address", objects=billing_address_data
|
180
|
+
)[0]
|
181
|
+
except (CityException, TownshipException, DistrictException) as exc:
|
182
|
+
log_service.add_exception(exc)
|
183
|
+
|
184
|
+
with log_service.step("create_address_error_report"):
|
185
|
+
omnitron_integration.do_action(
|
186
|
+
key="create_address_error_report", object=exc
|
187
|
+
)
|
188
|
+
return
|
156
189
|
|
157
|
-
|
158
|
-
|
190
|
+
except IndexError as exc:
|
191
|
+
log_service.add_exception(exc)
|
192
|
+
return
|
159
193
|
|
160
|
-
|
161
|
-
|
162
|
-
try:
|
163
|
-
cargo_company = omnitron_integration.do_action(
|
164
|
-
key='get_cargo_company',
|
165
|
-
objects=order.cargo_company
|
166
|
-
)[0]
|
167
|
-
except (CargoCompanyException, IndexError):
|
168
|
-
return
|
169
|
-
cargo_company: CargoCompany
|
170
|
-
|
171
|
-
order_data = asdict(order)
|
172
|
-
order_data["customer"] = customer.pk
|
173
|
-
order_data["shipping_address"] = shipping_address.pk
|
174
|
-
order_data["billing_address"] = billing_address.pk
|
175
|
-
order_data["cargo_company"] = cargo_company.pk
|
176
|
-
omnitron_order = OmnitronOrderDto(**order_data)
|
177
|
-
create_order_dto = OmnitronCreateOrderDto(
|
178
|
-
order=omnitron_order,
|
179
|
-
order_item=channel_order.order_item)
|
180
|
-
try:
|
181
|
-
orders: List[Order] = omnitron_integration.do_action(
|
182
|
-
key='create_order',
|
183
|
-
objects=create_order_dto
|
184
|
-
)
|
185
|
-
order = orders[0]
|
186
|
-
except (OrderException, IndexError):
|
187
|
-
return
|
194
|
+
shipping_address: Address
|
195
|
+
billing_address: Address
|
188
196
|
|
189
|
-
|
197
|
+
with log_service.step("get_cargo_company"):
|
198
|
+
try:
|
199
|
+
cargo_company = omnitron_integration.do_action(
|
200
|
+
key="get_cargo_company", objects=order.cargo_company
|
201
|
+
)[0]
|
202
|
+
except (CargoCompanyException, IndexError) as exc:
|
203
|
+
log_service.add_exception(exc)
|
204
|
+
return
|
205
|
+
|
206
|
+
cargo_company: CargoCompany
|
207
|
+
|
208
|
+
order_data = asdict(order)
|
209
|
+
order_data["customer"] = customer.pk
|
210
|
+
order_data["shipping_address"] = shipping_address.pk
|
211
|
+
order_data["billing_address"] = billing_address.pk
|
212
|
+
order_data["cargo_company"] = cargo_company.pk
|
213
|
+
|
214
|
+
with log_service.step("create_order_dto"):
|
215
|
+
omnitron_order = OmnitronOrderDto(**order_data)
|
216
|
+
|
217
|
+
with log_service.step("create_omnitron_order_dto"):
|
218
|
+
create_order_dto = OmnitronCreateOrderDto(
|
219
|
+
order=omnitron_order, order_item=channel_order.order_item
|
220
|
+
)
|
190
221
|
|
191
|
-
|
192
|
-
with OmnitronIntegration(
|
193
|
-
content_type=ContentType.order.value) as omnitron_integration:
|
194
|
-
get_updated_orders = ChannelIntegration().do_action(
|
195
|
-
key='get_updated_order_items',
|
196
|
-
batch_request=omnitron_integration.batch_request
|
197
|
-
)
|
198
|
-
get_updated_orders: Generator
|
199
|
-
order_batch_objects = []
|
200
|
-
while True:
|
222
|
+
with log_service.step("create_order"):
|
201
223
|
try:
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
objects=report)
|
224
|
+
orders: List[Order] = omnitron_integration.do_action(
|
225
|
+
key="create_order", objects=create_order_dto
|
226
|
+
)
|
227
|
+
order = orders[0]
|
228
|
+
except (OrderException, IndexError) as exc:
|
229
|
+
log_service.add_exception(exc)
|
230
|
+
return
|
231
|
+
|
232
|
+
return order
|
233
|
+
except Exception as exc:
|
234
|
+
log_service.add_exception(exc)
|
235
|
+
raise
|
236
|
+
finally:
|
237
|
+
log_service.save()
|
217
238
|
|
218
|
-
|
219
|
-
|
239
|
+
def fetch_and_update_order_items(self, is_success_log=True):
|
240
|
+
log_service = LogService()
|
241
|
+
tx_id = uuid.uuid4()
|
242
|
+
log_service.create_flow(
|
243
|
+
name="Fetch and Update Order Items", transaction_id=tx_id
|
244
|
+
)
|
220
245
|
|
221
|
-
|
246
|
+
try:
|
247
|
+
with log_service.step("fetch_and_update_order_items"):
|
248
|
+
with OmnitronIntegration(
|
249
|
+
content_type=ContentType.order.value
|
250
|
+
) as omnitron_integration:
|
222
251
|
|
223
|
-
|
224
|
-
|
225
|
-
|
252
|
+
with log_service.step("get_updated_order_items"):
|
253
|
+
get_updated_orders = ChannelIntegration().do_action(
|
254
|
+
key="get_updated_order_items",
|
255
|
+
batch_request=omnitron_integration.batch_request,
|
256
|
+
)
|
257
|
+
get_updated_orders: Generator
|
258
|
+
order_batch_objects = []
|
259
|
+
while True:
|
260
|
+
try:
|
261
|
+
channel_update_order, report_list, _ = next(
|
262
|
+
get_updated_orders
|
263
|
+
)
|
264
|
+
except StopIteration:
|
265
|
+
break
|
226
266
|
|
267
|
+
# tips
|
268
|
+
channel_update_order: ChannelUpdateOrderItemDto
|
269
|
+
report_list: List[ErrorReportDto]
|
270
|
+
for report in report_list:
|
271
|
+
if report and (is_success_log or not report.is_ok):
|
272
|
+
report.error_code = (
|
273
|
+
f"{omnitron_integration.batch_request.local_batch_id}"
|
274
|
+
f"_GetUpdatedOrders_{channel_update_order.remote_id}"
|
275
|
+
)
|
227
276
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
omnitron_integration.batch_request.objects =
|
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
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
277
|
+
with log_service.step(
|
278
|
+
"create_error_report",
|
279
|
+
metadata=channel_update_order.order_number,
|
280
|
+
):
|
281
|
+
omnitron_integration.do_action(
|
282
|
+
key="create_error_report", objects=report
|
283
|
+
)
|
284
|
+
|
285
|
+
with log_service.step("update_order_items"):
|
286
|
+
omnitron_integration.do_action(
|
287
|
+
key="update_order_items", objects=channel_update_order
|
288
|
+
)
|
289
|
+
|
290
|
+
omnitron_integration.batch_request.objects = order_batch_objects
|
291
|
+
|
292
|
+
with log_service.step("batch_to_done"):
|
293
|
+
self.batch_service(settings.OMNITRON_CHANNEL_ID).to_done(
|
294
|
+
batch_request=omnitron_integration.batch_request
|
295
|
+
)
|
296
|
+
except Exception as fatal:
|
297
|
+
log_service.add_exception(fatal)
|
298
|
+
raise
|
299
|
+
finally:
|
300
|
+
log_service.save()
|
301
|
+
|
302
|
+
def update_orders(self, is_sync=True, is_success_log=True, add_order_items=False):
|
303
|
+
log_service = LogService()
|
304
|
+
tx_id = uuid.uuid4()
|
305
|
+
log_service.create_flow(name="Update Orders", transaction_id=tx_id)
|
306
|
+
|
307
|
+
try:
|
308
|
+
with log_service.step("update_orders"):
|
309
|
+
with OmnitronIntegration(
|
310
|
+
content_type=ContentType.order.value
|
311
|
+
) as omnitron_integration:
|
312
|
+
|
313
|
+
with log_service.step("get_orders"):
|
314
|
+
orders = omnitron_integration.do_action(key="get_orders")
|
315
|
+
|
316
|
+
orders: List[Order]
|
317
|
+
first_order_count = len(orders)
|
318
|
+
if add_order_items:
|
319
|
+
with log_service.step("get_order_items_with_order"):
|
320
|
+
orders = orders and omnitron_integration.do_action(
|
321
|
+
key="get_order_items_with_order", objects=orders
|
322
|
+
)
|
323
|
+
|
324
|
+
if not orders:
|
325
|
+
if first_order_count:
|
326
|
+
omnitron_integration.batch_request.objects = None
|
327
|
+
|
328
|
+
with log_service.step("batch_to_fail"):
|
329
|
+
self.batch_service(
|
330
|
+
omnitron_integration.channel_id
|
331
|
+
).to_fail(omnitron_integration.batch_request)
|
332
|
+
return
|
333
|
+
|
334
|
+
with log_service.step("send_updated_orders"):
|
335
|
+
response_data, reports, data = ChannelIntegration().do_action(
|
336
|
+
key="send_updated_orders",
|
337
|
+
objects=orders,
|
338
|
+
batch_request=omnitron_integration.batch_request,
|
339
|
+
is_sync=True,
|
340
|
+
)
|
341
|
+
|
342
|
+
# tips
|
343
|
+
response_data: List[OrderBatchRequestResponseDto]
|
344
|
+
reports: List[ErrorReportDto]
|
345
|
+
data: List[Order]
|
346
|
+
|
347
|
+
if not is_sync:
|
348
|
+
if reports[0].is_ok:
|
349
|
+
with log_service.step("batch_send_to_remote"):
|
350
|
+
self.batch_service(
|
351
|
+
settings.OMNITRON_CHANNEL_ID
|
352
|
+
).to_sent_to_remote(
|
353
|
+
batch_request=omnitron_integration.batch_request
|
354
|
+
)
|
355
|
+
else:
|
356
|
+
is_sync = True
|
357
|
+
|
358
|
+
if reports and (is_success_log or not reports[0].is_ok):
|
359
|
+
for report in reports:
|
360
|
+
with log_service.step("create_error_report"):
|
361
|
+
omnitron_integration.do_action(
|
362
|
+
key="create_error_report", objects=report
|
363
|
+
)
|
364
|
+
|
365
|
+
if is_sync:
|
366
|
+
with log_service.step("process_order_batch_requests"):
|
367
|
+
omnitron_integration.do_action(
|
368
|
+
key="process_order_batch_requests",
|
369
|
+
objects=response_data,
|
370
|
+
)
|
371
|
+
except Exception as e:
|
372
|
+
log_service.add_exception(e)
|
373
|
+
raise
|
374
|
+
finally:
|
375
|
+
log_service.save()
|
276
376
|
|
277
377
|
def get_order_batch_requests(self, is_success_log=False):
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
378
|
+
log_service = LogService()
|
379
|
+
tx_id = uuid.uuid4()
|
380
|
+
log_service.create_flow(name="Get Order Batch Requests", transaction_id=tx_id)
|
381
|
+
|
382
|
+
try:
|
383
|
+
with log_service.step("get_order_batch_requests"):
|
384
|
+
with OmnitronIntegration(create_batch=False) as omnitron_integration:
|
385
|
+
with log_service.step("get_batch_requests"):
|
386
|
+
batch_request_data = omnitron_integration.do_action(
|
387
|
+
"get_batch_requests",
|
388
|
+
params={
|
389
|
+
"status": ["sent_to_remote", "ongoing"],
|
390
|
+
"content_type": ContentType.order.value,
|
391
|
+
},
|
392
|
+
)
|
393
|
+
|
394
|
+
# tips
|
395
|
+
batch_request_data: List[BatchRequest]
|
396
|
+
|
397
|
+
for batch_request in batch_request_data:
|
398
|
+
with log_service.step("check_orders"):
|
399
|
+
(
|
400
|
+
response_data,
|
401
|
+
report,
|
402
|
+
data,
|
403
|
+
) = ChannelIntegration().do_action(
|
404
|
+
key="check_orders", objects=batch_request
|
405
|
+
)
|
406
|
+
|
407
|
+
# tips
|
408
|
+
response_data: List[OrderBatchRequestResponseDto]
|
409
|
+
report: ErrorReportDto
|
410
|
+
data: BatchRequest
|
411
|
+
|
412
|
+
if report and (is_success_log or not report.is_ok):
|
413
|
+
with log_service.step("create_error_report"):
|
414
|
+
omnitron_integration.do_action(
|
415
|
+
key="create_error_report", objects=report
|
416
|
+
)
|
417
|
+
|
418
|
+
if response_data:
|
419
|
+
omnitron_integration.batch_request = batch_request_data
|
420
|
+
|
421
|
+
with log_service.step("process_order_batch_requests"):
|
422
|
+
omnitron_integration.do_action(
|
423
|
+
key="process_order_batch_requests",
|
424
|
+
objects=response_data,
|
425
|
+
)
|
426
|
+
except Exception as fatal:
|
427
|
+
log_service.add_exception(fatal)
|
428
|
+
raise
|
429
|
+
finally:
|
430
|
+
log_service.save()
|
308
431
|
|
309
432
|
def fetch_and_create_cancel(self, is_success_log=True):
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
key='get_cancelled_orders',
|
314
|
-
batch_request=omnitron_integration.batch_request
|
315
|
-
)
|
316
|
-
get_cancelled_order: Generator
|
433
|
+
log_service = LogService()
|
434
|
+
tx_id = uuid.uuid4()
|
435
|
+
log_service.create_flow(name="Fetch and Create Cancel", transaction_id=tx_id)
|
317
436
|
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
437
|
+
try:
|
438
|
+
with log_service.step("fetch_and_create_cancel"):
|
439
|
+
with OmnitronIntegration(
|
440
|
+
content_type=ContentType.order.value
|
441
|
+
) as omnitron_integration:
|
442
|
+
with log_service.step("get_cancelled_orders"):
|
443
|
+
get_cancelled_order = ChannelIntegration().do_action(
|
444
|
+
key="get_cancelled_orders",
|
445
|
+
batch_request=omnitron_integration.batch_request,
|
446
|
+
)
|
323
447
|
|
324
|
-
|
325
|
-
|
326
|
-
|
448
|
+
get_cancelled_order: Generator
|
449
|
+
|
450
|
+
while True:
|
451
|
+
try:
|
452
|
+
cancel_order_dto, report, _ = next(get_cancelled_order)
|
453
|
+
except StopIteration:
|
454
|
+
break
|
455
|
+
|
456
|
+
# tips
|
457
|
+
cancel_order_dto: CancelOrderDto
|
458
|
+
report: ErrorReportDto
|
459
|
+
|
460
|
+
if report and (is_success_log or not report.is_ok):
|
461
|
+
with log_service.step("create_error_report"):
|
462
|
+
omnitron_integration.do_action(
|
463
|
+
key="create_error_report", objects=report
|
464
|
+
)
|
327
465
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
466
|
+
with log_service.step("create_cancel"):
|
467
|
+
self.create_cancel(
|
468
|
+
omnitron_integration=omnitron_integration,
|
469
|
+
cancel_order_dto=cancel_order_dto,
|
470
|
+
)
|
332
471
|
|
333
|
-
|
334
|
-
|
472
|
+
except Exception as fatal:
|
473
|
+
log_service.add_exception(fatal)
|
474
|
+
raise
|
475
|
+
finally:
|
476
|
+
log_service.save()
|
335
477
|
|
336
|
-
def create_cancel(
|
337
|
-
|
478
|
+
def create_cancel(
|
479
|
+
self,
|
480
|
+
omnitron_integration: OmnitronIntegration,
|
481
|
+
cancel_order_dto: CancelOrderDto,
|
482
|
+
):
|
338
483
|
success_datas: List[Order] = omnitron_integration.do_action(
|
339
|
-
key="create_order_cancel",
|
340
|
-
|
484
|
+
key="create_order_cancel", objects=cancel_order_dto
|
485
|
+
)
|
341
486
|
try:
|
342
487
|
success_data = success_datas[0]
|
343
488
|
return success_data
|
@@ -345,137 +490,221 @@ class OrderService(object):
|
|
345
490
|
return
|
346
491
|
|
347
492
|
def fetch_and_create_cancellation_requests(self, is_success_log=True):
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
493
|
+
log_service = LogService()
|
494
|
+
tx_id = uuid.uuid4()
|
495
|
+
log_service.create_flow(
|
496
|
+
name="Fetch and Create Cancellation Requests", transaction_id=tx_id
|
497
|
+
)
|
498
|
+
|
499
|
+
try:
|
500
|
+
with log_service.step("fetch_and_create_cancellation_requests"):
|
501
|
+
with OmnitronIntegration(
|
502
|
+
content_type=ContentType.cancellation_request.value
|
503
|
+
) as omnitron_integration:
|
504
|
+
with log_service.step("get_cancellation_requests"):
|
505
|
+
get_cancellation_requests = ChannelIntegration().do_action(
|
506
|
+
key="get_cancellation_requests",
|
507
|
+
batch_request=omnitron_integration.batch_request,
|
508
|
+
)
|
509
|
+
|
510
|
+
get_cancellation_requests: Generator
|
511
|
+
while True:
|
512
|
+
try:
|
513
|
+
cancellation_request, report_list, _ = next(
|
514
|
+
get_cancellation_requests
|
515
|
+
)
|
516
|
+
except StopIteration:
|
517
|
+
break
|
518
|
+
|
519
|
+
# tips
|
520
|
+
cancellation_request: CancellationRequestDto
|
521
|
+
report_list: List[ErrorReportDto]
|
522
|
+
for report in report_list:
|
523
|
+
if report and (is_success_log or not report.is_ok):
|
524
|
+
report.error_code = (
|
525
|
+
f"{omnitron_integration.batch_request.local_batch_id}"
|
526
|
+
f"-Channel-GetCancellationRequests_{cancellation_request.order_item}"
|
527
|
+
)
|
528
|
+
|
529
|
+
with log_service.step("create_error_report"):
|
530
|
+
omnitron_integration.do_action(
|
531
|
+
key="create_error_report", objects=report
|
532
|
+
)
|
533
|
+
|
534
|
+
# omnitron integration do action create_cancellation_request
|
535
|
+
with log_service.step("create_cancellation_requests"):
|
536
|
+
cancellation_request_response = (
|
537
|
+
omnitron_integration.do_action(
|
538
|
+
key="create_cancellation_requests",
|
539
|
+
objects=cancellation_request,
|
540
|
+
)
|
541
|
+
)
|
542
|
+
try:
|
543
|
+
with log_service.step("batch_to_done"):
|
544
|
+
self.batch_service(settings.OMNITRON_CHANNEL_ID).to_done(
|
545
|
+
batch_request=omnitron_integration.batch_request
|
546
|
+
)
|
547
|
+
except requests_exceptions.HTTPError as exc:
|
548
|
+
log_service.add_exception(exc)
|
549
|
+
if (
|
550
|
+
exc.response.status_code == 406
|
551
|
+
and "batch_request_status_100_1" in exc.response.text
|
552
|
+
):
|
553
|
+
pass
|
554
|
+
else:
|
555
|
+
raise exc
|
556
|
+
except Exception as fatal:
|
557
|
+
log_service.add_exception(fatal)
|
558
|
+
raise
|
559
|
+
finally:
|
560
|
+
log_service.save()
|
388
561
|
|
389
562
|
def update_cancellation_requests(self, is_success_log=True):
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
563
|
+
log_service = LogService()
|
564
|
+
log_service.create_flow(name="Update Cancellation Requests")
|
565
|
+
|
566
|
+
try:
|
567
|
+
with log_service.step("update_cancellation_requests"):
|
568
|
+
with OmnitronIntegration(
|
569
|
+
content_type=ContentType.cancellation_request.value
|
570
|
+
) as omnitron_integration:
|
571
|
+
with log_service.step("get_cancellation_requests_update"):
|
572
|
+
cancellation_requests = omnitron_integration.do_action(
|
573
|
+
key="get_cancellation_requests_update", objects={}
|
574
|
+
)
|
575
|
+
|
576
|
+
cancellation_requests: List[CancellationRequest]
|
577
|
+
|
578
|
+
if not cancellation_requests:
|
579
|
+
omnitron_integration.batch_request.objects = None
|
580
|
+
with log_service.step("batch_to_fail"):
|
581
|
+
self.batch_service(omnitron_integration.channel_id).to_fail(
|
582
|
+
omnitron_integration.batch_request
|
583
|
+
)
|
584
|
+
|
585
|
+
return
|
586
|
+
|
587
|
+
batch_request_object_list = []
|
588
|
+
|
589
|
+
for cancellation_request in cancellation_requests:
|
590
|
+
with log_service.step("get_channel_order_item"):
|
591
|
+
remote_order_item = self.get_channel_order_item(
|
592
|
+
omnitron_integration, cancellation_request.order_item
|
593
|
+
)
|
594
|
+
|
595
|
+
with log_service.step("get_channel_reason"):
|
596
|
+
remote_reason = self.get_channel_reason(
|
597
|
+
omnitron_integration, cancellation_request.reason
|
598
|
+
)
|
599
|
+
|
600
|
+
with log_service.step("get_channel_cancellation_request"):
|
601
|
+
remote_cancellation_request = (
|
602
|
+
self.get_channel_cancellation_request(
|
603
|
+
omnitron_integration, cancellation_request.id
|
604
|
+
)
|
605
|
+
)
|
606
|
+
|
607
|
+
with log_service.step(
|
608
|
+
"create_channel_cancellation_request_dto"
|
609
|
+
):
|
610
|
+
channel_cancellation_request = ChannelCancellationRequestDto(
|
611
|
+
cancellation_type=cancellation_request.cancellation_type,
|
612
|
+
status=cancellation_request.status,
|
613
|
+
order_item=remote_order_item,
|
614
|
+
reason=remote_reason,
|
615
|
+
description=cancellation_request.description,
|
616
|
+
remote_id=remote_cancellation_request,
|
617
|
+
)
|
618
|
+
|
619
|
+
with log_service.step(
|
620
|
+
"update_cancellation_request",
|
621
|
+
metadata=channel_cancellation_request.remote_id,
|
622
|
+
):
|
623
|
+
(
|
624
|
+
response_data,
|
625
|
+
reports,
|
626
|
+
data,
|
627
|
+
) = ChannelIntegration().do_action(
|
628
|
+
key="update_cancellation_request",
|
629
|
+
objects=channel_cancellation_request,
|
630
|
+
batch_request=omnitron_integration.batch_request,
|
631
|
+
is_sync=True,
|
632
|
+
)
|
633
|
+
|
634
|
+
# tips
|
635
|
+
response_data: List[BatchRequestResponseDto]
|
636
|
+
reports: List[ErrorReportDto]
|
637
|
+
data: List[ChannelCancellationRequestDto]
|
638
|
+
|
639
|
+
if reports and (is_success_log or not reports[0].is_ok):
|
640
|
+
for report in reports:
|
641
|
+
with log_service.step("create_error_report"):
|
642
|
+
omnitron_integration.do_action(
|
643
|
+
key="create_error_report", objects=report
|
644
|
+
)
|
645
|
+
|
646
|
+
if response_data:
|
647
|
+
failed_reason_type = None
|
648
|
+
else:
|
649
|
+
failed_reason_type = FailedReasonType.remote.value
|
650
|
+
|
651
|
+
batch_request_object_dto = dict(
|
652
|
+
pk=cancellation_request.id,
|
653
|
+
version_date=cancellation_request.modified_date,
|
654
|
+
content_type=ContentType.cancellation_request.value,
|
655
|
+
failed_reason_type=failed_reason_type,
|
656
|
+
remote_id=remote_cancellation_request,
|
657
|
+
)
|
658
|
+
|
659
|
+
batch_request_object_list.append(batch_request_object_dto)
|
660
|
+
|
661
|
+
status = BatchRequestStatus.fail.value
|
662
|
+
if any(
|
663
|
+
[
|
664
|
+
br["failed_reason_type"] is None
|
665
|
+
for br in batch_request_object_list
|
666
|
+
]
|
667
|
+
):
|
668
|
+
status = BatchRequestStatus.done.value
|
669
|
+
|
670
|
+
omnitron_integration.batch_request.objects = (
|
671
|
+
batch_request_object_list
|
672
|
+
)
|
673
|
+
service = self.batch_service(omnitron_integration.channel_id)
|
674
|
+
|
675
|
+
if status == BatchRequestStatus.done.value:
|
676
|
+
with log_service.step("batch_to_done"):
|
677
|
+
service.to_done(
|
678
|
+
batch_request=omnitron_integration.batch_request
|
679
|
+
)
|
680
|
+
else:
|
681
|
+
with log_service.step("batch_to_fail"):
|
682
|
+
service.to_fail(
|
683
|
+
batch_request=omnitron_integration.batch_request
|
684
|
+
)
|
685
|
+
except Exception as fatal:
|
686
|
+
log_service.add_exception(fatal)
|
687
|
+
raise
|
688
|
+
finally:
|
689
|
+
log_service.save()
|
462
690
|
|
463
691
|
def get_channel_order_item(self, omnitron_integration, order_item):
|
464
692
|
channel_id = omnitron_integration.channel_id
|
465
|
-
end_point = ChannelIntegrationActionEndpoint(
|
466
|
-
channel_id=channel_id)
|
693
|
+
end_point = ChannelIntegrationActionEndpoint(channel_id=channel_id)
|
467
694
|
params = {
|
468
695
|
"channel": channel_id,
|
469
696
|
"content_type_name": ContentType.order_item.value,
|
470
|
-
"object_id": order_item
|
697
|
+
"object_id": order_item,
|
471
698
|
}
|
472
699
|
integration_action = end_point.list(params=params)
|
473
700
|
if not integration_action:
|
474
701
|
return Exception("Order item remote id not found: {}".format(order_item))
|
475
702
|
if len(integration_action) > 1:
|
476
|
-
return Exception(
|
703
|
+
return Exception(
|
704
|
+
"Multiple order item remote id found: {}".format(order_item)
|
705
|
+
)
|
477
706
|
return integration_action[0].remote_id
|
478
|
-
|
707
|
+
|
479
708
|
def get_channel_reason(self, omnitron_integration, omnitron_reason):
|
480
709
|
configuration = omnitron_integration.channel.conf
|
481
710
|
# configuration icinde yer alan reason_mapping anahtari bir json objesi,
|
@@ -484,25 +713,30 @@ class OrderService(object):
|
|
484
713
|
for channel_reason, omni_reason in configuration.get("reason_mapping", {}):
|
485
714
|
if omnitron_reason == omni_reason:
|
486
715
|
return channel_reason
|
487
|
-
|
716
|
+
|
488
717
|
return "10"
|
489
|
-
|
490
|
-
def get_channel_cancellation_request(
|
491
|
-
|
718
|
+
|
719
|
+
def get_channel_cancellation_request(
|
720
|
+
self, omnitron_integration, omnitron_cancel_request_pk
|
721
|
+
):
|
492
722
|
channel_id = omnitron_integration.channel_id
|
493
|
-
end_point = ChannelIntegrationActionEndpoint(
|
494
|
-
channel_id=channel_id)
|
723
|
+
end_point = ChannelIntegrationActionEndpoint(channel_id=channel_id)
|
495
724
|
params = {
|
496
725
|
"channel": channel_id,
|
497
726
|
"content_type_name": ContentType.cancellation_request.value,
|
498
|
-
"object_id": omnitron_cancel_request_pk
|
727
|
+
"object_id": omnitron_cancel_request_pk,
|
499
728
|
}
|
500
729
|
integration_action = end_point.list(params=params)
|
501
730
|
if not integration_action:
|
502
|
-
return Exception(
|
503
|
-
|
731
|
+
return Exception(
|
732
|
+
"Cancellation request remote id not found: {}".format(
|
733
|
+
omnitron_cancel_request_pk
|
734
|
+
)
|
735
|
+
)
|
504
736
|
if len(integration_action) > 1:
|
505
|
-
return Exception(
|
506
|
-
|
737
|
+
return Exception(
|
738
|
+
"Multiple cancellation request remote id found: {}".format(
|
739
|
+
omnitron_cancel_request_pk
|
740
|
+
)
|
741
|
+
)
|
507
742
|
return integration_action[0].remote_id
|
508
|
-
|