One driver, two deliveries at once
A delivery firm books one van for a 10:00 drop in Bristol and a 10:30 drop in Bath. The first drop takes an hour.
Somebody will be waiting, and somebody will be cross.
Netheret books engineers for visits. The app must refuse a booking that puts one engineer in two places.
Key words
| Word | What it means | Example |
|---|---|---|
| Job | A visit by an engineer to a customer's premises. | |
| Overlap | When two time slots share some minutes. | |
| Clash | A booking that overlaps one the engineer already has. | |
| Property | A value a class works out when you ask for it. | job.scheduled_end |
| Defensive programming | Writing code that expects bad input and handles it. | |
| Function | A named block of code that does one job. |
The cells students fill in are left blank in this preview.
By the end of this lesson
Note
I can stop a booking that clashes with an engineer's other jobs.
Starter: what you already know
Have a go
Starter: what you already know
3 questionsSprint 2, step 8: the goal and stories
| Part | For this step |
|---|---|
| Sprint (a short block of work) | Sprint 2: plan on-site jobs and give engineers their schedules. |
| E3.1 | As a manager, I want to schedule a job, so that visits are planned. |
| E3.2 | As a manager, I want to assign an engineer to a job, so that I never double-book them. |
The cells students fill in are left blank in this preview.
Two jobs overlap if each starts first
Think of two jobs as two bars on a timeline. They overlap when each one starts before the other one ends.
Back to back is fine. A job from 9:00 to 10:00 and one from 10:00 to 11:00 do not clash.
A job stores its start and its length. A property called scheduled_end works out the end time whenever it is needed.
Worked example: clash or not?
| Booked job | New job | Each starts before the other ends? | Clash? |
|---|---|---|---|
| 9:00 to 10:00 | 9:30 to 10:30 | 9:00 is before 10:30, and 9:30 is before 10:00 | Yes |
| 9:00 to 10:00 | 10:00 to 11:00 | 10:00 is not before 10:00 | No |
| 9:00 to 10:00 | 8:30 to 9:30 | 9:00 is before 9:30, and 8:30 is before 10:00 | Yes |
| 10:00 to 10:30 | 9:00 to 12:00 | 10:00 is before 12:00, and 9:00 is before 10:30 | Yes |
The cells students fill in are left blank in this preview.
Check: overlaps
Have a go
Check: overlaps
3 questionsStep 8: find_clash
# helpdesk/models.py, inside class Job
@property
def scheduled_end(self):
return self.scheduled_start + timedelta(minutes=self.duration_minutes)
# helpdesk/jobs.py (part)
def find_clash(engineer_id, start, end):
"""Return a job that overlaps start to end for this engineer, or None.
Two jobs overlap when each one starts before the other one ends.
"""
jobs = db.session.scalars(
db.select(Job).where(Job.engineer_id == engineer_id, Job.status != "cancelled")
).all()
for job in jobs:
if job.scheduled_start < end and start < job.scheduled_end:
return job
return None
Make a free account to save your work and track your progress.
Times as minutes after midnight
To try the rule without dates, write each time as minutes after midnight. 9:00 is 9 lots of 60, so 540.
9:30 is 540 and 30 more, so 570. 10:00 is 600.
Then the rule is just two comparisons of whole numbers.
Your turn: fix the overlap check
This is the overlap rule from find_clash, with times as minutes after midnight. An AI coding assistant wrote the first version. Run it with the two examples. Then fix it so it uses the full rule: each job starts before the other ends. Back to back jobs must not clash. Write the test values you used in a small table: booked job, new job, expected, actual.
def jobs_overlap(start_a, minutes_a, start_b, minutes_b):
# Times are minutes after midnight, so 9:00 is 540.
# Give back True when the two jobs clash, False when they do not.
# An AI coding assistant wrote this first version. Run it, then fix it.
return start_a <= start_b <= start_a + minutes_a
print(jobs_overlap(540, 60, 600, 60)) # 9:00 for 1 hour, then 10:00: should be False
print(jobs_overlap(540, 60, 510, 60)) # 9:00, and 8:30 for 1 hour: should be True
Make a free account to save your work, or press Start lab to work through the whole lesson.
Check the form, then look for a clash
The job form is checked field by field, in the same order as the page, so the error summary reads top to bottom.
This is defensive programming. The length must be a whole number from 15 to 480 minutes. The start must be in the future. A chosen request must belong to the same company.
The clash check runs last, only when every value it needs is valid.
Step 8: the end of check_job_form
# helpdesk/jobs.py, inside check_job_form (part)
duration = form.get("duration_minutes", "")
if not duration.isdigit() or not 15 <= int(duration) <= 480:
errors["duration_minutes"] = "Enter how long the job takes, from 15 to 480 minutes."
values["duration"] = None
else:
values["duration"] = int(duration)
# Only look for a clash once everything it needs is valid.
if engineer and values["start"] and values["duration"] and not errors:
end = values["start"] + timedelta(minutes=values["duration"])
clash = find_clash(engineer.id, values["start"], end)
if clash:
errors["scheduled_start"] = (
f"{engineer.name} already has \"{clash.title}\" from "
f"{clash.scheduled_start:%H:%M} to {clash.scheduled_end:%H:%M} that day."
)
return errors, values
Make a free account to save your work and track your progress.
A common mistake
Warning
Many students think "starts inside the other job" is the whole clash rule. It misses a job that starts earlier and runs into it. It also misses a long job that covers a short one. Use the full rule: each job starts before the other ends.
Check: the job form
Have a go
Check: the job form
3 questionsA week view, and the customer is told
The week view finds the Monday of any date, then lists seven days with their jobs. Buttons move a week back or forward.
A request's page has a "Schedule a job" link that fills in the company and the title. When the job is saved, a customer-visible update goes on that request: an engineer will visit, with the day and time.
Try it: find the Monday
from datetime import date, timedelta
# weekday() is 0 on Monday and 6 on Sunday.
day = date(2026, 3, 12)
monday = day - timedelta(days=day.weekday())
print("The week of", day, "starts on", monday)
# The seven days of that week, as the week view lists them.
for number in range(7):
print(monday + timedelta(days=number))
# Change the date to date(2026, 3, 15), a Sunday, then press Run.
Make a free account to save your work and track your progress.
Build it yourself: step 8
Build step 8. 1. Add scheduled_end to Job. 2. Write find_clash and check_job_form, with the checks in the same order as the form. 3. Add a manager-only route to schedule a job, which tells the customer on the request's timeline. 4. Add a week view with previous and next week links. 5. Test by hand: book Sam 9:00 to 10:00, then try 9:30, then 10:00. Try a length of 600 and a start in the past. 6. Commit and write the change log row.
A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.
Make a free account to save your work, or press Start lab to work through the whole lesson.
Compare with the step 8 code
Tip
Compare yours with the step 8 code on GitHub. Look at jobs/week.html for how the seven days are drawn.
Change log entry for step 8
| Date | Version and files | What changed | Why |
|---|---|---|---|
| 11 March | step-08: jobs.py, models.py, jobs/new.html, jobs/week.html, tickets/detail.html | Managers schedule jobs with a double booking check, a week view, and a link from a request that tells the customer | The client plans and schedules on-site work |
The cells students fill in are left blank in this preview.
Next: my schedule and job briefs
Managers can book jobs. In step 9 the engineers get their side: their own schedule, a brief for each job and a way to mark progress.
Exit quiz
Have a go