Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask Basics Character Builder Cookies

Why is stuff "obliterated"?

In the video Kenneth says he knows some stuff will be completely obliterated below and then he modifies the save(). I don't understand what and why it will be obliterated. Can someone explain that bit?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,995 Points

The save is sending cookie information back to the browser. The issue is what happens if the cookie being held by the browser already has some data in the json object stored in that cookie? If the response ignores this pre-existing cookie data, it will get overwritten ("obliterated") by the new cookie in the response.

Kenneth's solution is to extract the existing cookie info from the request, update only the parts that have changed via the form, then send the updated cookie info back in the response.

This is a Read-Modify-Write flow that is typically used when updating partial information.

Nice concise answer Chris.

Thanks for this!

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,797 Points

I wonder though what I need to do to reproduce this obiliteration. Just so I can understand what's going on. I tested out the cookie data before applying the changes Kenneth made and didn't have any problems with the key value pairs not changing or losing that data. (I've since updated the code to the state at the end of the video.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,995 Points

Hey Jonathan Grieve, great followup question! In the current code set, "obliteration" will not happen since the form contains the entire data set from name to all the accessories. This means if no saved data was used, it would still work since the form is keeping all data alive.

I modified the code to verify all the data from the saved fields is present in the form data. The form data effectively overwrites all of the saved data, so a merge with saved, is moot.

saved data from incoming cookie:  
   {'name': '', 'shirts': '', 'pants': '', 'hat': '', 'glasses': '', 'footwear': ''}
data submitted from form items:  
   {'name': '', 'shirts': '', 'pants': '', 'hat': '', 'glasses': '', 'footwear': ''}

Now if some of the data on the page was saved in cookies but not covered by the form, than that data would get obliterated.

It's possible to contrive an example from the current code:

Again, under the current code, if accessories are chosen on the builder.html page and "update" is selected, all data is kept in the cookies. Even if you go back in history to the index.html page, then hit 'Let's build it" to restart, all previously selected accessories are remembered. However, if you add the line below to the saved() function, it will ignore the saved data:

    # ...
    data = get_saved_data()
    data = {}  # ADD THIS LINE to ignore saved data  
    data.update(dict(request.form.items()))
    response.set_cookie('character', json.dumps(data))
    return response

Now, visit the builder page to update accessories, followed by going back to the index page, and hitting "Let's build it". This time all of the previously selected accessories on the builder page will have been forgotten. This is because the index.html page only updates the name field. Since the accessory data wasn't moved from saved data to the "returning cookie", the builder only has the "name" passed through.

I wish all of that would be in the Video, so far this is the most unclear tutorial on TreeHouse what I have seen. I am like reading topics here on the forum to understand what is going on in Videos.