Paul's Python CoursePPC

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.

About 20 hours, in 13 steps.

Sign up free to open itLog in

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.

  1. A skipper can book a berth before arriving, from a web page or through the API.
  2. The berth master can see who is in every berth on any night.
  3. Two boats are never booked into one berth on the same night.
  4. Nothing is lost when the server is switched off.
  5. 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 with boat, length (metres), berth (its id), date (the night of arrival, YYYY-MM-DD) and nights (1 if left out): 201 and the booking, with its id
  • GET /api/bookings/<id>: 200 and that booking
  • DELETE /api/bookings/<id>: 204, with nothing else
  • GET /: 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.

What it prints
$ 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

  1. How a web app answers
  2. Routes in Flask
  3. The rules, before the routes
  4. A fresh app for every test
  5. The marina's project
  6. Milestone 1: the plan and the routes
  7. Keeping data between runs
  8. Milestone 2: the data, kept
  9. Pages and forms
  10. Milestone 3: the pages people use
  11. Milestone 4: tested, documented, handed over
  12. Ship it