Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions examples/simple_orm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/python3

from yeti_switch_api.orm import OrmClient
from yeti_switch_api.orm import Contractor
from yeti_switch_api.orm import OrmClient, Contractor
from yeti_switch_api.orm.billing import Invoice
from yeti_switch_api.orm.system import SmtpConnection

Expand Down Expand Up @@ -51,6 +50,7 @@
"name": "test_python",
"enabled": True,
"customer": True,
"external-id": 100123,
}
# relationships={
# "smtp-connection": {
Expand All @@ -61,13 +61,15 @@
# }
# },
)
print("new_contractor before create", new_contractor.raw_object)
new_contractor.smtp_connection = found_smtp_connections[0]
print("new_contractor before create", new_contractor.raw_object_for_create())
new_contractor.create()
print("new_contractor after create", new_contractor.raw_object)

print("new_contractor before update", new_contractor.raw_object)
new_contractor.external_id = 100124
new_contractor.vendor = True
new_contractor.description = "test"
print("new_contractor before update", new_contractor.raw_object_for_update())
new_contractor.update()
print("new_contractor after update", new_contractor.raw_object)

Expand Down
12 changes: 6 additions & 6 deletions yeti_switch_api/orm/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def build(cls, id=None, attributes=None, relationships=None):

def create(self):
api_response = self._options.api.endpoint(self.endpoint_path()).post(
object=self.__raw_object_for_create()
object=self.raw_object_for_create()
)
if api_response.status_code == 201:
self.raw_object = api_response.content.data

def update(self):
api_response = self.endpoint.patch(object=self.__raw_object_for_update())
api_response = self.endpoint.patch(object=self.raw_object_for_update())
if api_response.status_code == 200 and api_response.content.data:
self.raw_object = api_response.content.data

Expand All @@ -47,13 +47,13 @@ def creatable_fields(self):
def updatable_fields(self):
return self.__class__._options.fields.keys()

def __raw_object_for_update(self):
def raw_object_for_update(self):
updatable_fields = self.updatable_fields()
attributes = {
k: v for k, v in self.raw_object.attributes.items() if k in updatable_fields
}
relationships = {
k: v
k: v.as_data()
for k, v in self.raw_object.relationships.items()
if k in updatable_fields
}
Expand All @@ -64,13 +64,13 @@ def __raw_object_for_update(self):
relationships=relationships,
)

def __raw_object_for_create(self):
def raw_object_for_create(self):
creatable_fields = self.creatable_fields()
attributes = {
k: v for k, v in self.raw_object.attributes.items() if k in creatable_fields
}
relationships = {
k: v
k: v.as_data()
for k, v in self.raw_object.relationships.items()
if k in creatable_fields
}
Expand Down
4 changes: 2 additions & 2 deletions yeti_switch_api/orm/contractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def creatable_fields(self):
"description",
"address",
"phones",
"external_id",
"smtp_connection",
"external-id",
"smtp-connection",
]

def updatable_fields(self):
Expand Down