Skip to content

Ordering Meals

jsem-nerad edited this page Nov 5, 2026 · 4 revisions

Ordering Meals

The basics

strava.menu.fetch()
result = strava.menu.order_meals(5, 9)

print(result)           # "2 changed"
print(result.changed)   # (5, 9)
strava.menu.cancel_meals(5, 9)

Both take any number of meal ids or keys, and both return an OrderResult.

Meal ids come from the API's veta field and are only valid until the canteen next renumbers the menu, which it does at every login and whenever the session is reset. A rollback resets it. After a reset a number you read earlier is refused with MenuNotFetchedError until you call fetch(), because it could be another meal by then. meal.key ("2026-09-21:O1") keeps working, and order_meals() and cancel_meals() accept it in place of an id. Results name each meal the way you did.

How a transaction works

Ordering is not a single request. The library:

  1. Checks locally that every id exists and that the canteen allows the change. A problem here means nothing is sent at all.
  2. Stages each change with one pridejJidloS5 request per meal.
  3. Saves all of them with a single saveOrders request.
  4. Re-fetches the menu and confirms each meal really changed, looking it up by its key.

What a failure leaves behind depends on where it happens:

step that fails what is on the account what you get
staging, by default nothing changed; staged changes are discarded the original error
staging, continue_on_error=True the other meals are saved the meal in failed, or in changed if the stage reached the canteen and only its answer was lost
the canteen refuses the save nothing changed the refusal, saying "Nothing was saved"
the save's answer never arrives read back to find out a normal result if it landed, otherwise the error saying "Nothing was saved"
...and it can't be read back either unknown OrderNotConfirmedError
reading back after a good save probably saved OrderNotConfirmedError saying the canteen accepted it

OrderNotConfirmedError carries .changed and .unconfirmed, and is raised with continue_on_error=True as well, because it means the whole batch is in doubt. Don't treat it as a failed order and retry blindly: fetch the menu and look.

Nothing to change means nothing is sent. Ordering a meal you already ordered, or calling order_meals() with no arguments, performs no API request at all:

result = strava.menu.order_meals(5)   # 5 was already ordered
print(result.unchanged)               # (5,)
print(result.changed)                 # ()

The OrderResult object

attribute type meaning
changed tuple[int | str, ...] Meals whose state was successfully changed
unchanged tuple[int | str, ...] Meals that already had the wanted state
skipped tuple[tuple[int | str, str], ...] (meal, reason) for deliberately skipped meals
failed tuple[tuple[int | str, Exception], ...] (meal, error) for meals the canteen refused
ok bool True when nothing failed

It is falsy when something failed, so this reads naturally:

result = strava.menu.order_meals(5, 9, continue_on_error=True)

if not result:
    for meal_id, error in result.failed:
        print(f"meal {meal_id}: {error}")

Failing fast, or carrying on

By default the first problem raises and nothing is saved:

from strava_cz import MealNotOrderableError

try:
    strava.menu.order_meals(5, 85, 9)
except MealNotOrderableError as exc:
    print(f"meal {exc.meal_id}: {exc.reason.description}")
    # nothing was ordered - 5 and 9 included

With continue_on_error=True nothing is raised. Everything that can be ordered is, and the rest is reported:

result = strava.menu.order_meals(5, 85, 999999, continue_on_error=True)

print(result.changed)   # (5,)          ordered
print(result.failed)    # ((85, MealNotOrderableError(...)),
                        #  (999999, MealNotFoundError(...)))
print(result.ok)        # False

This is the mode to use in a script that orders a whole week and should not give up because one day is already past its deadline.

Duplicates

Two meals of the same kind on the same day compete for one slot — ordering both would just make the second replace the first. By default the library keeps the first and warns:

import warnings

with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    result = strava.menu.order_meals(9, 10)   # both are "Oběd" on the same day

print(result.changed)   # (9,)
print(result.skipped)   # ((10, 'meal 10 competes with meal 9 for the same slot ...'),)

With strict_duplicates=True it is an error instead, and nothing is sent:

from strava_cz import DuplicateMealError

try:
    strava.menu.order_meals(9, 10, strict_duplicates=True)
except DuplicateMealError as exc:
    print(exc)

Meals of different kinds, or on different days, are never treated as duplicates — a soup and a main dish on the same day do not conflict.

Repeated ids are collapsed silently: order_meals(5, 5, 5) sends one request, and so does one meal named by both its id and its key.

Balance

The balance is refreshed from the API after every operation:

print(strava.user.balance)          # 512.5
strava.menu.order_meals(5)
print(strava.user.balance)          # 462.5

It is always a float, so you can do arithmetic on it directly.

If the account cannot cover an order the canteen refuses it and the library raises InsufficientBalanceError, rolling the transaction back. This isn't only a prepaid problem: an account invoiced monthly that sits at 0.00 has no headroom either, and was seen refusing a meal on an empty day with code 35, "Zákaz přihlášení pod limit konta". Swapping one meal for another nets to zero and goes through, and cancelling a meal frees its price for another order.

from strava_cz import InsufficientBalanceError

try:
    strava.menu.order_meals(5)
except InsufficientBalanceError as exc:
    print(f"{exc.message} - balance is {strava.user.balance} Kč")

Verification

The canteen's response to saveOrders is not proof that anything changed, so the library re-fetches the menu and checks. A meal the check doesn't find raises, naming whatever did change:

OrderNotConfirmedError: The canteen accepted the request but meal 5 is still not ordered

There is no need to call fetch() yourself afterwards — the menu is already current.

By default this reads the login's own copy of the orders, which already shows staged changes, so it can't catch a save that quietly did nothing. verify_saved=True resets the session first and reads the canteen's records:

result = strava.menu.order_meals("2026-09-21:O2", verify_saved=True)

It costs one more request and renumbers the menu, so numbers you read earlier are refused until the next fetch(). That is why it isn't the default in the library. Keys aren't affected, and the strava-cz command always verifies this way.

Ordering a whole week

from strava_cz import MealType

strava.menu.fetch()

wanted = []
for day in strava.menu.get_days(date_from="2026-09-07", date_to="2026-09-11"):
    if day.ordered:
        continue
    choice = next((m for m in day.orderable if m.type is MealType.MAIN), None)
    if choice:
        wanted.append(choice.id)

result = strava.menu.order_meals(*wanted, continue_on_error=True)
print(f"Ordered {len(result.changed)} lunches")
for meal_id, error in result.failed:
    print(f"  meal {meal_id} failed: {error}")

Cancelling

cancel_meals() mirrors order_meals(), minus the duplicate handling:

ordered = strava.menu.get_meals(ordered=True)
strava.menu.cancel_meals(*[m.id for m in ordered], continue_on_error=True)

Cancelling something that is not ordered is a no-op reported in unchanged, not an error. Cancelling an id that does not exist raises MealNotFoundError — earlier versions reported success for it, which hid stale ids.

Whether a meal can be cancelled is a separate question from whether it can be ordered: once the deadline passes, can_cancel becomes False too.

if meal.can_cancel:
    strava.menu.cancel_meals(meal.id)
else:
    print(meal.cancel_restriction.description)

Clone this wiki locally