Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Task 2 A: Scheduling on-site jobs without clashes

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

75 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
JobA visit by an engineer to a customer's premises.
OverlapWhen two time slots share some minutes.
ClashA booking that overlaps one the engineer already has.
PropertyA value a class works out when you ask for it.job.scheduled_end
Defensive programmingWriting code that expects bad input and handles it.
FunctionA 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Sprint 2, step 8: the goal and stories

PartFor this step
Sprint (a short block of work)Sprint 2: plan on-site jobs and give engineers their schedules.
E3.1As a manager, I want to schedule a job, so that visits are planned.
E3.2As 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 jobNew jobEach starts before the other ends?Clash?
9:00 to 10:009:30 to 10:309:00 is before 10:30, and 9:30 is before 10:00Yes
9:00 to 10:0010:00 to 11:0010:00 is not before 10:00No
9:00 to 10:008:30 to 9:309:00 is before 9:30, and 8:30 is before 10:00Yes
10:00 to 10:309:00 to 12:0010:00 is before 12:00, and 9:00 is before 10:30Yes

The cells students fill in are left blank in this preview.

Check: overlaps

Have a go

Check: overlaps

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Step 8: find_clash

Code: python
# 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.

Code: python
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

Code: python
# 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Code: python
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

DateVersion and filesWhat changedWhy
11 Marchstep-08: jobs.py, models.py, jobs/new.html, jobs/week.html, tickets/detail.htmlManagers schedule jobs with a double booking check, a week view, and a link from a request that tells the customerThe 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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.