An AI assistant wrote GET /orders?since=… for the shop: the orders placed on or after a date. Customers type the date day first, like 18/04/2026. The database keeps each order's date year first, like 2026-04-18. The AI compared the two texts as they are:
@app.get("/orders")
def orders_since():
since = request.args.get("since")
rows = db.execute("SELECT * FROM orders").fetchall()
result = []
for row in rows:
if row["ordered"] >= since:
result.append(dict(row))
return jsonify(result)
Python compares strings character by character, and the first difference decides. "2026-03-02" >= "18/04/2026" is True, because "2" comes after "1". So since=18/04/2026 answered all 7 orders and since=30/04/2026 answered none. The impossible 31/04/2026 got 200 and an empty list, and a request without since crashed with 500.
The fix: turn each text into a real date with datetime.strptime(text, format), then compare dates. In a format, %d is the day, %m the month and %Y the four-digit year. A text that doesn't fit the format, or a day that doesn't exist, raises a ValueError:
from datetime import datetime
d = datetime.strptime("07/05/2026", "%d/%m/%Y")
print(d.day, d.month, d.year) # 7 5 2026
datetime.strptime("30/02/2026", "%d/%m/%Y") # ValueError: there is no 30 February
The requests and the answers your route must give:
GET /orders?since=18/04/2026 → 200 [{"customer_id": 3, "id": 4, "ordered": "2026-04-18", "status": "shipped"}, …] (orders 4 to 7)
GET /orders?since=30/04/2026 → 200 [{"customer_id": 4, "id": 5, "ordered": "2026-05-07", "status": "paid"}, …] (orders 5 to 7)
GET /orders?since=01/06/2026 → 200 []
GET /orders?since=31/04/2026 → 400 {"error": "since must be a date like 18/04/2026"}
GET /orders → 400 {"error": "since must be a date like 18/04/2026"}
Your task: rewrite orders_since. Read since as a date with the format "%d/%m/%Y", and answer 400 with the error when it isn't one. Read each order's ordered with "%Y-%m-%d", and answer the orders placed on or after since.
Assignment
Follow the Coding Tutorial to rewrite orders_since so it compares real dates instead of texts.
Hint
Put strptime inside try: and catch except ValueError:. Read since with the default "", so a missing date lands in the same except. Use >= so an order placed on the since day itself is kept.
Free plan: 30 tutor messages. PRO is unlimited.
