Skip to content

Commit

Permalink
Merge pull request #150 from inventree/count
Browse files Browse the repository at this point in the history
Adds "count" functionality for each API endpoint
  • Loading branch information
SchrodingersGat committed Nov 11, 2022
2 parents e04fab5 + 1455cf8 commit d5cdeec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion inventree/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,22 @@ def create(cls, api, data, **kwargs):

return cls(api, data=response)

@classmethod
def count(cls, api, **kwargs):
"""Return a count of all items of this class in the database"""

params = kwargs

# By limiting to a single result, we perform a fast query, but get a total number of results
params['limit'] = 1

response = api.get(url=cls.URL, params=params, **kwargs)

return response['count']

@classmethod
def list(cls, api, **kwargs):
""" Return a list of all items in this class on the database.
"""Return a list of all items in this class on the database.
Requires:
Expand Down
3 changes: 3 additions & 0 deletions test/test_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def test_part_cats(self):
n = len(cats)
self.assertTrue(len(cats) >= 9)

# Check that the 'count' method returns the same result
self.assertEqual(n, PartCategory.count(self.api))

# Filtered categories must be fewer than *all* categories
cats = PartCategory.list(self.api, parent=1)

Expand Down

0 comments on commit d5cdeec

Please sign in to comment.