April 26, 2016 4:47 am

Python: A shareable Todo lists/notes web app with web2py Part 2

In web2py, controllers are located in web2py_folder/applications/YOUR_APPLICATION_FOLDER/controllers. If your application is called bars and you create a new .py file called snickers.py and add a function in it called view there, this would equal to the following path /bars/snickers/view.Learning Python a shareable Todo lists-notes web application with web2py

We will create a controller called default.py and add our functions/route methods there.

Here is how you can import modules in Python (we import the randint function from the random module).

In web2py, we use the following statement to connect to the MySQL database test with a user called root and empty password:

Part 1: Python: A shareable Todo lists/notes web app with web2py Part 1

Then, we can use the define_table method to create a database table if it does not exist. For our notes app, we create a todos table with two strings columns – item and identifier.

We create our app’s landing route:

In it, we set the session cookie to expire after 5 years so that users’ note/todo lists will remain active for longer.

If the user has not been generated an identifier associated with his todo list, we create it. It is just a random string of letters and numbers.

Then, if there are notes saved in the session we set userNotes (that variable will get sent to the view) to them. Notes saved in the session will consist of notes from some specific user identifier and not the user’s own identifier. Otherwise, we just get the user own note list from the database.

Finally, we return so that the view gets displayed and pass the notes to the view.

The function that adds notes is quite simple. It checks if there is a note to add in POST known as todo_item. If there is, it adds the note along with the user’s identifier to the database table and redirects to our home view.

The function for removing notes checks if a note has been chosen for removal. If it has been chosen, it checks if the user has his/her own identifier (if the user does not have one, we cannot really delete anything). Next, it checks if the user is viewing someone else’s todo list. If he is viewing someone else’s to do list, we redirect as he cannot delete someone else’s note. Thereafter, we try to delete the note with the text passed to POST that belongs to the user with the specific identifier and redirects.

Our final function checks if there is a POST property called the_key. If there is none, we just redirect as the user has not entered an identifier whose todo list to see. Thereafter, we try to get the notes of the user with the inputted identifier. If there are no notes returned from the database query, we inform the user that the identifier is invalid. Otherwise, we set the notes to the session (which will be displayed to the user in our index controller’s function), set the viewing session property to contain the identifier whose notes the user is about to see and redirect.

Finally, we have the my function which gets triggered when the user clicks on “My Todo List” in the navigation. When he/she clicks there, we just clear the session properties that are used when viewing someone else’s todo list and redirect to the index view.

And, voilà, we have a nice todo List app and can start creating better and more complicated web apps in Python.

Tutorial Categories:

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *