Choosers that need the requesting user

Sometimes you require the current user to determine the choices for a field.

A tested document

This is a tested document. The following instructions are used for initialization:

>>> from lino import startup
>>> startup('lino_book.projects.team.settings.doctests')
>>> from lino.api.doctest import *

If your chooser method needs to know the current user to determine the choices for a field, include a "ar" parameter to your chooser method:

@dd.chooser()
def food_choices(cls, ar):
    year_in_school = ar.get_user().year_in_school if ar is not None else None
    food = []
    for name, reserved_for in MENU:
        if (year_in_school is None) or (reserved_for is None) or year_in_school in reserved_for:
            food.append(name)
    return food

For example the chooser for the lino_xl.lib.tickets.Ticket.site field wants to know who is asking before deciding which sites to display, because not everybody can see every site.

>>> url = '/choices/tickets/Ticket/site'
>>> show_choices("robin", url) 
<br/>
pypi
>>> show_choices("luc", url) 
<br/>
welket
docs