示例#1
0
    def save_options(self, options):
        '''Persists specific field properties.'''
        self.save_option('default', options['defaul'])  # the default value

        for key in ('list_type', 'multiple_choice', 'sort_choices',
                    'new_option_label',
                    'min_num',  # minimum number of choices
                    'max_num',  # maximum number of choices
                    'size_options',     # number of choices
                    'moderated',  # other moderated
                    'new_option',  # possible to add a new option
                    'case_sensitive',  # other case sensitive
                    'opt_restrictions', # restricted number of options
                   # 'export_in_columns',  # when creating a CSV
                   ):
            self.save_option(key, options[key])

        inserted_options = {}
        for option_id, opt in options['options'].items():
            if opt['option_id'] != 'new':
                lo = sas.query(ListOption).get(opt['option_id'])
                lo.label = opt['label']
                lo.value = opt['value']
                lo.opt_default = opt['opt_default']
                lo.position = opt['position']
                # lo.status = opt['status']
                # To prevent KeyError, Nando changed the above line to:
                lo.status = opt.get('status', 'Form owner')
            else:
                lo = ListOption()
                lo.label = opt['label']
                lo.value = opt['value']
                lo.opt_default = opt['opt_default']
                lo.field = self.field
                lo.position = opt['position']
                lo.status = 'Form owner'
                sas.add(lo)
                sas.flush()
                inserted_options[option_id] = lo.id

        # Delete options
        for list_option_id in options['deleteOptions']:
            lo = sas.query(ListOption).get(list_option_id)
            if lo:
                sas.delete(lo)
        return {'insertedOptions': inserted_options}