Project 1 of 9, for Caitlin
Berth Booking Web App
Caitlin's first real client: visitor berth bookings for a marina on the river, as a Flask web app on your own computer, with agreed routes, your own tests, and a handover the marina can run.
Act 9 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- CaitlinI've got my first real client, a marina on the river with five visitor berths, all booked in pencil.
- CaitlinPencil runs in the rain, it turns out. They've given me five requirements, and the fifth is surviving a switch-off.
- RiyaWho's going to use it? Skippers on a wet pontoon and the berth master at a desk, so design for both.
- PaulThis one's yours from the very first line. Plan it, test it and hand it over properly.
Requirements, from the marina, through Caitlin. Visiting boats book one of the five visitor berths in berths.py, for a number of nights.
- A skipper can book a berth before arriving, from a web page or through the API.
- The berth master can see who is in every berth on any night.
- Two boats are never booked into one berth on the same night.
- Nothing is lost when the server is switched off.
- A booking number, once given, is never given to another boat.
The agreed routes. The tests in tests/ check every one:
GET /api/berths: 200 and a list of the berths, each like{"id": 1, "name": "Pontoon A", "max_length": 8.0}GET /api/bookings: 200 and every booking, oldest first. With?date=2027-04-11, only the boats staying that night.POST /api/bookings, sent as JSON withboat,length(metres),berth(its id),date(the night of arrival,YYYY-MM-DD) andnights(1 if left out): 201 and the booking, with itsidGET /api/bookings/<id>: 200 and that bookingDELETE /api/bookings/<id>: 204, with nothing elseGET /: a web page listing every berth, and who is in each on the night given by?date=
When something is wrong, the answer is JSON, {"error": "what went wrong, in words"}, with this status:
- 400 for anything wrong with what was sent: not JSON, no boat name, a length that isn't a number above 0, a berth that doesn't exist or is too short for the boat, a date that isn't a real
YYYY-MM-DD, or nights that aren't a whole number from 1 to 14 - 404 for a booking that doesn't exist
- 409 for a berth already taken on any of those nights
Boat names are tidied: mary rose is Mary Rose. A boat staying two nights from the 10th leaves on the morning of the 12th, so another can arrive that day.
Two parts. Here in the workshop, the marina's rules go in rules.py, with no Flask in it, and are checked. Then on your own computer, the Flask app around them: the routes, the bookings kept in a file, the pages and your own tests, over four milestones.
$ flask --app app run
* Running on http://127.0.0.1:5000
$ curl -X POST http://127.0.0.1:5000/api/bookings -H "Content-Type: application/json" -d '{"boat": "kittiwake", "length": 9.5, "berth": 2, "date": "2027-04-10", "nights": 2}'
{"berth":2,"boat":"Kittiwake","date":"2027-04-10","id":1,"length":9.5,"nights":2}What's in it
- How a web app answers
- Routes in Flask
- The rules, before the routes
- A fresh app for every test
- The marina's project
- Milestone 1: the plan and the routes
- Keeping data between runs
- Milestone 2: the data, kept
- Pages and forms
- Milestone 3: the pages people use
- Milestone 4: tested, documented, handed over
- Ship it