def db_create_field(store, field_dict, language): """ Create and add a new field to the store, then return the new serialized object. :param store: the store on which perform queries. :param field_dict: the field definition dict :param language: the language of the field definition dict :return: a serialization of the object """ fill_localized_keys(field_dict, models.Field.localized_keys, language) field = models.Field(field_dict) if field_dict['template_id'] != '': field.template_id = field_dict['template_id'] if field_dict['step_id'] != '': field.step_id = field_dict['step_id'] if field_dict['fieldgroup_id'] != '': ancestors = set(fieldtree_ancestors(store, field_dict['fieldgroup_id'])) if field.id == field_dict['fieldgroup_id'] or field.id in ancestors: raise errors.InvalidInputFormat( "Provided field association would cause recursion loop") field.fieldgroup_id = field_dict['fieldgroup_id'] store.add(field) if field.template: # special handling of the whistleblower_identity field if field.template.key == 'whistleblower_identity': if field.step: if not field.step.questionnaire.enable_whistleblower_identity: field.step.questionnaire.enable_whistleblower_identity = True else: raise errors.InvalidInputFormat( "Whistleblower identity field already present") else: raise errors.InvalidInputFormat( "Cannot associate whistleblower identity field to a fieldgroup" ) else: db_update_fieldattrs(store, field.id, field_dict['attrs'], language) db_update_fieldoptions(store, field.id, field_dict['options'], language) for c in field_dict['children']: c['fieldgroup_id'] = field.id db_create_field(store, c, language) return field
def create_dummy_field(self, store, **custom_attrs): field = get_dummy_field() fill_localized_keys(field, models.Field.localized_keys, 'en') field.update(custom_attrs) f = models.Field(field) store.add(f) return f.id