unistra / python-glpi-api

Python module for interacting with GLPI using the API.
GNU General Public License v3.0
18 stars 10 forks source link

Search Criteria NOT, AND , OR #8

Closed jsalatiel closed 3 years ago

jsalatiel commented 3 years ago

Hi, I am having problems trying to make a search with multiple criterias. I have created a ticket using this api and if I do a simple search by name using the following criteria I can get the result as expected: criteria = [ {'field': 'name' , 'searchtype': 'contains', 'value': "^" + ticketName + "$" }

[
  {
    "2": 2021070035,
    "1": "serverName - disk failure",
    "80": "ORG",
    "15": "2021-07-14 11:07:41",
    "12": 1,
    "7": null,
    "4": "4785",
    "5": null,
    "19": "2021-07-14 11:07:41"
  }
]

But I can not find a way to search for tickets with that name and status not '5' ( solved ). First I tried the following: criteria = [ {'field': 'name' , 'searchtype': 'contains', 'value': "^" + ticketName + "$" } ,{'field': 'status' , 'searchtype': 'notequals', 'value': "5" } ] but it returned empty.

Then I tried criteria = [ {'field': 'name' , 'searchtype': 'contains', 'value': "^" + subject + "$" } , { 'link': 'NOT', 'criteria': [ {'field': 'status' , 'searchtype': 'equals', 'value': "5" } ] } ] Also empty.

What am I doing wrong ?

fmenabe commented 3 years ago

The notequals does not work because it is apparently not supported by the status field (don't ask me why :wink:):

>>> glpi.list_search_options('Ticket')['12']
{'name': 'Status',
 'table': 'glpi_tickets',
 'field': 'status',
 ...
 'available_searchtypes': ['equals']}

Your second try does not work because NOT is not a valid link, from the search documentation:

link: (optional for 1st element) logical operator in [AND, OR, AND NOT, AND NOT].

Note there's a typo as they are two 'AND NOT' and no 'OR NOT'.

When the link if not valid, it seems to fallback to an AND so you get the contrary of what you want. A quick test on my debug instance confirms it (I have 12 tickets but only one solved):

>>> len(glpi.search('Ticket', criteria=[{'link': 'NOT', 'field': 'status', 'searchtype': 'equals', 'value': '5'}]))
1

>>> len(glpi.search('Ticket', criteria=[{'link': 'AND NOT', 'field': 'status', 'searchtype': 'equals', 'value': '5'}]))
11
jsalatiel commented 3 years ago

Thank you