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
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: [3.8]
python-version: [3.11]

steps:
- name: Checkout Code
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: [3.8]
python-version: [3.11]

steps:
- name: Checkout Code
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pypi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.11
- name: Check Release Tag
run: |
python3 ci/check_version_number.py ${{ github.event.release.tag_name }}
Expand Down
36 changes: 13 additions & 23 deletions inventree/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, host=None, **kwargs):
if kwargs.get('connect', True):
self.connect()

def setHostName(self, host):
def setHostName(self, host: str):
"""Validate that the provided base URL is valid"""

if host is None:
Expand Down Expand Up @@ -136,7 +136,7 @@ def connect(self):
if not self.token:
self.requestToken()

def constructApiUrl(self, endpoint_url):
def constructApiUrl(self, endpoint_url: str) -> str:
"""Construct an API endpoint URL based on the provided API URL.

Arguments:
Expand All @@ -145,17 +145,7 @@ def constructApiUrl(self, endpoint_url):
Returns: A fully qualified URL for the subsequent request
"""

# Strip leading / character if provided
if endpoint_url.startswith("/"):
endpoint_url = endpoint_url[1:]

url = urljoin(self.api_url, endpoint_url)

# Ensure the API URL ends with a trailing slash
if not url.endswith('/'):
url += '/'

return url
return urljoin(self.api_url, endpoint_url)

def testAuth(self):
"""
Expand All @@ -169,7 +159,7 @@ def testAuth(self):
return False

try:
response = self.get('/user/me/')
response = self.get('user/me/')
except requests.exceptions.HTTPError as e:
logger.fatal(f"Authentication error: {str(type(e))}")
return False
Expand Down Expand Up @@ -254,7 +244,7 @@ def requestToken(self):
# Request an auth token from the server
try:
response = self.get(
'/user/token/',
'user/token/',
params={
'name': self.token_name,
}
Expand All @@ -273,14 +263,14 @@ def requestToken(self):

return self.token

def request(self, api_url, **kwargs):
def request(self, url: str, **kwargs):
""" Perform a URL request to the Inventree API """

if not self.connected:
# If we have not established a connection to the server yet, attempt now
self.connect()

api_url = self.constructApiUrl(api_url)
api_url = self.constructApiUrl(url)

data = kwargs.get('data', kwargs.get('json', {}))
files = kwargs.get('files', {})
Expand Down Expand Up @@ -401,7 +391,7 @@ def request(self, api_url, **kwargs):

return response

def delete(self, url, **kwargs):
def delete(self, url: str, **kwargs):
""" Perform a DELETE request. Used to remove a record in the database.

"""
Expand All @@ -420,7 +410,7 @@ def delete(self, url, **kwargs):

return response

def post(self, url, data, **kwargs):
def post(self, url: str, data: dict, **kwargs):
""" Perform a POST request. Used to create a new record in the database.

Args:
Expand Down Expand Up @@ -457,7 +447,7 @@ def post(self, url, data, **kwargs):

return data

def patch(self, url, data, **kwargs):
def patch(self, url: str, data: dict, **kwargs):
"""
Perform a PATCH request.

Expand Down Expand Up @@ -495,7 +485,7 @@ def patch(self, url, data, **kwargs):

return data

def put(self, url, data, **kwargs):
def put(self, url: str, data: dict, **kwargs):
"""
Perform a PUT request. Used to update existing records in the database.

Expand Down Expand Up @@ -531,7 +521,7 @@ def put(self, url, data, **kwargs):

return data

def get(self, url, **kwargs):
def get(self, url: str, **kwargs):
""" Perform a GET request.

For argument information, refer to the 'request' method
Expand Down Expand Up @@ -633,7 +623,7 @@ def scanBarcode(self, barcode_data):
barcode_data = json.dumps(barcode_data)

response = self.post(
'/barcode/',
'barcode/',
{
'barcode': str(barcode_data),
}
Expand Down
24 changes: 15 additions & 9 deletions inventree/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ class InventreeObject(object):
URL = ""

@classmethod
def get_url(cls, api):
def get_url(cls):
"""Helper method to get the URL associated with this model."""
return cls.URL

url = cls.URL

if not url.endswith('/'):
url += '/'

return url

# Minimum server version for the particular model type
MIN_API_VERSION = None
Expand Down Expand Up @@ -90,7 +96,7 @@ def __init__(self, api, pk=None, data=None):
if pk <= 0:
raise ValueError(f"Supplier <pk> value ({pk}) for {self.__class__} must be positive.")

url = self.get_url(api)
url = self.get_url()

self._url = f"{url}/{pk}/"
self._api = api
Expand Down Expand Up @@ -134,7 +140,7 @@ def options(cls, api):
cls.checkApiVersion(api)

response = api.request(
cls.URL,
cls.get_url(),
method='OPTIONS',
)

Expand Down Expand Up @@ -543,7 +549,7 @@ class MetadataMixin:
- The 'metadata' is not used for any InvenTree business logic
- Instead it can be used by plugins for storing arbitrary information
- Internally it is stored as a JSON database field
- Metadata is accessed via the API by appending '/metadata/' to the API URL
- Metadata is accessed via the API by appending './metadata/' to the API URL

Note: Requires server API version 49 or newer

Expand Down Expand Up @@ -668,7 +674,7 @@ def _statusupdate(self, status: str, reload=True, data=None, **kwargs):
raise ValueError(f"Order stats {status} not supported.")

# Set the url
URL = self.URL + f"/{self.pk}/{status}"
URL = self.URL + f"/{self.pk}/{status}/"

if data is None:
data = {}
Expand Down Expand Up @@ -718,7 +724,7 @@ def assignBarcode(self, barcode_data: str, reload=True):
model_type = self.barcodeModelType()

response = self._api.post(
'/barcode/link/',
'barcode/link/',
{
'barcode': barcode_data,
model_type: self.pk,
Expand All @@ -730,13 +736,13 @@ def assignBarcode(self, barcode_data: str, reload=True):

return response

def unassignBarcode(self, reload=True):
def unassignBarcode(self, reload: bool = True):
"""Unassign a barcode from this object"""

model_type = self.barcodeModelType()

response = self._api.post(
'/barcode/unlink/',
'barcode/unlink/',
{
model_type: self.pk,
}
Expand Down
2 changes: 1 addition & 1 deletion inventree/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Build(
):
""" Class representing the Build database model """

URL = 'build'
URL = 'build/'
MODEL_TYPE = 'build'

def issue(self):
Expand Down
10 changes: 5 additions & 5 deletions inventree/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Address(inventree.base.InventreeObject):
class Company(inventree.base.ImageMixin, inventree.base.MetadataMixin, inventree.base.InventreeObject):
""" Class representing the Company database model """

URL = 'company'
URL = 'company/'
MODEL_TYPE = "company"

def getContacts(self, **kwargs):
Expand Down Expand Up @@ -103,7 +103,7 @@ class SupplierPart(inventree.base.BarcodeMixin, inventree.base.BulkDeleteMixin,
- Implements the BulkDeleteMixin
"""

URL = 'company/part'
URL = 'company/part/'

def getPriceBreaks(self):
""" Get a list of price break objects for this SupplierPart """
Expand All @@ -122,7 +122,7 @@ class ManufacturerPart(
- Implements the BulkDeleteMixin
"""

URL = 'company/part/manufacturer'
URL = 'company/part/manufacturer/'
MODEL_TYPE = "manufacturerpart"

def getParameters(self, **kwargs):
Expand All @@ -139,10 +139,10 @@ class ManufacturerPartParameter(inventree.base.BulkDeleteMixin, inventree.base.I
- Implements the BulkDeleteMixin
"""

URL = 'company/part/manufacturer/parameter'
URL = 'company/part/manufacturer/parameter/'


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

URL = 'company/price-break'
URL = 'company/price-break/'
4 changes: 2 additions & 2 deletions inventree/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def saveOutput(self, output, filename):
def printLabel(self, template, plugin=None, destination=None, *args, **kwargs):
"""Print a label against the provided label template."""

print_url = '/label/print/'
print_url = 'label/print/'

template_id = self.getTemplateId(template)

Expand Down Expand Up @@ -161,7 +161,7 @@ def downloadTemplate(self, destination, overwrite=False):
class LabelTemplate(LabelFunctions):
"""Class representing the LabelTemplate database model."""

URL = 'label/template'
URL = 'label/template/'

def __str__(self):
"""String representation of the LabelTemplate instance."""
Expand Down
22 changes: 11 additions & 11 deletions inventree/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class PartCategoryParameterTemplate(inventree.base.InventreeObject):
"""A model which link a ParameterTemplate to a PartCategory"""

URL = 'part/category/parameters'
URL = 'part/category/parameters/'

def getCategory(self):
"""Return the referenced PartCategory instance"""
Expand All @@ -30,7 +30,7 @@ def getTemplate(self):
class PartCategory(inventree.base.MetadataMixin, inventree.base.InventreeObject):
""" Class representing the PartCategory database model """

URL = 'part/category'
URL = 'part/category/'

def getParts(self, **kwargs):
return Part.list(self._api, category=self.pk, **kwargs)
Expand Down Expand Up @@ -68,7 +68,7 @@ class Part(
):
""" Class representing the Part database model """

URL = 'part'
URL = 'part/'
MODEL_TYPE = 'part'

def getCategory(self):
Expand Down Expand Up @@ -149,7 +149,7 @@ def getRequirements(self):
class PartTestTemplate(inventree.base.MetadataMixin, inventree.base.InventreeObject):
""" Class representing a test template for a Part """

URL = 'part/test-template'
URL = 'part/test-template/'

@classmethod
def generateTestKey(cls, test_name):
Expand Down Expand Up @@ -183,7 +183,7 @@ class BomItem(
):
""" Class representing the BomItem database model """

URL = 'bom'
URL = 'bom/'


class BomItemSubstitute(
Expand All @@ -192,13 +192,13 @@ class BomItemSubstitute(
):
"""Class representing the BomItemSubstitute database model"""

URL = "bom/substitute"
URL = "bom/substitute/"


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

URL = 'part/internal-price'
URL = 'part/internal-price/'

@classmethod
def setInternalPrice(cls, api, part, quantity: int, price: float):
Expand All @@ -219,7 +219,7 @@ def setInternalPrice(cls, api, part, quantity: int, price: float):
class SalePrice(inventree.base.InventreeObject):
""" Class representing the SalePrice model """

URL = 'part/sale-price'
URL = 'part/sale-price/'

@classmethod
def setSalePrice(cls, api, part, quantity: int, price: float, price_currency: str):
Expand All @@ -241,7 +241,7 @@ def setSalePrice(cls, api, part, quantity: int, price: float, price_currency: st
class PartRelated(inventree.base.InventreeObject):
""" Class representing a relationship between parts"""

URL = 'part/related'
URL = 'part/related/'

@classmethod
def add_related(cls, api, part1, part2):
Expand All @@ -266,7 +266,7 @@ def add_related(cls, api, part1, part2):

class Parameter(inventree.base.InventreeObject):
"""class representing the Parameter database model """
URL = 'part/parameter'
URL = 'part/parameter/'

def getunits(self):
""" Get the units for this parameter """
Expand All @@ -277,4 +277,4 @@ def getunits(self):
class ParameterTemplate(inventree.base.InventreeObject):
""" class representing the Parameter Template database model"""

URL = 'part/parameter/template'
URL = 'part/parameter/template/'
Loading