Esempio n. 1
0
    def save_data(self, entry, value):
        list_type = self.field.get_option('list_type')

        if value:
            if value['option'] and value['option'] != c.null:
                if list_type != 'radio':
                    for opt in filter(lambda o: o != '', value['option']):
                        self.data = ListData()
                        # TODO: Check if is a valid value
                        self.data.value = opt
                        self.data.entry_id = entry.id
                        self.data.field_id = self.field.id
                        sas.add(self.data)
                else:
                    self.data = ListData()
                    # TODO: Check if is a valid value
                    self.data.value = value['option']
                    self.data.entry_id = entry.id
                    self.data.field_id = self.field.id
                    sas.add(self.data)

            if value.has_key('other') and value['other'] != '':
                moderated = self.field.get_option('moderated')
                case_sensitive = self.field.get_option('case_sensitive')

                if case_sensitive == 'true':
                    option = sas.query(ListOption) \
                                .filter(ListOption.label == value['other']) \
                                .filter(ListOption.field_id == self.field.id) \
                                .first()
                else:
                    option = sas.query(ListOption) \
                            .filter(ListOption.label.ilike(value['other'])) \
                            .filter(ListOption.field_id == self.field.id) \
                            .first()

                no_options = sas.query(ListOption) \
                        .filter(ListOption.field_id == self.field.id).count()

                if not option:
                    lo = ListOption()
                    lo.label = value['other']
                    lo.value = lo.label
                    lo.field = self.field
                    lo.position = no_options
                    lo.status = 'Approved' if moderated == 'false' \
                        else 'Awaiting moderation'
                    sas.add(lo)
                    sas.flush()
                else:
                    lo = option

                data = ListData()
                # TODO: Check if is a valid value
                data.value = lo.id
                data.entry_id = entry.id
                data.field_id = self.field.id
                sas.add(data)
Esempio n. 2
0
    def copy(self, base_field):
        '''Receives base_field and copies its specific options into self.field
        options.
        Do not copy options of the field_option model, just specific ones.
        '''
        # iterate over all list options
        for base_lo in base_field.list_options:
            # option instance copy
            lo_copy = ListOption()
            lo_copy.field = self.field
            for attr in ('label', 'value', 'opt_default', 'position', 'status'):
                lo_copy.__setattr__(attr, base_lo.__getattribute__(attr))

            sas.add(lo_copy)
Esempio n. 3
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}