server-client-python icon indicating copy to clipboard operation
server-client-python copied to clipboard

workbook filter fails with special characters.

Open VDFaller opened this issue 2 years ago • 6 comments

Describe the bug If my workbook has special character in it, my filter fails. I'm currently working around this but hoping that it can be fixed.

Versions Details of your environment, including:

  • Tableau online
  • Python version: 3.9.12
  • tableauserverclient==0.23.4

To Reproduce

    with server.auth.sign_in(tableau_auth):
        wbs = server.workbooks.all()
        wbs.filter(name="T(L-F,SZ&V-MY) - PC")
        next(iter(wbs_iter)) # this will fail

Results

ServerResponseError: 

	400065: Bad Request
		None

VDFaller avatar Feb 28 '23 21:02 VDFaller

Unfortunately you can't represent some characters in a search, but you can replace them with a wildcard. Specifically, I think it's "," and "&" that don't work. But this does:

` wb_name = "T(L-FSZV-MY) - PC"

    print("Searching for " + wb_name + "")

    for workbook in server.workbooks.filter(name=wb_name):

        print(workbook.name, workbook.id)

`

produces: Searching for T(L-F*SZ*V-MY) - PC T(L-F,SZ&V-MY) - PC b5d8b87f-3a3c-4787-83d4-6fd5cc2664db T(L-F^SZ|V-MY) - PC 44ea14d9-54e8-4b9f-9ec9-150fbea57b08

https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_concepts_filtering_and_sorting.htm#filter-expression-notes

jacalata avatar Mar 14 '23 21:03 jacalata

Should make sure our docs mention this limitation and workaround.

jacalata avatar Apr 12 '23 20:04 jacalata

So would it make sense to add a replace_illegal_chars argument, hopefuly something pithier, to filter? I could try it if I get a hint or something. There's not a lot of doc strings in this repo.

Maybe here?

something like

def filter(self, *invalid, **kwargs):
    if invalid:
        raise RuntimeError(f"Only accepts keyword arguments.")
    # special arg to allow for filtering on fields that contain illegal characters
    replace_illegal_characters = kwargs.pop("replace_illegal_characters", False)
    
    for kwarg_key, value in kwargs.items():
        field_name, operator = self._parse_shorthand_filter(kwarg_key)
        if replace_illegal_characters:
            value = self._replace_illegal_characters(value)
        self.request_options.filter.add(Filter(field_name, operator, value))
    return self

def _replace_illegal_characters(self, value):
    """helper function to replace illegal characters in a string value for filtering"""
    if isinstance(value, str):
        return value.replace(",", "*").replace('&', '*')
    return value

VDFaller avatar Apr 20 '23 01:04 VDFaller

Not sure if this applies here, but we've run into issues with commas in other queries. We got around them by replacing , with %5C%2C instead of just %2C. If I can figure out how to test a patch I may try it when I have some more time.

mtwalkup avatar Nov 16 '23 02:11 mtwalkup

Disregard my last comment, I tried to make it work and forced a \ (%5C) before the comma still got a 400 error.

However; just wanted to also note I am getting this same problem with a job filter.

Edit: comma instead of command

mtwalkup avatar Nov 17 '23 20:11 mtwalkup

We have a bunch of questions around filters, and will go through all of them to carefully document current behavior. that might turn into some work items if there are inconsistencies/gaps.

jacalata avatar Sep 20 '24 08:09 jacalata