0

This is my UI

End user can able to update Summary or my_story one at a time, This is my endpoint URL http:localhost:3000/api/account/profile, once user update any one of the field, the URL will work

This is Request payload for summary field

If the user update the Summary field the above endpoint URL will work.

This is Request payload for my_story field

If the user update the my_story field the above endpoint URL will work.

My code(Once the user update anyone of the field. I want to check which field is updated, for this how can I check whether the user is updated Summary or my_story,after accessing the field I want to sanitize the field and send it to response below one is my code):

from lxml.html import clean
def account_update():
    data = json.loads(request.data)
    cleaner = clean.Cleaner()
    if data['other_details']['summary']:
        clean_overview = (data['other_details']['summary'])
        sanitized_html = cleaner.clean_html(clean_overview)
    else:
        clean_overview = (data['other_details']['my_story'])
        sanitized_html = cleaner.clean_html(clean_overview)
    return jsonify({"account": data})

Guys , in the above code I am getting the request payload as data, after that I am accessing the summary and my_story fields as data['other_details']['summary'] and data['other_details']['summary'], here I wrote if condition to check if the user update summary field if condition will work suppose user update my_story field it will goes to else part, but in my case if I update my_story field getting error.

Error:
if data['other_details']['summary']:

KeyError: 'summary'

NOTE:

Sanitizing the field is working fine (I mean cleaner = clean.Cleaner() and sanitized_html = cleaner.clean_html(clean_overview), I just want to know which field the user is updating. Please help me guys.

saipy
  • 25
  • 6

2 Answers2

0

What i see your program just can't find summary atribute.

Solution 1

Im not sure this will work but function getattr() can help you. Change this line

data['other_details']['summary']

To this

getattr(getattr(data,'other_details'),'summary', False)

Here you can learn more about getattr() What is getattr() exactly and how do I use it?

Solution 2

Just use some try: and except:

Packman
  • 415
  • 4
  • 9
0

Most likely, the request.data doesn't always have the summary field in data['other_details'].

This means, you have to check for it in you if-else block before trying to access it.

Best to check for other_details as well.

Here is one way of doing this:

from lxml.html import clean

def account_update():
    data = json.loads(request.data)
    cleaner = clean.Cleaner()
    if 'other_details' in data:
        other_details = data['other_details']

        if 'summary' in other_details:
            clean_overview = other_details['summary']
            sanitized_html = cleaner.clean_html(clean_overview)

        elif 'my_story' in other_details:
            clean_overview = other_details['my_story']
            sanitized_html = cleaner.clean_html(clean_overview)

    return jsonify({"account": data})
Paul P
  • 3,346
  • 2
  • 12
  • 26