def test_converts_sheeted_xls(self): base_dir = os.path.join("dkobo", "koboform", "tests", "example_xls") with_accents = os.path.join(base_dir, "with_accents.xls") with_accents_csv = os.path.join(base_dir, "with_accents.csv") without_accents = os.path.join(base_dir, "without_accents.xls") without_accents_csv = os.path.join(base_dir, "without_accents.csv") with open(without_accents_csv, 'r') as ff: expected = ff.read() with open(without_accents, 'rb') as fff: out_csv = pyxform_utils.convert_xls_to_csv_string(fff) self.assertEqual(expected, out_csv) with open(with_accents_csv, 'rb') as ff: expected = ff.read() with open(with_accents, 'rb') as fff: out_csv = pyxform_utils.convert_xls_to_csv_string(fff) self.assertEqual(expected, out_csv)
def import_questions(request): """ Imports an XLS or CSV file into the user's SurveyDraft list. Returns an error in JSON if the survey was not valid. """ output = {} posted_file = request.FILES.get(u'files') response_code = 200 if posted_file: posted_file.seek(0) if posted_file.content_type in XLS_CONTENT_TYPES: imported_sheets_as_csv = pyxform_utils.convert_xls_to_csv_string(posted_file) elif posted_file.content_type == "text/csv": imported_sheets_as_csv = posted_file.read() else: raise Exception("Content-type not recognized: '%s'" % posted_file.content_type) split_surveys = xlform.split_apart_survey(imported_sheets_as_csv) new_survey_drafts = [] for _split_survey in split_surveys: sd = SurveyDraft(name='New Form', body=_split_survey[0], user=request.user, asset_type='question') sd._summarize() new_survey_drafts.append(sd) SurveyDraft.objects.bulk_create(new_survey_drafts) output[u'survey_draft_id'] = -1 else: response_code = 204 # Error 204: No input output[u'error'] = "No file posted" return HttpResponse(json.dumps(output), content_type="application/json", status=response_code)
def import_questions(request): """ Imports an XLS or CSV file into the user's SurveyDraft list. Returns an error in JSON if the survey was not valid. """ output = {} posted_file = request.FILES.get(u'files') response_code = 200 if posted_file: posted_file.seek(0) if posted_file.content_type in XLS_CONTENT_TYPES: imported_sheets_as_csv = pyxform_utils.convert_xls_to_csv_string(posted_file) elif posted_file.content_type == "text/csv": imported_sheets_as_csv = posted_file.read() else: raise Exception("Content-type not recognized: '%s'" % posted_file.content_type) split_surveys = xlform.split_apart_survey(imported_sheets_as_csv) new_survey_drafts = [] for _split_survey in split_surveys: sd = SurveyDraft(name='New Form', body=_split_survey[0], user=request.user, asset_type='question') sd._summarize() new_survey_drafts.append(sd) SurveyDraft.objects.bulk_create(new_survey_drafts) output[u'survey_draft_id'] = -1 else: response_code = 204 # Error 204: No input output[u'error'] = "No file posted" return HttpResponse(json.dumps(output), content_type="application/json", status=response_code)
}) output[u'survey_draft_id'] = new_survey_draft.id except Exception, err: response_code = 500 output[u'error'] = err.message or str(err) output[u'warnings'] = warnings else: try: # create and validate the xform but ignore the results warnings = [] pyxform_utils.validate_kobo_xlsform(posted_file, warnings=warnings) output[u'xlsform_valid'] = True posted_file.seek(0) if posted_file.content_type in XLS_CONTENT_TYPES: _csv = pyxform_utils.convert_xls_to_csv_string(posted_file) elif posted_file.content_type == "text/csv": _csv = posted_file.read() else: raise Exception("Content-type not recognized: '%s'" % posted_file.content_type) new_survey_draft = SurveyDraft.objects.create( **{ u'body': smart_unicode(_csv), u'name': posted_file.name, u'user': request.user }) output[u'survey_draft_id'] = new_survey_draft.id except Exception, err: response_code = 500
}) output[u'survey_draft_id'] = new_survey_draft.id except Exception, err: response_code = 500 output[u'error'] = err.message or str(err) output[u'warnings'] = warnings else: try: # create and validate the xform but ignore the results warnings = [] pyxform_utils.validate_kobo_xlsform(posted_file, warnings=warnings) output[u'xlsform_valid'] = True posted_file.seek(0) if posted_file.content_type in XLS_CONTENT_TYPES: _csv = pyxform_utils.convert_xls_to_csv_string(posted_file) elif posted_file.content_type == "text/csv": _csv = posted_file.read() else: raise Exception("Content-type not recognized: '%s'" % posted_file.content_type) new_survey_draft = SurveyDraft.objects.create(**{ u'body': smart_unicode(_csv), u'name': posted_file.name, u'user': request.user }) output[u'survey_draft_id'] = new_survey_draft.id except Exception, err: response_code = 500 output[u'error'] = err.message or str(err) return HttpResponse(json.dumps(output), content_type="application/json", status=response_code)