def validate_populate_imports(request, records): """ Return list of errors, imports and forms :request: request obj :records: a list of csv row data :return: errors, imports, and forms for template rendering """ errors, imports, forms = ([], [], []) for record in records: schema_dict = { 'name': record['schema_name'], 'title': record['schema_title'], 'publish_date': record['publish_date'].strftime('%Y-%m-%d') } FormForm = FormFormFactory(context=None, request=request) form_form = FormForm.from_json(schema_dict) if not form_form.validate(): schema_error = {} schema_error['errors'] = wtferrors(form_form) schema_error['schema_name'] = schema_dict['name'] schema_error['schema_title'] = schema_dict['title'] schema_error['name'] = 'N/A' schema_error['title'] = 'N/A' errors.append(schema_error) else: schema = datastore.Schema.from_json(schema_dict) if schema.to_json() not in forms: forms.append(schema.to_json()) choices = parse.get_choices(record['choices']) # below needed because we are calling from_json on record record['choices'] = choices FieldForm = FieldFormFactory(context=schema, request=request) form = FieldForm.from_json(record) if not form.validate(): output = log_errors(wtferrors(form), record) errors.append(output) else: imports.append((datastore.Attribute( name=record['name'], title=record['title'], description=record['description'], is_required=record['is_required'], is_collection=record['is_collection'], is_private=record['is_private'], type=record['type'], order=record['order'], choices=choices ), schema)) return errors, imports, forms
def test_get_choices(): raw_choices = [[u'0', u'label'], [u'1', u'label2']] choices = parse.get_choices(raw_choices) assert choices['0'].name == u'0' assert choices['0'].title == u'label' assert choices['0'].order == 0 assert choices['1'].name == u'1' assert choices['1'].title == u'label2' assert choices['1'].order == 1