Skip to content

Commit

Permalink
Return order support (#178)
Browse files Browse the repository at this point in the history
* Add API version check helper to base class

* Add ReturnOrder models

- Also split existing 'order.py' into separate files
- Add imports for backwards compatibility

* Add support for "Contact" model

* Add functions for returning company and contact associated with orders

* Update tasks

* Unit testing for new models

* Add test for Contact model

* PEP exceptions

* PEP fixes

* Come on!

* Fix for unit test
  • Loading branch information
SchrodingersGat committed Mar 29, 2023
1 parent bda9276 commit 7cce375
Show file tree
Hide file tree
Showing 10 changed files with 831 additions and 479 deletions.
31 changes: 31 additions & 0 deletions inventree/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
class InventreeObject(object):
""" Base class for an InvenTree object """

# API URL (required) for the particular model type
URL = ""

# Minimum server version for the particular model type
REQUIRED_API_VERSION = None

def __str__(self):
"""
Expand All @@ -35,6 +39,8 @@ def __init__(self, api, pk=None, data=None):
data - JSON representation of the object
"""

self.checkApiVersion(api)

# If the pk is not explicitly provided,
# extract it from the provided dataset
if pk is None and data:
Expand All @@ -61,6 +67,18 @@ def __init__(self, api, pk=None, data=None):
if len(self._data) == 0:
self.reload()

@classmethod
def checkApiVersion(cls, api):
"""Check if the API version supports this particular model.
Raises:
NotSupportedError if the server API version is too 'old'
"""

if cls.REQUIRED_API_VERSION is not None:
if cls.REQUIRED_API_VERSION > api.api_version:
raise NotImplementedError(f"Server API Version ({api.api_version}) is too old for the '{cls.__name__}' class, which requires API version {cls.REQUIRED_API_VERSION}")

@classmethod
def options(cls, api):
"""Perform an OPTIONS request for this model, to determine model information.
Expand All @@ -69,6 +87,8 @@ def options(cls, api):
This endpoint provides information on the various fields available for that endpoint.
"""

cls.checkApiVersion(api)

response = api.request(
cls.URL,
method='OPTIONS',
Expand Down Expand Up @@ -137,6 +157,8 @@ def pk(self):
def create(cls, api, data, **kwargs):
""" Create a new database object in this class. """

cls.checkApiVersion(api)

# Ensure the pk value is None so an existing object is not updated
if 'pk' in data.keys():
data.pop('pk')
Expand Down Expand Up @@ -170,6 +192,7 @@ def list(cls, api, **kwargs):
URL - Base URL
"""
cls.checkApiVersion(api)

# Check if custom URL is present in request arguments
if 'url' in kwargs:
Expand All @@ -195,13 +218,18 @@ def list(cls, api, **kwargs):

def delete(self):
""" Delete this object from the database """

self.checkApiVersion(self._api)

if self._api:
return self._api.delete(self._url)

def save(self, data=None, files=None, method='PATCH'):
"""
Save this object to the database
"""

self.checkApiVersion(self._api)

# If 'data' is not specified, then use *all* the data
if data is None:
Expand Down Expand Up @@ -251,6 +279,9 @@ def is_valid(self):

def reload(self):
""" Reload object data from the database """

self.checkApiVersion(self._api)

if self._api:
data = self._api.get(self._url)

Expand Down
26 changes: 24 additions & 2 deletions inventree/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@
logger = logging.getLogger('inventree')


class Contact(inventree.base.InventreeObject):
"""Class representing the Contact model"""

URL = 'company/contact/'
REQUIRED_API_VERSION = 104


class Company(inventree.base.ImageMixin, inventree.base.InventreeObject):
""" Class representing the Company database model """

URL = 'company'

def getContacts(self, **kwargs):
"""Return contacts associated with this Company"""
kwargs['company'] = self.pk
return Contact.list(self._api, **kwargs)

def getSuppliedParts(self, **kwargs):
"""
Return list of SupplierPart objects supplied by this Company
Expand All @@ -30,7 +42,7 @@ def getPurchaseOrders(self, **kwargs):
"""
Return list of PurchaseOrder objects associated with this company
"""
return inventree.order.PurchaseOrder.list(self._api, supplier=self.pk)
return inventree.order.PurchaseOrder.list(self._api, supplier=self.pk, **kwargs)

def createPurchaseOrder(self, **kwargs):
"""
Expand All @@ -48,7 +60,7 @@ def getSalesOrders(self, **kwargs):
"""
Return list of SalesOrder objects associated with this company
"""
return inventree.order.SalesOrder.list(self._api, customer=self.pk)
return inventree.order.SalesOrder.list(self._api, customer=self.pk, **kwargs)

def createSalesOrder(self, **kwargs):
"""
Expand All @@ -62,6 +74,16 @@ def createSalesOrder(self, **kwargs):
data=kwargs
)

def getReturnOrders(self, **kwargs):
"""Return list of ReturnOrder objects associated with this company"""
return inventree.order.ReturnOrder.list(self._api, customer=self.pk, **kwargs)

def createReturnOrder(self, **kwargs):
"""Create (and return) a new ReturnOrder against this company"""
kwargs['customer'] = self.pk

return inventree.order.ReturnOrder.create(self._api, data=kwargs)


class SupplierPart(inventree.base.BarcodeMixin, inventree.base.BulkDeleteMixin, inventree.base.InventreeObject):
"""Class representing the SupplierPart database model
Expand Down
Loading

0 comments on commit 7cce375

Please sign in to comment.