In this class, we built a simple shopping site -- minus the payment part, and other pieces -- simply in web2py.
The site contains a main page, where a list of products is given.
Logged-in users can buy products (they are asked to log in if they press the BUY button), and they can see the list of their orders. They can reorder, or delete orders. The store owner can see the list of products and orders, and update both.
This is really a very simple site, but the lecture covers this very useful material (follow links for the documentation):
All the code is available in the 2018-10-24 branch; in particular, here are things you should really look at in detail:
- If you use the right validators, you get drop-down menus in your forms!
- This is how we create a SQLFORM.grid form
- We can add links or arbitrary content to the table. Every link is a dictionary, with a header (used as header for the table column) and a body. The body is a lambda function (an unnamed function) that takes as argument the table row, and computes whatever you want to display in the table. This is an extremely powerful way of adding information to tables.
- validate_purchase is a function that, given a product, returns a validator for the form. The validator is the function f that is defined, and then returned. This is an extremely powerful way of doing validation: in the validator, you can check whatever you want and you have access to all you need (database, etc); if you set some error fields of a form, then web2py assumes that the form has errors, and what follows in the accepted: test is not executed.
- Note how you can use variables (here, one named q) to provide e.g. default values for a form. Note that I could not have used "quantity" as the variable name; that name is already used by the form.
- In the buy and cancel_order controllers, I use a Transaction object to ensure that when someone places or cancels an order, the changes to the product and order tables are either both carried out, or none of them is. I define the Transaction object in a model. Unfortunately, how to do transactions is dependent on the database; I wrote code for mysql and sqlite. Some databases have no useful notion of transaction (e.g., Google Datastore).