channel-app 0.0.157a9__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.
@@ -1,12 +1,15 @@
1
1
  from typing import List
2
2
 
3
- from omnisdk.omnitron.models import (ProductStock, Product, IntegrationAction,
4
- BatchRequest)
3
+ from omnisdk.omnitron.models import (
4
+ Product,
5
+ IntegrationAction,
6
+ BatchRequest,
7
+ )
5
8
 
6
9
  from channel_app.core import settings
7
- from channel_app.core.data import (ProductBatchRequestResponseDto,
8
- ErrorReportDto)
10
+ from channel_app.core.data import ProductBatchRequestResponseDto, ErrorReportDto
9
11
  from channel_app.core.settings import OmnitronIntegration, ChannelIntegration
12
+ from channel_app.logs.services import LogService
10
13
  from channel_app.omnitron.batch_request import ClientBatchRequest
11
14
  from channel_app.omnitron.constants import ContentType
12
15
 
@@ -14,241 +17,392 @@ from channel_app.omnitron.constants import ContentType
14
17
  class ProductService(object):
15
18
  batch_service = ClientBatchRequest
16
19
 
17
- def insert_products(self, add_mapped=True, add_stock=True, add_price=True,
18
- add_categories=True, is_sync=True, is_success_log=True):
19
- with OmnitronIntegration(
20
- content_type=ContentType.product.value) as omnitron_integration:
21
- products = omnitron_integration.do_action(
22
- key='get_inserted_products')
23
-
24
- first_product_count = len(products)
25
-
26
- if add_mapped:
27
- products = products and omnitron_integration.do_action(
28
- key='get_mapped_products', objects=products)
29
-
30
- if add_stock:
31
- products = products and omnitron_integration.do_action(
32
- key='get_product_stocks', objects=products)
33
-
34
- if add_price:
35
- products = products and omnitron_integration.do_action(
36
- key='get_product_prices', objects=products)
37
-
38
- if add_categories:
39
- products = products and omnitron_integration.do_action(
40
- key='get_product_categories', objects=products)
41
-
42
- if not products:
43
- if first_product_count:
44
- omnitron_integration.batch_request.objects = None
45
- self.batch_service(omnitron_integration.channel_id).to_fail(
46
- omnitron_integration.batch_request
47
- )
48
-
49
- return
50
-
51
- products: List[Product]
52
-
53
- response_data, reports, data = ChannelIntegration().do_action(
54
- key='send_inserted_products',
55
- objects=products,
56
- batch_request=omnitron_integration.batch_request,
57
- is_sync=is_sync)
58
-
59
- # tips
60
- response_data: List[ProductBatchRequestResponseDto]
61
- reports: List[ErrorReportDto]
62
- data: List[Product]
63
-
64
- if not is_sync:
65
- if reports[0].is_ok:
66
- self.batch_service(
67
- settings.OMNITRON_CHANNEL_ID).to_sent_to_remote(
68
- batch_request=omnitron_integration.batch_request)
69
- else:
70
- is_sync = True
71
-
72
- if reports and (is_success_log or not reports[0].is_ok):
73
- for report in reports:
74
- omnitron_integration.do_action(
75
- key='create_error_report',
76
- objects=report)
77
-
78
- if is_sync:
79
- omnitron_integration.do_action(
80
- key='process_product_batch_requests',
81
- objects=response_data)
82
-
83
- def update_products(self, add_mapped=True, add_stock=True, add_price=True,
84
- add_categories=True, is_sync=True, is_success_log=True):
85
- with OmnitronIntegration(
86
- content_type=ContentType.product.value) as omnitron_integration:
87
- products = omnitron_integration.do_action(
88
- key='get_updated_products')
89
-
90
- first_product_count = len(products)
91
-
92
- if add_mapped:
93
- products = products and omnitron_integration.do_action(
94
- key='get_mapped_products', objects=products)
95
-
96
- if add_stock:
97
- products = products and omnitron_integration.do_action(
98
- key='get_product_stocks', objects=products)
99
-
100
- if add_price:
101
- products = products and omnitron_integration.do_action(
102
- key='get_product_prices', objects=products)
103
-
104
- if add_categories:
105
- products = products and omnitron_integration.do_action(
106
- key='get_product_categories', objects=products)
107
-
108
- if not products:
109
- if first_product_count:
110
- omnitron_integration.batch_request.objects = None
111
- self.batch_service(omnitron_integration.channel_id).to_fail(
112
- omnitron_integration.batch_request
113
- )
114
-
115
- return
116
-
117
- products: List[Product]
118
-
119
- response_data, reports, data = ChannelIntegration().do_action(
120
- key='send_updated_products',
121
- objects=products,
122
- batch_request=omnitron_integration.batch_request,
123
- is_sync=is_sync)
124
-
125
- # tips
126
- response_data: List[ProductBatchRequestResponseDto]
127
- reports: List[ErrorReportDto]
128
- data: List[Product]
129
-
130
- if not is_sync:
131
- if reports[0].is_ok:
132
- self.batch_service(
133
- settings.OMNITRON_CHANNEL_ID).to_sent_to_remote(
134
- batch_request=omnitron_integration.batch_request)
135
- else:
136
- is_sync = True
137
-
138
- if reports and (is_success_log or not reports[0].is_ok):
139
- for report in reports:
140
- omnitron_integration.do_action(
141
- key='create_error_report',
142
- objects=report)
143
-
144
- if is_sync:
145
- omnitron_integration.do_action(
146
- key='process_product_batch_requests',
147
- objects=response_data)
148
-
149
- def delete_products(self, is_sync=True, is_content_object=True,
150
- is_success_log=True):
151
- with OmnitronIntegration(
152
- content_type=ContentType.integration_action.value) as omnitron_integration:
153
- products_integration_action = omnitron_integration.do_action(
154
- key='get_deleted_products')
155
- if not products_integration_action:
156
- return
157
- products_integration_action = omnitron_integration.do_action(
158
- key="get_content_objects_from_integrations",
159
- objects=products_integration_action
160
- )
161
- products_integration_action: List[IntegrationAction]
162
- response_data, reports, data = ChannelIntegration().do_action(
163
- key='send_deleted_products',
164
- objects=products_integration_action,
165
- batch_request=omnitron_integration.batch_request,
166
- is_sync=is_sync)
167
-
168
- # tips
169
- response_data: List[ProductBatchRequestResponseDto]
170
- reports: List[ErrorReportDto]
171
- data: List[IntegrationAction]
172
-
173
- if not is_sync:
174
- if reports[0].is_ok:
175
- self.batch_service(
176
- settings.OMNITRON_CHANNEL_ID).to_commit(
177
- batch_request=omnitron_integration.batch_request)
178
- self.batch_service(
179
- settings.OMNITRON_CHANNEL_ID).to_sent_to_remote(
180
- batch_request=omnitron_integration.batch_request)
181
- else:
182
- is_sync = True
183
-
184
- if reports and (is_success_log or not reports[0].is_ok):
185
- for report in reports:
186
- omnitron_integration.do_action(
187
- key='create_error_report',
188
- objects=report)
189
-
190
- if is_sync:
191
- omnitron_integration.do_action(
192
- key='process_delete_product_batch_requests',
193
- objects=response_data)
20
+ def insert_products(
21
+ self,
22
+ add_mapped=True,
23
+ add_stock=True,
24
+ add_price=True,
25
+ add_categories=True,
26
+ is_sync=True,
27
+ is_success_log=True,
28
+ ):
29
+ log_service = LogService()
30
+ log_service.create_flow(name="Insert Products")
31
+
32
+ try:
33
+ with log_service.step("insert_products"):
34
+ with OmnitronIntegration(
35
+ content_type=ContentType.product.value
36
+ ) as omnitron_integration:
37
+ with log_service.step("get_inserted_products"):
38
+ products = omnitron_integration.do_action(
39
+ key="get_inserted_products"
40
+ )
41
+
42
+ first_product_count = len(products)
43
+
44
+ if add_mapped:
45
+ with log_service.step("get_mapped_products"):
46
+ products = products and omnitron_integration.do_action(
47
+ key="get_mapped_products", objects=products
48
+ )
49
+
50
+ if add_stock:
51
+ with log_service.step("get_product_stocks"):
52
+ products = products and omnitron_integration.do_action(
53
+ key="get_product_stocks", objects=products
54
+ )
55
+
56
+ if add_price:
57
+ with log_service.step("get_product_prices"):
58
+ products = products and omnitron_integration.do_action(
59
+ key="get_product_prices", objects=products
60
+ )
61
+
62
+ if add_categories:
63
+ with log_service.step("get_product_categories"):
64
+ products = products and omnitron_integration.do_action(
65
+ key="get_product_categories", objects=products
66
+ )
67
+
68
+ if not products:
69
+ if first_product_count:
70
+ omnitron_integration.batch_request.objects = None
71
+
72
+ with log_service.step("batch_to_fail"):
73
+ self.batch_service(
74
+ omnitron_integration.channel_id
75
+ ).to_fail(omnitron_integration.batch_request)
76
+
77
+ return
78
+
79
+ products: List[Product]
80
+
81
+ with log_service.step("send_inserted_products"):
82
+ response_data, reports, data = ChannelIntegration().do_action(
83
+ key="send_inserted_products",
84
+ objects=products,
85
+ batch_request=omnitron_integration.batch_request,
86
+ is_sync=is_sync,
87
+ )
88
+
89
+ # tips
90
+ response_data: List[ProductBatchRequestResponseDto]
91
+ reports: List[ErrorReportDto]
92
+ data: List[Product]
93
+
94
+ if not is_sync:
95
+ if reports[0].is_ok:
96
+
97
+ with log_service.step("batch_send_to_remote"):
98
+ self.batch_service(
99
+ settings.OMNITRON_CHANNEL_ID
100
+ ).to_sent_to_remote(
101
+ batch_request=omnitron_integration.batch_request
102
+ )
103
+ else:
104
+ is_sync = True
105
+
106
+ if reports and (is_success_log or not reports[0].is_ok):
107
+ for report in reports:
108
+ with log_service.step("create_error_report"):
109
+ omnitron_integration.do_action(
110
+ key="create_error_report", objects=report
111
+ )
112
+
113
+ if is_sync:
114
+ with log_service.step("process_product_batch_requests"):
115
+ omnitron_integration.do_action(
116
+ key="process_product_batch_requests",
117
+ objects=response_data,
118
+ )
119
+ except Exception as fatal:
120
+ log_service.add_exception(fatal)
121
+ raise
122
+ finally:
123
+ log_service.save()
124
+
125
+ def update_products(
126
+ self,
127
+ add_mapped=True,
128
+ add_stock=True,
129
+ add_price=True,
130
+ add_categories=True,
131
+ is_sync=True,
132
+ is_success_log=True,
133
+ ):
134
+ log_service = LogService()
135
+ log_service.create_flow(name="Update Products")
136
+
137
+ try:
138
+ with log_service.step("update_products"):
139
+ with OmnitronIntegration(
140
+ content_type=ContentType.product.value
141
+ ) as omnitron_integration:
142
+ with log_service.step("get_updated_products"):
143
+ products = omnitron_integration.do_action(
144
+ key="get_updated_products"
145
+ )
146
+
147
+ first_product_count = len(products)
148
+
149
+ if add_mapped:
150
+ with log_service.step("get_mapped_products"):
151
+ products = products and omnitron_integration.do_action(
152
+ key="get_mapped_products", objects=products
153
+ )
154
+
155
+ if add_stock:
156
+ with log_service.step("get_product_stocks"):
157
+ products = products and omnitron_integration.do_action(
158
+ key="get_product_stocks", objects=products
159
+ )
160
+
161
+ if add_price:
162
+ with log_service.step("get_product_prices"):
163
+ products = products and omnitron_integration.do_action(
164
+ key="get_product_prices", objects=products
165
+ )
166
+
167
+ if add_categories:
168
+ with log_service.step("get_product_categories"):
169
+ products = products and omnitron_integration.do_action(
170
+ key="get_product_categories", objects=products
171
+ )
172
+
173
+ if not products:
174
+ if first_product_count:
175
+ omnitron_integration.batch_request.objects = None
176
+ with log_service.step("batch_to_fail"):
177
+ self.batch_service(
178
+ omnitron_integration.channel_id
179
+ ).to_fail(omnitron_integration.batch_request)
180
+
181
+ return
182
+
183
+ products: List[Product]
184
+
185
+ with log_service.step("send_updated_products"):
186
+ response_data, reports, data = ChannelIntegration().do_action(
187
+ key="send_updated_products",
188
+ objects=products,
189
+ batch_request=omnitron_integration.batch_request,
190
+ is_sync=is_sync,
191
+ )
192
+
193
+ # tips
194
+ response_data: List[ProductBatchRequestResponseDto]
195
+ reports: List[ErrorReportDto]
196
+ data: List[Product]
197
+
198
+ if not is_sync:
199
+ if reports[0].is_ok:
200
+ with log_service.step("batch_send_to_remote"):
201
+ self.batch_service(
202
+ settings.OMNITRON_CHANNEL_ID
203
+ ).to_sent_to_remote(
204
+ batch_request=omnitron_integration.batch_request
205
+ )
206
+ else:
207
+ is_sync = True
208
+
209
+ if reports and (is_success_log or not reports[0].is_ok):
210
+ for report in reports:
211
+ with log_service.step("create_error_report"):
212
+ omnitron_integration.do_action(
213
+ key="create_error_report", objects=report
214
+ )
215
+
216
+ if is_sync:
217
+ with log_service.step("process_product_batch_requests"):
218
+ omnitron_integration.do_action(
219
+ key="process_product_batch_requests",
220
+ objects=response_data,
221
+ )
222
+ except Exception as fatal:
223
+ log_service.add_exception(fatal)
224
+ raise
225
+ finally:
226
+ log_service.save()
227
+
228
+ def delete_products(
229
+ self, is_sync=True, is_content_object=True, is_success_log=True
230
+ ):
231
+ log_service = LogService()
232
+ log_service.create_flow("Delete Products")
233
+
234
+ try:
235
+ with log_service.step("delete_products"):
236
+ with OmnitronIntegration(
237
+ content_type=ContentType.integration_action.value
238
+ ) as omnitron_integration:
239
+ with log_service.step("get_deleted_products"):
240
+ products_integration_action = omnitron_integration.do_action(
241
+ key="get_deleted_products"
242
+ )
243
+ if not products_integration_action:
244
+ return
245
+
246
+ with log_service.step("get_content_objects_from_integrations"):
247
+ products_integration_action = omnitron_integration.do_action(
248
+ key="get_content_objects_from_integrations",
249
+ objects=products_integration_action,
250
+ )
251
+ products_integration_action: List[IntegrationAction]
252
+
253
+ with log_service.step("send_deleted_products"):
254
+ response_data, reports, data = ChannelIntegration().do_action(
255
+ key="send_deleted_products",
256
+ objects=products_integration_action,
257
+ batch_request=omnitron_integration.batch_request,
258
+ is_sync=is_sync,
259
+ )
260
+
261
+ # tips
262
+ response_data: List[ProductBatchRequestResponseDto]
263
+ reports: List[ErrorReportDto]
264
+ data: List[IntegrationAction]
265
+
266
+ if not is_sync:
267
+ if reports[0].is_ok:
268
+ with log_service.step("batch_to_commit"):
269
+ self.batch_service(
270
+ settings.OMNITRON_CHANNEL_ID
271
+ ).to_commit(
272
+ batch_request=omnitron_integration.batch_request
273
+ )
274
+ with log_service.step("batch_send_to_remote"):
275
+ self.batch_service(
276
+ settings.OMNITRON_CHANNEL_ID
277
+ ).to_sent_to_remote(
278
+ batch_request=omnitron_integration.batch_request
279
+ )
280
+ else:
281
+ is_sync = True
282
+
283
+ if reports and (is_success_log or not reports[0].is_ok):
284
+ for report in reports:
285
+ with log_service.step("create_error_report"):
286
+ omnitron_integration.do_action(
287
+ key="create_error_report", objects=report
288
+ )
289
+
290
+ if is_sync:
291
+ with log_service.step("process_delete_product_batch_requests"):
292
+ omnitron_integration.do_action(
293
+ key="process_delete_product_batch_requests",
294
+ objects=response_data,
295
+ )
296
+ except Exception as fatal:
297
+ log_service.add_exception(fatal)
298
+ raise
299
+ finally:
300
+ log_service.save()
194
301
 
195
302
  def get_delete_product_batch_requests(self, is_success_log=True):
196
- with OmnitronIntegration(create_batch=False) as omnitron_integration:
197
- batch_request_data = omnitron_integration.do_action(
198
- 'get_batch_requests',
199
- params={
200
- "status": ["sent_to_remote", "ongoing"],
201
- "content_type": ContentType.integration_action.value})
202
- # tips
203
- batch_request_data: List[BatchRequest]
204
-
205
- for batch_request in batch_request_data:
206
- response_data, report, data = ChannelIntegration().do_action(
207
- key='check_deleted_products', objects=batch_request,
208
- batch_request=batch_request)
209
-
210
- # tips
211
- response_data: List[ProductBatchRequestResponseDto]
212
- report: ErrorReportDto
213
- data: BatchRequest
214
-
215
- if report and (is_success_log or not report.is_ok):
216
- omnitron_integration.do_action(
217
- key='create_error_report',
218
- objects=report)
219
- if response_data:
220
- omnitron_integration.batch_request = batch_request
221
- omnitron_integration.do_action(
222
- key='process_delete_product_batch_requests',
223
- objects=response_data)
303
+ log_service = LogService()
304
+ log_service.create_flow(name="Get Delete Product Batch Requests")
305
+
306
+ try:
307
+ with log_service.step("get_delete_product_batch_requests"):
308
+ with OmnitronIntegration(create_batch=False) as omnitron_integration:
309
+ with log_service.step("get_batch_requests"):
310
+ batch_request_data = omnitron_integration.do_action(
311
+ "get_batch_requests",
312
+ params={
313
+ "status": ["sent_to_remote", "ongoing"],
314
+ "content_type": ContentType.integration_action.value,
315
+ },
316
+ )
317
+ # tips
318
+ batch_request_data: List[BatchRequest]
319
+
320
+ for batch_request in batch_request_data:
321
+ with log_service.step("check_deleted_products"):
322
+ (
323
+ response_data,
324
+ report,
325
+ data,
326
+ ) = ChannelIntegration().do_action(
327
+ key="check_deleted_products",
328
+ objects=batch_request,
329
+ batch_request=batch_request,
330
+ )
331
+
332
+ # tips
333
+ response_data: List[ProductBatchRequestResponseDto]
334
+ report: ErrorReportDto
335
+ data: BatchRequest
336
+
337
+ if report and (is_success_log or not report.is_ok):
338
+ with log_service.step("create_error_report"):
339
+ omnitron_integration.do_action(
340
+ key="create_error_report", objects=report
341
+ )
342
+ if response_data:
343
+ omnitron_integration.batch_request = batch_request
344
+ with log_service.step(
345
+ "process_delete_product_batch_requests"
346
+ ):
347
+ omnitron_integration.do_action(
348
+ key="process_delete_product_batch_requests",
349
+ objects=response_data,
350
+ )
351
+ except Exception as fatal:
352
+ log_service.add_exception(fatal)
353
+ finally:
354
+ log_service.save()
224
355
 
225
356
  def get_product_batch_requests(self, is_success_log=True):
226
- with OmnitronIntegration(create_batch=False) as omnitron_integration:
227
- batch_request_data = omnitron_integration.do_action(
228
- 'get_batch_requests',
229
- params={
230
- "status": ["sent_to_remote", "ongoing"],
231
- "content_type": ContentType.product.value})
232
- # tips
233
- batch_request_data: List[BatchRequest]
234
-
235
- for batch_request in batch_request_data:
236
- response_data, report, data = ChannelIntegration().do_action(
237
- key='check_products', objects=batch_request,
238
- batch_request=batch_request)
239
-
240
- # tips
241
- response_data: List[ProductBatchRequestResponseDto]
242
- report: ErrorReportDto
243
- data: BatchRequest
244
-
245
- if report and (is_success_log or not report.is_ok):
246
- omnitron_integration.do_action(
247
- key='create_error_report',
248
- objects=report)
249
-
250
- if response_data:
251
- omnitron_integration.batch_request = batch_request
252
- omnitron_integration.do_action(
253
- key='process_product_batch_requests',
254
- objects=response_data)
357
+ log_service = LogService()
358
+ log_service.create_flow(name="Get Product Batch Requests")
359
+
360
+ try:
361
+ with log_service.step("get_product_batch_requests"):
362
+ with OmnitronIntegration(create_batch=False) as omnitron_integration:
363
+ with log_service.step("get_batch_requests"):
364
+ batch_request_data = omnitron_integration.do_action(
365
+ "get_batch_requests",
366
+ params={
367
+ "status": ["sent_to_remote", "ongoing"],
368
+ "content_type": ContentType.product.value,
369
+ },
370
+ )
371
+ # tips
372
+ batch_request_data: List[BatchRequest]
373
+
374
+ for batch_request in batch_request_data:
375
+ with log_service.step("check_products"):
376
+ (
377
+ response_data,
378
+ report,
379
+ data,
380
+ ) = ChannelIntegration().do_action(
381
+ key="check_products",
382
+ objects=batch_request,
383
+ batch_request=batch_request,
384
+ )
385
+
386
+ # tips
387
+ response_data: List[ProductBatchRequestResponseDto]
388
+ report: ErrorReportDto
389
+ data: BatchRequest
390
+
391
+ if report and (is_success_log or not report.is_ok):
392
+ with log_service.step("create_error_report"):
393
+ omnitron_integration.do_action(
394
+ key="create_error_report", objects=report
395
+ )
396
+
397
+ if response_data:
398
+ omnitron_integration.batch_request = batch_request
399
+ with log_service.step("process_product_batch_requests"):
400
+ omnitron_integration.do_action(
401
+ key="process_product_batch_requests",
402
+ objects=response_data,
403
+ )
404
+ except Exception as fatal:
405
+ log_service.add_exception(fatal)
406
+ raise
407
+ finally:
408
+ log_service.save()