MCS 275 Spring 2023
Emily Dumas
Reminders and announcements:
This is the last in our contiguous lecture series focused on writing a Flask+SQLite application.
(We may revisit this topic a bit in the last week.)
createdb.py
is now a module, not just a script.
It has functions to create or clear the tables, and to add sample data.
It also now sets the database filename used everywhere.
Main web app script imports it and uses it to make sure database exists before proceeding.
Added timefmt.py
, module defining:
ts_fmt(x)
- Given a timestamp x
, return a string like "12:53pm on April 28, 2023"
tsdiff_fmt(t)
- Given a number of seconds t
since something happened, return a string like "38 seconds ago"
or "94 days ago"
etc.
Worker view uses this to display times. Entries in the list are sorted by how long they've been in their current state (oldest first).
Now /wo/5/
gives you a page with status info about work order 5.
New template, new route in the main python program.
If the work order is assigned, this page links to the worker view for the assigned worker.
We redirect from /new/submit
to the status page.
But how?
We just INSERT
ed it so we don't know the woid
.
After a single-row INSERT
, how to get the primary key of the new row?
SELECT last_insert_rowid();
Implicitly refers to the most-recently executed INSERT
on this connection.
Any time a work order number appears on the worker view, the number itself is a link to the associated status page.
Larger text entry box for the description. No CSS needed!
The size
attribute of an input
of type "text"
sets the width in characters.
/worker/<name>/
- (GET) worker's view of orders/new/
- (GET) form for new order/new/submit/
- (POST) form submission destination/wo/<int:woid>/
- (GET) work order status/wo/<int:woid>/assign_to/<name>/
- (GET*) take assignment/wo/<int:woid>/cancel/<name>/
- (GET*) cancel assignment/wo/<int:woid>/complete/<name>/
- (GET*) mark complete* These should really be POST but we would need to use javascript or a different button markup to do it.
Natural things we could do, but won't:
We perform database queries. They might fail for some reason and raise an exception.
But we also expect some of these queries to change exactly one row. We don't check that.
An UPDATE
might match any number of rows (e.g. 0, 1, 50). The query
SELECT changes();
returns the number of rows changed by the last UPDATE
on this connection.
Right now, our templates contain explicit reference to the URLs our application uses.
These are also declared in the Python source (@app.route(...)
).
It would be nice to have them in only one place, for ease of change or maintenance.
flask.url_for(function_name)
- convert a function name into the URL that triggers it.
flask.abort(http_error_code)
- Immediately stop and return a HTTP error code (usually 400 bad request, 401 not authorized, 403 forbidden, or 404 not found).
Some of the things you'd do differently in a "real" application: