Python: A shareable Todo lists/notes web app with web2py Part 2
Публикувано: 2018-09-29 17:44:34
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.
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).
Click To Expand Code
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
Click To Expand Code
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.
Click To Expand Code
We create our app’s landing route:
Click To Expand Code
In it, we set the session cookie to expire after 5 years so that users’ note/todo lists will remain active for longer.
Click To Expand Code
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.
Click To Expand Code
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.
Click To Expand Code
Finally, we return so that the view gets displayed and pass the notes to the view.
Click To Expand Code
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.
Click To Expand Code
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.
Click To Expand Code
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.
Click To Expand Code
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.
Click To Expand Code
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: https://www.dimoff.biz. facebook, twitter
Article source: https://www.phpgang.com/python-a-shareable-todo-listsnotes-web-app-with-web2py-part-2_3878.html