A plumber's morning
A plumber opens an app at 7:30. It shows today's visits in order. Tap one: the address, the phone number, what is wrong and what to bring.
At the end of the job, they tap "done" and write a note.
Netheret's Tier 2 and Tier 3 engineers asked for exactly this.
Key words
| Word | What it means | Example |
|---|---|---|
| Schedule | A list of jobs in time order. | |
| Job brief | Everything an engineer needs to know on site. | |
| Dictionary | A collection of keys, each with a value. | |
| Method | A named block of code that belongs to a class. | job.can_be_seen_by(user) |
| Permission | Whether a user is allowed to see or change something. |
The cells students fill in are left blank in this preview.
By the end of this lesson
Note
I can show each engineer only their own jobs, grouped by day.
Starter: what you already know
Have a go
Starter: what you already know
3 questionsSprint 2, step 9: 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.3 | As an engineer, I want a job brief, so that I arrive knowing what to do. |
| E3.4 | As an engineer, I want to see my own schedule, so that I can plan my day. |
| E3.5 | As an engineer, I want to mark a job's progress, so that the help desk knows it is done. |
The cells students fill in are left blank in this preview.
My schedule shows my jobs only
The query (a question about the stored data) asks for jobs where the engineer is the signed-in user, from today onwards, in time order. It also keeps an older job still marked in progress, so nothing is forgotten.
Then the jobs are grouped by day in a dictionary. Each key is a date. Each value is the list of jobs on that date. The page prints a heading for each day and its jobs below.
Step 9: the my schedule route
# helpdesk/jobs.py (part)
@bp.route("/jobs/mine")
@role_required(*ENGINEER_ROLES)
def my_schedule():
"""An engineer's own jobs from today onwards, grouped by day."""
start_of_today = datetime.combine(date.today(), datetime.min.time())
jobs = db.session.scalars(
db.select(Job)
.where(Job.engineer_id == g.user.id, Job.status != "cancelled")
# Also show older jobs that were started but never marked done.
.where((Job.scheduled_start >= start_of_today) | (Job.status == "in_progress"))
.order_by(Job.scheduled_start)
).all()
# Group the jobs by day: {date: [jobs on that date]}.
days = {}
for job in jobs:
days.setdefault(job.scheduled_start.date(), []).append(job)
return render_template("jobs/mine.html", days=days, today=date.today())
Make a free account to save your work and track your progress.
Try it: group jobs by day
# Each job: the day, the time and the title.
jobs = [
["Tue 17", "09:00", "Annual PC health check"],
["Tue 17", "13:30", "Replace card reader"],
["Thu 19", "10:00", "Router swap"],
]
days = {}
for job in jobs:
day = job[0]
if day not in days:
days[day] = [] # first job on this day: start an empty list
days[day].append(job[1] + " " + job[2])
for day in days:
print(day)
for line in days[day]:
print(" ", line)
# Add a job on "Fri 20" at "11:00", then press Run.
Make a free account to save your work and track your progress.
Check: grouping by day
Have a go
Check: grouping by day
3 questionsThe brief pulls data from three tables
The job brief page shows the job, and pulls in more. From the company: the address and phone number. From the request, if there is one: its reference and what the customer said.
This is the relationships from step 3 at work. job.company.address follows a foreign key (a link to a row in another table) with nothing extra to write.
Long text keeps its line breaks, so step-by-step instructions stay readable.
Worked example: one job brief
| Line on the page | Where it comes from |
|---|---|
| When: Tue 17 Mar, 09:00 to 10:00 (60 minutes) | Job: scheduled_start and duration_minutes |
| Customer: Harbour Dental Practice | Company, through job.company |
| Address and phone | Company: address and phone |
| Request NTH-0010: Backup failed overnight | Ticket, through job.ticket |
| What to do | Job: brief |
The cells students fill in are left blank in this preview.
Check: the job brief
Have a go
Check: the job brief
3 questionsThe job decides who may see it
Who may see or change a job is a rule about the job. So the rule is a method on the Job class.
Managers and Tier 1 see every job. An engineer sees only their own. Anyone else gets error 403.
Engineers may move a job to in progress or done. Only a manager may cancel one.
Step 9: permission methods on Job
# helpdesk/models.py, inside class Job
def can_be_seen_by(self, user):
"""Managers and Tier 1 see every job; an engineer sees their own."""
return user.role in ["manager", "tier1"] or self.engineer_id == user.id
def can_be_updated_by(self, user):
return user.role == "manager" or self.engineer_id == user.id
# helpdesk/jobs.py, inside brief(job_id)
job = db.get_or_404(Job, job_id)
if not job.can_be_seen_by(g.user):
abort(403)
# Engineers can move a job along; only a manager can cancel one.
statuses = JOB_STATUSES if g.user.role == "manager" else ["scheduled", "in_progress", "done"]
Make a free account to save your work and track your progress.
A common mistake
Warning
Many students check only that a user is an engineer. Then Sam can open Priya's job just by changing the number in the address. Check the record too: is this job really theirs? Every page that shows one row needs that check.
Match each user to what they may do
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Step 9: a finished job is noted
# helpdesk/jobs.py, inside progress(job_id) (part)
finished_now = new_status == "done" and job.status != "done"
job.status = new_status
job.notes = request.form.get("notes", "").strip()
if finished_now and job.ticket:
# Record the visit on the request's timeline for the help desk.
add_update(job.ticket, f"On-site job \"{job.title}\" done. Notes: {job.notes or 'none'}", False)
db.session.commit()
Make a free account to save your work and track your progress.
Build it yourself: step 9
Build step 9. 1. Add can_be_seen_by and can_be_updated_by to Job. 2. Add My schedule for engineers, grouped by day. 3. Add the job brief page with the company, the request and the brief. 4. Add a progress form: status and notes, with cancel for managers only, and a timeline note when a job is first marked done. 5. Test by hand as tier2: open your own job and then another engineer's job by its address. Try to cancel with a changed form value. 6. Commit and write the change log row. Then write a short Sprint 2 review: what was finished, and what moves to the next sprint.
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 9 code
Tip
Compare yours with the step 9 code on GitHub. Sign in as tier2 and tier3 and check each sees only their own jobs.
Change log entry for step 9
| Date | Version and files | What changed | Why |
|---|---|---|---|
| 12 March | step-09: jobs.py, models.py, jobs/mine.html, jobs/brief.html | My schedule, the job brief page, and progress with a note on the request when a job is done | Tier 2 and Tier 3 asked for their own schedules and job briefs |
The cells students fill in are left blank in this preview.
Sprint 2 done. Next: the knowledge base
Sprint 2 is finished: jobs are booked, briefed and tracked. Sprint 3 starts in step 10 with a knowledge base, so Tier 1 can fix more problems without passing them on.
Exit quiz
Have a go