Choosers that need the requesting user

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

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

>>> from lino import startup
>>> startup('lino_book.projects.noi1e.settings.demo')
>>> 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
aab
>>> show_choices("luc", url) 
<br/>
welket
docs
bcc

Some special cases

Asking choices for a field that doesn't exist:

>>> show_choices("robin", '/choices/tickets/Ticket/foo') 
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'blank'

Asking choices for a field that exists but has no choices:

>>> show_choices("robin", '/choices/tickets/Ticket/summary') 
Traceback (most recent call last):
...
Exception: Response status (/choices/tickets/Ticket/summary) was 404 instead of 200