Who goes first at A and E?
In a hospital emergency room, people are not seen in the order they arrive. The most urgent go first.
A help desk is the same. A failed backup at a dental practice comes before a flickering screen.
Tier 1 needs one list of every open request, most urgent at the top, and a quick way to narrow it down.
Key words
| Word | What it means | Example |
|---|---|---|
| Database | An organised store of data, kept in tables. | |
| Query | A question you ask a database. | |
| SQL | The language used to ask a database questions. | |
| Filter | A rule that keeps only some rows. | priority = high |
| Query string | The part of a web address after the ?. | ?priority=high |
| Template filter | A small named piece of code that shows a value nicely in a template. |
The cells students fill in are left blank in this preview.
By the end of this lesson
Note
I can build a filtered, sorted list of records from the database.
Starter: what you already know
Have a go
Starter: what you already know
3 questionsSprint 1, step 6: the goal and stories
| Part | For this step |
|---|---|
| Sprint (a short block of work) | Sprint 1: people sign in and handle support requests. |
| E2.2 | As a Tier 1 agent, I want to triage new requests by priority, so that urgent ones are seen first. |
| Part of E2.5 | As staff, I want to find requests by status, tier and who has them, so that nothing is lost. |
The cells students fill in are left blank in this preview.
Each filter adds one condition
The queue starts with a query for every ticket. Each filter the user picks adds one more condition, called a WHERE.
The filters come from a GET form, so they end up in the address, such as /queue?priority=high. That means a filtered queue can be bookmarked or shared.
A value that is not on its list is simply ignored. A strange address never crashes the page.
Step 6: the queue route
# helpdesk/tickets.py (part)
@bp.route("/queue")
@role_required(*STAFF_ROLES)
def queue():
status = request.args.get("status", "active")
priority = request.args.get("priority", "")
tier = request.args.get("tier", "")
query = db.select(Ticket)
# Each filter adds one more WHERE condition to the query.
if status == "active":
query = query.where(Ticket.status.in_(["new", "open", "waiting"]))
elif status in STATUSES:
query = query.where(Ticket.status == status)
if priority in PRIORITIES:
query = query.where(Ticket.priority == priority)
if tier.isdigit() and int(tier) in TIERS:
query = query.where(Ticket.tier == int(tier))
# Most urgent first: CASE turns each priority into a number to sort by.
urgency = case({"critical": 0, "high": 1, "medium": 2, "low": 3}, value=Ticket.priority)
tickets = db.session.scalars(query.order_by(urgency, Ticket.created_at)).all()
return render_template("tickets/queue.html", tickets=tickets)
Make a free account to save your work and track your progress.
Check: filters
Have a go
Check: filters
3 questionsCASE puts the most urgent first
Sorting priorities by name would give critical, high, low, medium. That is alphabetical, not urgent.
So the query turns each priority into a number with CASE: critical is 0, high is 1, and so on. It sorts by that number, then by the date raised.
Flask-SQLAlchemy writes the SQL for you, but it helps to read what it sends.
Try it: the queue in SQL
-- The query the queue page sends, with no filters chosen.
SELECT reference, title, priority, tier
FROM ticket
WHERE status IN ('new', 'open', 'waiting')
ORDER BY CASE priority
WHEN 'critical' THEN 0
WHEN 'high' THEN 1
WHEN 'medium' THEN 2
ELSE 3
END,
created_at;
-- Try it: add the line AND tier = 2 just above ORDER BY, then press Run.
Make a free account to save your work and track your progress.
A common mistake
Warning
Many students think sorting by a text column always gives a sensible order. It gives alphabetical order. For priorities, statuses or sizes, alphabetical is usually wrong. Turn each value into a number first, as CASE does here, then sort by the number.
Check: sorting
Have a go
Check: sorting
3 questionsThe detail page and tidy dates
Each reference in the queue links to a detail page with everything about that request.
Dates are shown with a template filter. {{ ticket.created_at|datetime }} turns a date into "12 Nov 2026, 14:05". Every page uses the same filter, so dates always look the same.
The queue is a real HTML table with a caption and column headings, so a screen reader can read it.
Step 6: a template filter for dates
# helpdesk/filters.py (part)
def show_datetime(value):
"""12 Nov 2026, 14:05"""
if value is None:
return ""
return value.strftime("%d %b %Y, %H:%M")
def register(app):
# In a template: {{ ticket.created_at|datetime }}
app.add_template_filter(show_datetime, "datetime")
Make a free account to save your work and track your progress.
Match each part to its job
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Build it yourself: step 6
Build step 6. 1. Add a queue route for staff that reads status, priority and tier from the address and adds one WHERE for each valid one. 2. Sort by urgency with CASE, then by date. 3. Make queue.html with a GET filter form, a Clear filters link and a table with a caption. 4. Add a detail page and a date filter. 5. Test by hand: each filter alone, two together, and a nonsense value such as ?tier=9. Write the number of rows you expected and the number you got. 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 6 code
Tip
Compare yours with the step 6 code on GitHub. The real queue also filters by who a request is assigned to, including "me" and "nobody yet".
Change log entry for step 6
| Date | Version and files | What changed | Why |
|---|---|---|---|
| 9 March | step-06: tickets.py, filters.py, tickets/queue.html, tickets/detail.html | A staff queue with four filters, sorted by urgency, and a detail page | Tier 1 needs to see and triage what has come in |
The cells students fill in are left blank in this preview.
Next: act on a request
Staff can now find a request. In step 7 they act on it: assign it, escalate it to a higher tier and change its status. Every action goes on a timeline.
Exit quiz
Have a go