Skip to content

Commit 23ef0c0

Browse files
Merge pull request #7 from yeti-switch/fix-contractor-creatable-fields
fix orm assign relationship, fix contractor relationships
2 parents 598fa63 + edb6dba commit 23ef0c0

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

examples/simple_orm.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/python3
22

3-
from yeti_switch_api.orm import OrmClient
4-
from yeti_switch_api.orm import Contractor
3+
from yeti_switch_api.orm import OrmClient, Contractor
54
from yeti_switch_api.orm.billing import Invoice
65
from yeti_switch_api.orm.system import SmtpConnection
76

@@ -51,6 +50,7 @@
5150
"name": "test_python",
5251
"enabled": True,
5352
"customer": True,
53+
"external-id": 100123,
5454
}
5555
# relationships={
5656
# "smtp-connection": {
@@ -61,13 +61,15 @@
6161
# }
6262
# },
6363
)
64-
print("new_contractor before create", new_contractor.raw_object)
64+
new_contractor.smtp_connection = found_smtp_connections[0]
65+
print("new_contractor before create", new_contractor.raw_object_for_create())
6566
new_contractor.create()
6667
print("new_contractor after create", new_contractor.raw_object)
6768

68-
print("new_contractor before update", new_contractor.raw_object)
69+
new_contractor.external_id = 100124
6970
new_contractor.vendor = True
7071
new_contractor.description = "test"
72+
print("new_contractor before update", new_contractor.raw_object_for_update())
7173
new_contractor.update()
7274
print("new_contractor after update", new_contractor.raw_object)
7375

yeti_switch_api/orm/base_model.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ def build(cls, id=None, attributes=None, relationships=None):
3131

3232
def create(self):
3333
api_response = self._options.api.endpoint(self.endpoint_path()).post(
34-
object=self.__raw_object_for_create()
34+
object=self.raw_object_for_create()
3535
)
3636
if api_response.status_code == 201:
3737
self.raw_object = api_response.content.data
3838

3939
def update(self):
40-
api_response = self.endpoint.patch(object=self.__raw_object_for_update())
40+
api_response = self.endpoint.patch(object=self.raw_object_for_update())
4141
if api_response.status_code == 200 and api_response.content.data:
4242
self.raw_object = api_response.content.data
4343

@@ -47,13 +47,13 @@ def creatable_fields(self):
4747
def updatable_fields(self):
4848
return self.__class__._options.fields.keys()
4949

50-
def __raw_object_for_update(self):
50+
def raw_object_for_update(self):
5151
updatable_fields = self.updatable_fields()
5252
attributes = {
5353
k: v for k, v in self.raw_object.attributes.items() if k in updatable_fields
5454
}
5555
relationships = {
56-
k: v
56+
k: v.as_data()
5757
for k, v in self.raw_object.relationships.items()
5858
if k in updatable_fields
5959
}
@@ -64,13 +64,13 @@ def __raw_object_for_update(self):
6464
relationships=relationships,
6565
)
6666

67-
def __raw_object_for_create(self):
67+
def raw_object_for_create(self):
6868
creatable_fields = self.creatable_fields()
6969
attributes = {
7070
k: v for k, v in self.raw_object.attributes.items() if k in creatable_fields
7171
}
7272
relationships = {
73-
k: v
73+
k: v.as_data()
7474
for k, v in self.raw_object.relationships.items()
7575
if k in creatable_fields
7676
}

yeti_switch_api/orm/contractor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def creatable_fields(self):
2525
"description",
2626
"address",
2727
"phones",
28-
"external_id",
29-
"smtp_connection",
28+
"external-id",
29+
"smtp-connection",
3030
]
3131

3232
def updatable_fields(self):

0 commit comments

Comments
 (0)