channel-app 0.0.131__py3-none-any.whl → 0.0.135__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.
Files changed (61) hide show
  1. channel_app/app/product/service.py +16 -0
  2. channel_app/core/commands.py +3 -1
  3. channel_app/core/tests.py +15 -164
  4. channel_app/omnitron/commands/tests/test_products.py +494 -0
  5. {channel_app-0.0.131.dist-info → channel_app-0.0.135.dist-info}/METADATA +1 -1
  6. channel_app-0.0.135.dist-info/RECORD +60 -0
  7. {channel_app-0.0.131.dist-info → channel_app-0.0.135.dist-info}/WHEEL +1 -1
  8. channel_app/channel_app/app/__init__.py +0 -0
  9. channel_app/channel_app/app/order/__init__.py +0 -0
  10. channel_app/channel_app/app/order/service.py +0 -230
  11. channel_app/channel_app/app/product/__init__.py +0 -0
  12. channel_app/channel_app/app/product/service.py +0 -237
  13. channel_app/channel_app/app/product_price/__init__.py +0 -0
  14. channel_app/channel_app/app/product_price/service.py +0 -254
  15. channel_app/channel_app/app/product_stock/__init__.py +0 -0
  16. channel_app/channel_app/app/product_stock/service.py +0 -258
  17. channel_app/channel_app/app/setup/__init__.py +0 -0
  18. channel_app/channel_app/app/setup/service.py +0 -61
  19. channel_app/channel_app/channel/__init__.py +0 -0
  20. channel_app/channel_app/channel/commands/__init__.py +0 -0
  21. channel_app/channel_app/channel/commands/orders/__init__.py +0 -0
  22. channel_app/channel_app/channel/commands/orders/orders.py +0 -329
  23. channel_app/channel_app/channel/commands/product_categories.py +0 -1
  24. channel_app/channel_app/channel/commands/product_images.py +0 -1
  25. channel_app/channel_app/channel/commands/product_prices.py +0 -148
  26. channel_app/channel_app/channel/commands/product_stocks.py +0 -220
  27. channel_app/channel_app/channel/commands/products.py +0 -161
  28. channel_app/channel_app/channel/commands/setup.py +0 -948
  29. channel_app/channel_app/channel/integration.py +0 -84
  30. channel_app/channel_app/core/__init__.py +0 -0
  31. channel_app/channel_app/core/clients.py +0 -12
  32. channel_app/channel_app/core/commands.py +0 -364
  33. channel_app/channel_app/core/data.py +0 -227
  34. channel_app/channel_app/core/integration.py +0 -74
  35. channel_app/channel_app/core/products.py +0 -64
  36. channel_app/channel_app/core/settings.py +0 -28
  37. channel_app/channel_app/core/utilities.py +0 -99
  38. channel_app/channel_app/omnitron/__init__.py +0 -0
  39. channel_app/channel_app/omnitron/batch_request.py +0 -82
  40. channel_app/channel_app/omnitron/commands/__init__.py +0 -0
  41. channel_app/channel_app/omnitron/commands/batch_requests.py +0 -281
  42. channel_app/channel_app/omnitron/commands/error_reports.py +0 -86
  43. channel_app/channel_app/omnitron/commands/integration_actions.py +0 -200
  44. channel_app/channel_app/omnitron/commands/orders/__init__.py +0 -0
  45. channel_app/channel_app/omnitron/commands/orders/addresses.py +0 -242
  46. channel_app/channel_app/omnitron/commands/orders/cargo_companies.py +0 -40
  47. channel_app/channel_app/omnitron/commands/orders/customers.py +0 -72
  48. channel_app/channel_app/omnitron/commands/orders/orders.py +0 -450
  49. channel_app/channel_app/omnitron/commands/product_categories.py +0 -1
  50. channel_app/channel_app/omnitron/commands/product_images.py +0 -1
  51. channel_app/channel_app/omnitron/commands/product_prices.py +0 -192
  52. channel_app/channel_app/omnitron/commands/product_stocks.py +0 -229
  53. channel_app/channel_app/omnitron/commands/products.py +0 -735
  54. channel_app/channel_app/omnitron/commands/setup.py +0 -839
  55. channel_app/channel_app/omnitron/constants.py +0 -98
  56. channel_app/channel_app/omnitron/exceptions.py +0 -42
  57. channel_app/channel_app/omnitron/integration.py +0 -159
  58. channel_app/setup.py +0 -21
  59. channel_app-0.0.131.dist-info/RECORD +0 -110
  60. /channel_app/{channel_app → omnitron/commands/tests}/__init__.py +0 -0
  61. {channel_app-0.0.131.dist-info → channel_app-0.0.135.dist-info}/top_level.txt +0 -0
@@ -1,86 +0,0 @@
1
- from omnisdk.omnitron.endpoints import ChannelErrorReportEndpoint, \
2
- ContentTypeEndpoint
3
- from omnisdk.omnitron.models import ErrorReport
4
-
5
- from channel_app.core.commands import OmnitronCommandInterface
6
- from channel_app.core.data import ErrorReportDto
7
- from channel_app.omnitron.constants import CONTENT_TYPE_IDS
8
-
9
-
10
- class CreateAddressErrorReports(OmnitronCommandInterface):
11
- endpoint = ChannelErrorReportEndpoint
12
-
13
- def get_data(self) -> list:
14
- data = self.objects
15
- return data
16
-
17
- def send(self, validated_data) -> object:
18
- endpoint = self.endpoint(
19
- channel_id=self.integration.channel_id,
20
- raw=True,
21
- path=validated_data["type"])
22
- obj = endpoint.create(item=validated_data)
23
- return obj
24
-
25
- def row_send_error_report(self):
26
- pass
27
-
28
- def send_error_report(self, raw_request, raw_response):
29
- pass
30
-
31
- def check_run(self, is_ok, formatted_data):
32
- return False
33
-
34
- class CreateErrorReports(OmnitronCommandInterface):
35
- endpoint = ChannelErrorReportEndpoint
36
-
37
- def get_data(self) -> object:
38
- return self.objects
39
-
40
- def validated_data(self, data):
41
- assert isinstance(data, ErrorReportDto)
42
- return data
43
-
44
- def send(self, validated_data: ErrorReportDto) -> object:
45
- error_report = ErrorReport(
46
- action_content_type=self.get_content_type(
47
- validated_data.action_content_type),
48
- action_object_id=validated_data.action_object_id,
49
- target_content_type=self.get_content_type(
50
- validated_data.target_content_type or "channel"),
51
- target_object_id=validated_data.target_object_id or self.integration.channel_id,
52
- obj_modified_date=validated_data.modified_date,
53
- error_code=validated_data.error_code or "custom",
54
- error_desc=validated_data.error_description or "custom",
55
- raw_request=validated_data.raw_request,
56
- raw_response=validated_data.raw_response,
57
- is_ok=validated_data.is_ok
58
- )
59
- report = self.endpoint(
60
- channel_id=self.integration.channel_id).create(item=error_report)
61
- return [report]
62
-
63
- def get_content_type(self, content_type: str):
64
- try:
65
- return CONTENT_TYPE_IDS[content_type]
66
- except KeyError:
67
- pass
68
- content_type = ContentTypeEndpoint().list(
69
- params={"model": content_type})
70
- if not content_type:
71
- raise Exception("Invalid Content Type")
72
- if len(content_type) > 1:
73
- raise Exception("Invalid Content Type")
74
-
75
- content_type_id = content_type[0].id
76
- CONTENT_TYPE_IDS[content_type[0].model] = content_type_id
77
- return content_type_id
78
-
79
- def row_send_error_report(self):
80
- pass
81
-
82
- def send_error_report(self, raw_request, raw_response):
83
- pass
84
-
85
- def check_run(self, is_ok, formatted_data):
86
- return False
@@ -1,200 +0,0 @@
1
- from collections import defaultdict
2
- from typing import List
3
-
4
- from omnisdk.omnitron.endpoints import ChannelIntegrationActionEndpoint, \
5
- ChannelProductEndpoint, ChannelProductPriceEndpoint, \
6
- ChannelProductStockEndpoint
7
- from omnisdk.omnitron.models import IntegrationAction
8
-
9
- from channel_app.core.commands import OmnitronCommandInterface
10
- from channel_app.core.utilities import split_list
11
-
12
-
13
- class CreateIntegrationActions(OmnitronCommandInterface):
14
- endpoint = ChannelIntegrationActionEndpoint
15
-
16
- def get_data(self) -> list:
17
- obj_list = self.objects
18
- items = []
19
- for obj in obj_list:
20
- data = {
21
- "content_type": obj.content_type,
22
- "channel_id": self.integration.channel_id,
23
- "object_id": obj.pk,
24
- "remote_id": obj.remote_id,
25
- "version_date": obj.modified_date,
26
- "state": {}
27
- }
28
- items.append(data)
29
- return items
30
-
31
- def send(self, validated_data) -> object:
32
- integration_actions = []
33
- for item in validated_data:
34
- integration_action = self.endpoint(
35
- channel_id=self.integration.channel_id).create(item=item)
36
-
37
- integration_actions.append(integration_action)
38
- return integration_actions
39
-
40
-
41
- class UpdateIntegrationActions(CreateIntegrationActions):
42
- endpoint = ChannelIntegrationActionEndpoint
43
-
44
- def get_data(self) -> list:
45
- return self.objects
46
-
47
- def send(self, validated_data) -> object:
48
- integration_actions = []
49
- for item in validated_data:
50
- item.content_type_id = item.content_type['id']
51
- delattr(item, "content_type")
52
-
53
- integration_action = self.endpoint(
54
- channel_id=self.integration.channel_id
55
- ).update(id=item.pk, item=item)
56
-
57
- integration_actions.append(integration_action)
58
- return integration_actions
59
-
60
-
61
- class GetIntegrationActionsWithObjectId(OmnitronCommandInterface):
62
- endpoint = ChannelIntegrationActionEndpoint
63
- id_type = "object_id__in"
64
- CHUNK_SIZE = 50
65
-
66
- def get_ia_dict(self, integration_action):
67
- return {i.object_id: i for i in integration_action}
68
-
69
- def get_data(self) -> List:
70
- integration_action_list = []
71
- group_by_content_type = self.get_grup_by_content_type_pk_list()
72
-
73
- for ct, pk_list in group_by_content_type.items():
74
- for chunk_pk_list in split_list(pk_list, self.CHUNK_SIZE):
75
- chunk_ia = self.endpoint(
76
- channel_id=self.integration.channel_id
77
- ).list(params={
78
- "channel_id": self.integration.channel_id,
79
- "content_type_name": ct,
80
- self.id_type: ",".join([str(pk) for pk in chunk_pk_list])
81
- })
82
- integration_action_list.extend(chunk_ia)
83
-
84
- ia_dict = self.get_ia_dict(integration_action_list)
85
- self.update_objects(ia_dict)
86
- return self.objects
87
-
88
- def update_objects(self, ia_dict):
89
- for obj in self.objects:
90
- obj.integration_action = ia_dict[obj.pk]
91
-
92
- def get_grup_by_content_type_pk_list(self):
93
- group_by_content_type = defaultdict(list)
94
- for obj in self.objects:
95
- group_by_content_type[obj.content_type].append(obj.pk)
96
- return group_by_content_type
97
-
98
-
99
- class GetIntegrationActionsWithRemoteId(GetIntegrationActionsWithObjectId):
100
- endpoint = ChannelIntegrationActionEndpoint
101
- id_type = "remote_id__in"
102
-
103
- def get_grup_by_content_type_pk_list(self):
104
- group_by_content_type = defaultdict(list)
105
- for obj in self.objects:
106
- group_by_content_type[obj.content_type].append(obj.remote_id)
107
- return group_by_content_type
108
-
109
- def get_ia_dict(self, ia):
110
- return {i.remote_id: i for i in ia}
111
-
112
- def update_objects(self, ia_dict):
113
- for obj in self.objects:
114
- obj.integration_action = ia_dict[obj.remote_id]
115
-
116
-
117
- class GetIntegrationActions(OmnitronCommandInterface):
118
- endpoint = ChannelIntegrationActionEndpoint
119
- CHUNK_SIZE = 10
120
-
121
- def get_data(self) -> dict:
122
- if "limit" not in self.objects:
123
- self.objects.update({"limit": self.CHUNK_SIZE})
124
- return self.objects
125
-
126
- def run(self) -> List[IntegrationAction]:
127
- integration_action_endpoint = ChannelIntegrationActionEndpoint(
128
- channel_id=self.integration.channel_id)
129
- self.objects.update({
130
- "channel_id": self.integration.channel_id,
131
- })
132
- integration_action_list = integration_action_endpoint.list(
133
- params=self.objects)
134
- for batch in integration_action_endpoint.iterator:
135
- if not batch:
136
- break
137
- integration_action_list.extend(batch)
138
- return integration_action_list
139
-
140
-
141
- class GetObjectsFromIntegrationAction(OmnitronCommandInterface):
142
- endpoint = {
143
- "product": ChannelProductEndpoint,
144
- "productprice": ChannelProductPriceEndpoint,
145
- "productstock": ChannelProductStockEndpoint
146
- }
147
- CHUNK_SIZE = 100
148
-
149
- def get_data(self) -> List[IntegrationAction]:
150
- """
151
-
152
- :return: Added content type object in integration action objects
153
- Example: if content type product, -> integration_action.product
154
- if content type productprice, -> integration_action.productprice
155
- """
156
- integration_actions = self.objects
157
- group = self.group_by_content_type(integration_actions)
158
- grouped_integration_actions = self.get_content_objects(group)
159
- return self.extract_group(grouped_integration_actions)
160
-
161
- def group_by_content_type(self, integration_actions):
162
- """
163
-
164
- :param integration_actions:
165
- :return:
166
- {
167
- "product":[integration1,integration2,...],
168
- "productprice":[integration3,integration4,...]
169
- }
170
- """
171
- group = defaultdict(list)
172
- for integration_action in integration_actions:
173
- model = integration_action.content_type.get("model")
174
- group[model].append(integration_action)
175
- return group
176
-
177
- def get_content_objects(self, grouped_integration_actions: dict):
178
- for content_type, integration_actions in grouped_integration_actions.items():
179
- endpoint = self.endpoint.get(content_type)
180
- if endpoint:
181
- id_list = [str(ia.object_id) for ia in integration_actions]
182
- end_point = endpoint(channel_id=self.integration.channel_id)
183
- objects_list = []
184
- for chunk_id_list in split_list(id_list, self.CHUNK_SIZE):
185
- objects = end_point.list(
186
- params={"pk__in": ",".join(chunk_id_list),
187
- "limit": len(chunk_id_list)})
188
- objects_list.extend(objects)
189
- objects_dict = {s.pk: s for s in objects_list}
190
- for integration_action in integration_actions:
191
- setattr(integration_action, content_type,
192
- objects_dict.get(integration_action.object_id))
193
-
194
- return grouped_integration_actions
195
-
196
- def extract_group(self, group: dict):
197
- object_list = []
198
- for key, value_list in group.items():
199
- object_list.extend(value_list)
200
- return object_list
@@ -1,242 +0,0 @@
1
- from omnisdk.omnitron.endpoints import (ChannelAddressEndpoint,
2
- ChannelCountryEndpoint,
3
- ChannelCityEndpoint,
4
- ChannelTownshipEndpoint,
5
- ChannelDistrictEndpoint)
6
- from omnisdk.omnitron.models import (City, Country, Township, District, Address,
7
- Customer)
8
- from requests import HTTPError
9
-
10
- from channel_app.core.commands import OmnitronCommandInterface
11
- from channel_app.core.data import AddressDto
12
- from channel_app.omnitron.constants import INTEGRATION_TYPE, ErrorType
13
- from channel_app.omnitron.exceptions import (CountryException,
14
- IntegrationMappingException,
15
- CityException, TownshipException,
16
- DistrictException)
17
-
18
-
19
- class GetOrCreateAddress(OmnitronCommandInterface):
20
- endpoint = ChannelAddressEndpoint
21
-
22
- def get_data(self) -> dict:
23
- """
24
- :return:
25
- {
26
- "email": "john.doe@akinon.com",
27
- "phone_number": "05556667788",
28
- "first_name": "John",
29
- "last_name": "Doe",
30
- "country": 1,
31
- "city": 80,
32
- "line": "Hemen sahil kenarı",
33
- "title": "COMM-876",
34
- "township": 933,
35
- "district": 71387,
36
- "postcode": "",
37
- "notes": null,
38
- "company_name": "",
39
- "tax_office": "",
40
- "tax_no": "",
41
- "e_bill_taxpayer": false,
42
- "hash_data": "b5e07b50cb033f5e4c86de7f1093bb37",
43
- "address_type": "customer",
44
- "retail_store": null,
45
- "remote_id": null,
46
- "identity_number": null,
47
- "extra_field": {},
48
- "customer": 36406,
49
- "is_active": true
50
- },
51
- """
52
- address: AddressDto = self.objects["address"]
53
- customer: Customer = self.objects["customer"]
54
- country_pk, city_pk, township_pk, district_pk = \
55
- self.get_location_objects(address.country,
56
- address.city,
57
- address.township,
58
- address.district)
59
- data = {
60
- "email": address.email[:254],
61
- "phone_number": address.phone_number[:128],
62
- "first_name": customer.first_name[:255],
63
- "last_name": customer.last_name[:255],
64
- "customer": customer.pk,
65
- "country": country_pk,
66
- "city": city_pk,
67
- "township": township_pk,
68
- "district": district_pk,
69
- "title": address.title and address.title[:128],
70
- "line": address.line[:255],
71
- "postcode": address.postcode[:64],
72
- "notes": address.notes and address.notes[:512],
73
- "company_name": address.company_name and address.company_name[:255],
74
- "tax_office": address.tax_office and address.tax_office[:64],
75
- "tax_no": address.tax_no and address.tax_no[:20],
76
- "e_bill_taxpayer": address.e_bill_taxpayer or False,
77
- "remote_id": address.remote_id,
78
- "identity_number": address.identity_number and address.identity_number[:64],
79
- "extra_field": address.extra_field or {}
80
- }
81
- return data
82
-
83
- def send(self, validated_data) -> object:
84
- """
85
-
86
- :param validated_data: data for address
87
- :return: address objects
88
- """
89
- try:
90
- address_obj = Address(**validated_data)
91
- address = self.endpoint(
92
- channel_id=self.integration.channel_id).create(
93
- item=address_obj)
94
- except HTTPError as e:
95
- addresses = []
96
- if ("hash_data" in e.response.text and
97
- "non_field_errors" in e.response.text):
98
- try:
99
- error_data = e.response.json()['non_field_errors'].split()[0].split(':')
100
- if error_data[0] == "hash_data":
101
- hash_data = error_data[1]
102
- addresses = self.endpoint(
103
- channel_id=self.integration.channel_id).list(
104
- params={"hash_data": hash_data})
105
- except Exception:
106
- pass
107
- if len(addresses) != 1:
108
- raise
109
- address = addresses[0]
110
- return [address]
111
-
112
- def get_location_objects(self, country_code, city_name, township_name=None,
113
- district_name=None):
114
- """
115
- :param country_code: country code ("Tr")
116
- :param city_name: city name ("Istanbul")
117
- :param township_name: town ship ("Kadikoy")
118
- :param district_name: district name ("Moda")
119
- :return: country_pk, city_pk, township_pk, district_pk
120
- """
121
- country = self.get_country(country_code=country_code)
122
- country_pk = country.pk
123
-
124
- city = self.get_city(country=country, city_name=city_name)
125
- city_pk = city.pk
126
- township = None
127
- township_pk = None
128
- if township_name:
129
- township = self.get_township(country=country, city=city,
130
- township_name=township_name)
131
- township_pk = township.pk
132
- district_pk = None
133
- if district_name and township:
134
- district = self.get_district(country=country, city=city,
135
- township=township,
136
- district_name=district_name)
137
- district_pk = district.pk
138
- return country_pk, city_pk, township_pk, district_pk
139
-
140
- def get_mapping_object(self, code, endpoint):
141
- """
142
- :param code: Identifier code for the City and Country on the Sales Channel. This is
143
- prefix with channel_id as integration_code
144
- :param endpoint: omnitron sdk endpoint
145
- :return: mapped objects E.g (City, Country)
146
- """
147
- integration_code = f"{self.integration.channel_id}_{code}"
148
- params = {"mapping__code__exact": integration_code,
149
- "mapping__integration_type": INTEGRATION_TYPE}
150
- objects = endpoint.list(params=params)
151
- if len(objects) != 1:
152
- raise IntegrationMappingException(params={"code": integration_code})
153
- return objects
154
-
155
- def get_country(self, country_code: str) -> Country:
156
- endpoint = ChannelCountryEndpoint(channel_id=self.integration.channel_id)
157
-
158
- params = {"code__exact": country_code}
159
- countries = endpoint.list(params=params)
160
- if len(countries) == 1:
161
- return countries[0]
162
- try:
163
- countries = self.get_mapping_object(country_code, endpoint)
164
- except IntegrationMappingException as exc:
165
- raise CountryException(params={
166
- "type": ErrorType.country.value,
167
- "remote_code": exc.params['code'],
168
- "country_code": country_code,
169
- "name": country_code,
170
- "integration_type": INTEGRATION_TYPE,
171
- "err_code": "Country was not found",
172
- "err_desc": f"Country code {country_code} was not found in Omnitron"})
173
- return countries[0]
174
-
175
- def get_city(self, country: Country, city_name: str) -> City:
176
- endpoint = ChannelCityEndpoint(channel_id=self.integration.channel_id)
177
-
178
- params = {"name__exact": city_name, "country": country.pk}
179
- cities = endpoint.list(params=params)
180
- if len(cities) == 1:
181
- return cities[0]
182
- try:
183
- cities = self.get_mapping_object(code=city_name, endpoint=endpoint)
184
- except IntegrationMappingException as exc:
185
- raise CityException(
186
- params={"type": ErrorType.city.value,
187
- "remote_code": exc.params['code'],
188
- "country_id": country.pk,
189
- "integration_type": INTEGRATION_TYPE,
190
- "err_code": "integrations_200_10_2",
191
- "err_desc": f"Address is not valid. channel: {self.integration.channel_id} "
192
- f"code:{exc.params['code']} "
193
- f"model: City country id: {country.pk}"})
194
- return cities[0]
195
-
196
- def get_township(self, country: Country, city: City, township_name: str) -> Township:
197
- endpoint = ChannelTownshipEndpoint(channel_id=self.integration.channel_id)
198
-
199
- params = {"name__exact": township_name, "country": country.pk, "city": city.pk}
200
- townships = endpoint.list(params=params)
201
- if len(townships) == 1:
202
- return townships[0]
203
- try:
204
- townships = self.get_mapping_object(township_name, endpoint)
205
- except IntegrationMappingException as exc:
206
- raise TownshipException(
207
- params={"type": ErrorType.township.value,
208
- "remote_code": exc.params['code'],
209
- "city_id": city.pk,
210
- "name": township_name,
211
- "integration_type": INTEGRATION_TYPE,
212
- "err_code": "integrations_200_10_1",
213
- "err_desc": f"Address is not valid. channel: {self.integration.channel_id}"
214
- f"code: {exc.params['code']} "
215
- f"model: Township city id: {city.pk}"})
216
- return townships[0]
217
-
218
- def get_district(self, country: Country, city: City, township: Township,
219
- district_name: str) -> District:
220
- endpoint = ChannelDistrictEndpoint(channel_id=self.integration.channel_id)
221
-
222
- params = {"name__exact": district_name, "country": country.pk, "city": city.pk,
223
- "township": township.pk}
224
- districts = endpoint.list(params=params)
225
- if len(districts) == 1:
226
- return districts[0]
227
- try:
228
- districts = self.get_mapping_object(district_name, endpoint)
229
- except IntegrationMappingException as exc:
230
- raise DistrictException(
231
- params={"type": ErrorType.district.value,
232
- "remote_code": exc.params['code'],
233
- "city_id": city.pk,
234
- "township_id": township.pk,
235
- "name": district_name,
236
- "integration_type": INTEGRATION_TYPE,
237
- "err_code": "integrations_200_10_3",
238
- "err_desc": f"Address is not valid. channel: {self.integration.channel_id}"
239
- f"code: {exc.params['code']} "
240
- f"model: District township id: {township.pk}"}
241
- )
242
- return districts[0]
@@ -1,40 +0,0 @@
1
- from typing import List
2
-
3
- from omnisdk.omnitron.endpoints import ChannelCargoEndpoint
4
- from omnisdk.omnitron.models import CargoCompany
5
-
6
- from channel_app.core.commands import OmnitronCommandInterface
7
-
8
-
9
- class GetCargoCompany(OmnitronCommandInterface):
10
- endpoint = ChannelCargoEndpoint
11
-
12
- def get_cargo_company(self, data):
13
- cargo_company_code = self.objects
14
- for cargo_company in data:
15
- if cargo_company.erp_code == cargo_company_code:
16
- return cargo_company
17
- raise Exception("CargoCompany does not exists: {}".format(
18
- cargo_company_code))
19
-
20
- def get_data(self) -> List[CargoCompany]:
21
- """
22
- :return: [
23
- {
24
- "pk": 1,
25
- "name": "Yurtici Kargo",
26
- "erp_code": "yurtici-kargo",
27
- "shipping_company": "yurtici"
28
- },
29
- ]
30
- """
31
- params = getattr(self, "param_{}".format("params"), {})
32
-
33
- end_point = self.endpoint(channel_id=self.integration.channel_id)
34
- cargo_companies = end_point.list(
35
- params=params)
36
- for next_cargo_companies in end_point.iterator:
37
- cargo_companies.extend(next_cargo_companies)
38
-
39
- cargo_company = self.get_cargo_company(data=cargo_companies)
40
- return [cargo_company]
@@ -1,72 +0,0 @@
1
- from dataclasses import asdict
2
- from omnisdk.omnitron.endpoints import ChannelCustomerEndpoint
3
- from omnisdk.omnitron.models import Customer
4
-
5
- from channel_app.core.commands import OmnitronCommandInterface
6
- from channel_app.core.data import CustomerDto
7
- from channel_app.omnitron.constants import CustomerIdentifierField
8
-
9
-
10
- class GetOrCreateCustomer(OmnitronCommandInterface):
11
- endpoint = ChannelCustomerEndpoint
12
-
13
- def get_data(self) -> Customer:
14
- data = self.objects
15
- data: CustomerDto
16
- return self.get_customer(data)
17
-
18
- def get_customer(self, data: CustomerDto) -> Customer:
19
- customer_identifier_field = self.integration.channel.conf.get(
20
- "CUSTOMER_IDENTIFIER_FIELD", CustomerIdentifierField.email)
21
- customers = None
22
- if customer_identifier_field == CustomerIdentifierField.email:
23
- customers = self.endpoint(channel_id=self.integration.channel_id).list(params={
24
- "email": data.email,
25
- })
26
- for c in customers:
27
- if c.email != data.email:
28
- raise Exception("Customer email filter incorrect")
29
- elif customer_identifier_field == CustomerIdentifierField.phone_number:
30
- # TODO phone_number filter does not exist on customerfilter
31
- customers = self.endpoint(channel_id=self.integration.channel_id).list(params={
32
- "phone_number": data.phone_number,
33
- })
34
- for c in customers:
35
- if c.phone_number != data.phone_number:
36
- raise Exception("Customer phone_number filter incorrect")
37
-
38
- if customers:
39
- customer = customers[0]
40
- must_update = False
41
- new_customer = Customer()
42
- new_customer.channel = customer.channel
43
- new_customer.channel_code = customer.channel_code
44
- if "email" in asdict(data) and data.email != customer.email:
45
- must_update = True
46
- new_customer.email = data.email
47
- if "phone_number" in asdict(data) and data.phone_number != customer.phone_number:
48
- must_update = True
49
- new_customer.phone_number = data.phone_number
50
- if "first_name" in asdict(data) and data.first_name != customer.first_name:
51
- must_update = True
52
- new_customer.first_name = data.first_name
53
- if "last_name" in asdict(data) and data.last_name != customer.last_name:
54
- must_update = True
55
- new_customer.first_name = data.last_name
56
- if must_update:
57
- customer = self.endpoint(channel_id=self.integration.channel_id).update(
58
- id=customer.pk, item=customer)
59
- else:
60
- new_customer = Customer()
61
- new_customer.channel = self.integration.channel_id
62
- new_customer.email = data.email
63
- new_customer.phone_number = data.phone_number
64
- new_customer.first_name = data.first_name
65
- new_customer.last_name = data.last_name
66
- new_customer.extra_field = data.extra_field or {}
67
- new_customer.channel_code = data.channel_code
68
- new_customer.is_active = data.is_active or True
69
- new_customer = self.endpoint(channel_id=self.integration.channel_id).create(
70
- item=new_customer)
71
- customer = new_customer
72
- return [customer]