oTree Forum >

Problem with setting up attributes for Page models.

#1 by janohirmas

I created a set of checks for my pages (checking that participants are on fullscreen and that they don't go out of focus). 
For this I need to create a few variables for each page and also add them to each page's form_fields. I created the following function:
def addCheckVars(player, lPages, bNoCheck=False):

    # Go through all page objects in list
    for page in lPages:
        sName = (page.__name__)                     # get name
        # If form_model is player on page, add these variables
        lForm       = getattr(page,'form_fields',[]) # form fields
        sFormModel  = getattr(page,'form_model')  # form model
        # If default, set it to player
        if sFormModel == None:
            sFormModel = 'player'
            setattr(page, "form_model", 'player')

        if sFormModel == 'player':
            print(f"setting up player in page {sName}")
            for var in ['iFS','iFocus','dFocusTime']:
                # For each page, add the three variables that 
                setattr(player, f"{var}_{sName}", models.StringField(blank=bNoCheck))
                # Append variables to specific page
                lForm.append(f"{var}_{sName}")
            setattr(page,'form_fields',lForm)
            
Then I use this after page sequence as follows:

    addCheckVars(Player, page_sequence)

This code creates the variables and adds them to the respective forms. The problem is that if I have pages/apps that do not use this subroutine. I get errors, that they get some form_fields that don't exist.

     Exception: Page has form_fields but not form_model
 
Is there a problem with this function? Am I messing something internal that is creating this issue?

Write a reply

Set forum username