cityofaustin / knackpy

A Python client for interacting with Knack applications
https://cityofaustin.github.io/knackpy/docs/user-guide/
Other
39 stars 17 forks source link

Issue with get function when Knack fields updated as connection fields #88

Closed RoseaghTerrins closed 3 years ago

RoseaghTerrins commented 3 years ago

See code snipped of function below -

I am trying to perform the get function on object_1 in my knackpy app - it was working successfully and I was able to retrieve the records and transform into a dictionary. Since then, an update to my knackpy app has been made where some fields are now connections - the error message I am getting is - TypeError: string indices must be integers

# take note of the record id associated with each job id (field 6 in knack) in a json format
def add_id_to_log(new_data):
    app = knackpy.App(app_id=settings.APP_ID, api_key=settings.API_KEY, tzinfo="GB")
    get_record = app.get("object_1")
    for i in range(len(get_record)):
        data = dict(get_record[i])
        if data['field_6'] == new_data['field_6']:
            new_id = {data['id']: data['field_6']}
            with open("log.json", "r+") as jsonFile:
                data = json.load(jsonFile)
                data.update(new_id)
                jsonFile.seek(0) 
                json.dump(data, jsonFile)
                jsonFile.truncate()
                jsonFile.close()
        else:
            pass
RoseaghTerrins commented 3 years ago

*sorry knack app

johnclary commented 3 years ago

@RoseaghTerrins this should help. The trick is to use record.format(keys=False) to convert the record to a dictionary and format the connection values as strings.

Beyond this, I'd need you to tell me which line of code is throwing an error for you.

# take note of the record id associated with each job id (field 6 in knack) in a json format
def add_id_to_log(new_data):
    app = knackpy.App(app_id=settings.APP_ID, api_key=settings.API_KEY, tzinfo="GB")
    get_records = app.get("object_1")  # get_records is a list of knackpy Records
    for record in get_records:  # iterate through all records. 
        record = record.format(keys=False)  # convert the record to a dict. connections will be formatted to strings.
        if record['field_6'] == new_data['field_6']:
            new_id = {record['id']: record['field_6']}
            with open("log.json", "r") as jsonFile:
                data = json.load(jsonFile)  # note the variable name. `data` is already in use
            data.update(new_id)
            with open("log.json", "w") as jsonFile:
                json.dump(data, jsonFile)
        else:
            pass

Your approach to updating the json file looks it could have unexpected results. I don't fully understand the intent, but in my code i suggest opening the log.json as read only to load the data, and separately open the file in overwrite mode to write the updated data. You should make a backup of log.json before run this code in case it's not the behavior you're looking for.

Hope that helps.

johnclary commented 3 years ago

One more comment: I'm assuming that new_data is a dictionary, but if it is a list you would run into a TypeError when you call new_data['field_6'].