Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inventree Project Code API bugs with both api and api endpoints #246

Closed
2 of 6 tasks
Eddalius opened this issue Oct 11, 2024 · 7 comments · Fixed by #247
Closed
2 of 6 tasks

Inventree Project Code API bugs with both api and api endpoints #246

Eddalius opened this issue Oct 11, 2024 · 7 comments · Fixed by #247
Labels
bug Something isn't working question Further information is requested

Comments

@Eddalius
Copy link

Please verify that this bug has NOT been raised before.

  • I checked and didn't find a similar issue

Describe the bug*

Unsure if this issue should be raised here or within the python app github but seemingly applies to an issue with both, so I'll raise it here.

When you attempt to use the python API to search the endpoints of the inventree instance for project codes, it errors if it is not a project code that exists. The project code is referenced by the primary key and not the project code itself, so if you were to search the purchase orders for all orders with a project code of 14, it'd error unless a project code had a primary key of 14 (which might not align with the naming scheme of your project codes).

I've checked the api endpoints and can see that the project code information is all there but cannot figure out a way to search it that doesn't throw an error.

Two parts to this bug:
Searching via purchase_order_list = PurchaseOrder.list(api, project_code=14) will return an error if a project code with the primary key of 14 does not exist

Secondly, it'll return the wrong project code if a primary key of 14 exists and isn't the correct project code.

Steps to Reproduce

  1. Auth with the API (I've been using the python interface for this, specifically: api = InvenTreeAPI(http://aninventree.instance.local:80, username=' ', password=' ') , with all relevant information obscured)
  2. Using the python app, attempt to search for a project code that doesn't exist (will error instead of returning none, odd behaviour, imo: purchase_order_list = PurchaseOrder.list(api, has_project_code=True, project_code=14) returns project_code":["Select a valid choice. That choice is not one of the available choices.)
  3. Create a project code (done via the web interface, made a project code with the code 14)
  4. Attempt to search project code via the code you setup (same input as step 3, same return as step 3)
  5. Attempt to search project code via primary key of the project code (purchase_order_list = PurchaseOrder.list(api, has_project_code=True, project_code=5), succeeded as my project code has a primary key of 5, can be seen via the api endpoint)

Expected behaviour

I expect to receive an empty list when searching for project codes and none are found. I also expect to be searching the project_code field itself when searching project_code=14 rather than the foreign key field for the primary key of the project code instead.

Deployment Method

  • Docker
  • Package
  • Bare metal
  • Other - added info in Steps to Reproduce

Version Information

Inventree version: 0.16.4
API version: 232

Please verify if you can reproduce this bug on the demo site.

  • I can reproduce this bug on the demo site.

Relevant log output

No response

@Eddalius Eddalius added bug Something isn't working question Further information is requested labels Oct 11, 2024
@SchrodingersGat
Copy link
Member

I expect to receive an empty list when searching for project codes and none are found.

We can certainly implement this, and return a [] value if an error is thrown while calling .list(). However, the error should still be logged / reported.

I also expect to be searching the project_code field itself when searching project_code=14 rather than the foreign key field for the primary key of the project code instead.

All "foreign key" fields in the API use the primary key to lookup, that is not going to change. You need to know ahead of time what the primary key value is

@Eddalius
Copy link
Author

Eddalius commented Oct 11, 2024

Yeah for some reason when specifically searching via the project code it throws an error and only wants to search primary keys. I'm currently trying to find a way around it but I'm not sure if it is a skill issue on my part or not.

p_code = ProjectCode(api, project_code=project_code)
Traceback (most recent call last):
  File "C:\Projects\...\job_costing\venv\lib\site-packages\IPython\core\interactiveshell.py", line 3505, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-44-c639136328c6>", line 1, in <module>
    p_code = ProjectCode(api, project_code=project_code)
TypeError: InventreeObject.__init__() got an unexpected keyword argument 'project_code'

p_code = ProjectCode(api, code=project_code)
Traceback (most recent call last):
  File "C:\Projects\...\job_costing\venv\lib\site-packages\IPython\core\interactiveshell.py", line 3505, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-46-c7a709e3a72f>", line 1, in <module>
    p_code = ProjectCode(api, code=project_code)
TypeError: InventreeObject.__init__() got an unexpected keyword argument 'code'

Checking the api endpoint it shows [{"pk":5,"code":"14","description":"","responsible":null,"responsible_detail":null}]

My alternate plan was to search the project code endpoint for the project code object, return the primary key and then filter by the primary key but that still runs into the error of

purchase_order_list = PurchaseOrder.list(api, has_project_code=True, project_code=10)
Traceback (most recent call last):
  File "C:\Projects\...\job_costing\venv\lib\site-packages\IPython\core\interactiveshell.py", line 3505, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-47-fdb0fd60af37>", line 1, in <module>
    purchase_order_list = PurchaseOrder.list(api, has_project_code=True, project_code=10)
  File "C:\Projects\...\job_costing\venv\lib\site-packages\inventree\base.py", line 234, in list
    response = api.get(url=url, params=kwargs)
  File "C:\Projects\...\job_costing\venv\lib\site-packages\inventree\api.py", line 530, in get
    response = self.request(url, method='get', **kwargs)
  File "C:\Projects\...\job_costing\venv\lib\site-packages\inventree\api.py", line 378, in request
    raise requests.exceptions.HTTPError(detail)
requests.exceptions.HTTPError: {'detail': 'Error occurred during API request', 'url': 'http://inventree.instance.local:80/api/order/po/', 'method': 'GET', 'status_code': 400, 'body': '{"project_code":["Select a valid choice. That choice is not one of the available choices."]}', 'headers': {'AUTHORIZATION': 'Token inv-****'}, 'params': {'has_project_code': True, 'project_code': 10}}

To be clear, project_code is the only field I've had this issue with, others seem to work just fine.
Where a project_code of 10 doesn't exist but a project_code of 5 does and returns a successful result.

I'm trying to interlink my app with the inventree app and I need a way to retrieve purchase orders linked to a specific project, if I need to make two requests: one to find the primary key and one to then retrieve the purchase orders, that's fine. But it looks like I can't search project codes like I figured I'd be able to?

Is there currently a way to find the primary key easily from the project_code.code itself? I am needing to do this remotely, ideally, although I can do some dodgy stuff if desperately needed.

EDIT:

All "foreign key" fields in the API use the primary key to lookup, that is not going to change. You need to know ahead of time what the primary key value is

Yeah ran into this myself on my own project, had to completely change how all my models worked because most of the additional apps I was using only played nicely with primary key lookups, even though I would have much preferred to lookup a different field.

EDIT:

Just realised worst comes to worst I can just return a list of length one via p_code = ProjectCode.list(api, code=project_code) and then pull the primary key from that, a bit finicky but good enough for what I need to do. Only potential issue is situations where project codes end up being similar (albeit, this is unlikely) and the implementation breaks because of this

@matmair
Copy link
Member

matmair commented Oct 15, 2024

just trying to understand this issue: is there an actual bug and what would be the expected fix or can we close this?

@Eddalius
Copy link
Author

I expect to receive an empty list when searching for project codes and none are found.

We can certainly implement this, and return a [] value if an error is thrown while calling .list(). However, the error should still be logged / reported.

This fix is ideal and to be clearer, the error I received when trying to do this was:

purchase_order_list = PurchaseOrder.list(api, has_project_code=True, project_code=10)
Traceback (most recent call last):
  File "C:\Projects\...\job_costing\venv\lib\site-packages\IPython\core\interactiveshell.py", line 3505, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-47-fdb0fd60af37>", line 1, in <module>
    purchase_order_list = PurchaseOrder.list(api, has_project_code=True, project_code=10)
  File "C:\Projects\...\job_costing\venv\lib\site-packages\inventree\base.py", line 234, in list
    response = api.get(url=url, params=kwargs)
  File "C:\Projects\...\job_costing\venv\lib\site-packages\inventree\api.py", line 530, in get
    response = self.request(url, method='get', **kwargs)
  File "C:\Projects\...\job_costing\venv\lib\site-packages\inventree\api.py", line 378, in request
    raise requests.exceptions.HTTPError(detail)
requests.exceptions.HTTPError: {'detail': 'Error occurred during API request', 'url': 'http://inventree.instance.local:80/api/order/po/', 'method': 'GET', 'status_code': 400, 'body': '{"project_code":["Select a valid choice. That choice is not one of the available choices."]}', 'headers': {'AUTHORIZATION': 'Token inv-****'}, 'params': {'has_project_code': True, 'project_code': 10}}

has_project_code didn't matter for the error or not. Realised in hindsight the second issue I can work around and that'll be fine

@Eddalius
Copy link
Author

Eddalius commented Oct 16, 2024

I also expect to be searching the project_code field itself when searching project_code=14 rather than the foreign key field for the primary key of the project code instead.

All "foreign key" fields in the API use the primary key to lookup, that is not going to change. You need to know ahead of time what the primary key value is

Is it possible for project_code and description to be added to the fields that you can look for via the project_code_list function of the api?

For some context, I won't be creating every single one of the project codes myself and rather some will be made to cover some partially legacy jobs. I won't have the primary keys for these, at least not initially, so I'll need to fetch it. There is some chance that this causes some clunky overlap but project codes only have a generic search and not a field restricted search. There would also be some usefulness in being able to search specifically descriptions for similar projects in the future, externally.

@SchrodingersGat SchrodingersGat transferred this issue from inventree/InvenTree Oct 16, 2024
@SchrodingersGat
Copy link
Member

@Eddalius FYI I have moved this issue from /inventree/ to /inventree-python/ repo

@SchrodingersGat
Copy link
Member

You can use the ?search=<xxx> to look for a particular project code currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants