def test_error_message_for_spaces_in_choices_name_for_multiple_select_choices( self): error_message = transform_error_message( 'Choice names with spaces cannot be added to multiple choice selects. See [spaced name] in [list]' ) self.assertEquals(error_message, EXPECTED_SPACE_NOT_ALLOWED_IN_CHOICES_ERROR)
def post(self, request): file_content = None tmp_file = None try: file_content = request.raw_post_data file_errors, file_extension = _perform_file_validations(request) tmp_file = NamedTemporaryFile(delete=True, suffix=file_extension) if file_errors: logger.info( "User: %s. Upload File validation failed: %s. File name: %s, size: %d", request.user.username, json.dumps(file_errors), request.GET.get("qqfile"), int(request.META.get('CONTENT_LENGTH'))) return HttpResponse(json.dumps({ 'success': False, 'error_msg': file_errors }), content_type='application/json') tmp_file.write(file_content) tmp_file.seek(0) project_name = request.GET['pname'].strip() manager = get_database_manager(request.user) questionnaire_code = generate_questionnaire_code(manager) xls_parser_response = XlsFormParser(tmp_file, project_name, manager).parse() send_email_if_unique_id_type_question_has_no_registered_unique_ids( xls_parser_response, request, project_name) profile = request.user.get_profile() organization = Organization.objects.get(org_id=profile.org_id) if xls_parser_response.is_multiple_languages: logger.info( "Creating Questionnaire %s with Multi Language support for organization : %s(%s) and email: %s", project_name, organization.name, profile.org_id, profile.user.email) if xls_parser_response.errors: error_list = list(xls_parser_response.errors) logger.info("User: %s. Upload Errors: %s", request.user.username, json.dumps(error_list)) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': error_list, 'message_prefix': _("Sorry! Current version of DataWinners does not support" ), 'message_suffix': _("Update your XLSForm and upload again.") })) tmp_file.seek(0) mangrove_service = MangroveService( request, questionnaire_code=questionnaire_code, project_name=project_name, xls_form=tmp_file, xls_parser_response=xls_parser_response) questionnaire_id, form_code = mangrove_service.create_project() except PyXFormError as e: logger.info("User: %s. Upload Error: %s", request.user.username, e.message) message = transform_error_message(e.message) if 'name_type_error' in message or 'choice_name_type_error' in message: if 'choice_name_type_error' in message: message_prefix = _( "On your \"choices\" sheet the first and second column must be \"list_name\" and \"name\". Possible errors:" ) else: message_prefix = _( "On your \"survey\" sheet the first and second column must be \"type\" and \"name\". Possible errors:" ) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [ _("Columns are missing"), _("Column name is misspelled"), _("Additional space in column name") ], 'message_prefix': message_prefix, 'message_suffix': _("Update your XLSForm and upload again.") })) else: return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [ message if message else ugettext( "all XLSForm features. Please check the list of unsupported features." ) ] })) except QuestionAlreadyExistsException as e: logger.info("User: %s. Upload Error: %s", request.user.username, e.message) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [ _("Duplicate labels. All questions (labels) must be unique." ) ], 'message_prefix': _("Sorry! Current version of DataWinners does not support" ), 'message_suffix': _("Update your XLSForm and upload again.") })) except UnicodeDecodeError as e: logger.info("User: %s. Upload Error: %s", request.user.username, e.message) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [ _("Check your columns for errors.<br>There are missing symbols (like $ for relevant or calculate) or incorrect characters<br>" ) + _("Update your XLSForm and upload again.") ], })) except Exception as e: message = e.message if e.message else _("Errors in excel") logger.info("User: %s. Upload Exception message: %s", request.user.username, e.message) odk_message = '' if not 'ODK Validate Errors:' in e.message: send_email_on_exception( request.user, "Questionnaire Create", traceback.format_exc(), additional_details={'file_contents': file_content}) else: odk_message = translate_odk_message(e.message) message = odk_message if odk_message else message return HttpResponse(content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [message], })) finally: if tmp_file: tmp_file.close() if not questionnaire_id: org = get_organization(request) if org.is_pro_sms: message = _( "Questionnaire or Poll with same name already exists.Upload was cancelled." ) else: message = _( "Questionnaire with same name already exists.Upload was cancelled." ) return HttpResponse(json.dumps({ 'success': False, 'duplicate_project_name': True, 'error_msg': [message] }), content_type='application/json') return HttpResponse(json.dumps({ "success": True, "project_name": project_name, "project_id": questionnaire_id, "form_code": form_code }), content_type='application/json')
def post(self, request, project_id): manager = get_database_manager(request.user) questionnaire = Project.get(manager, project_id) file_content = None tmp_file = None try: file_content = request.raw_post_data file_errors, file_extension = _perform_file_validations(request) tmp_file = NamedTemporaryFile(delete=True, suffix=file_extension) if file_errors: logger.info( "User: %s. Edit upload File validation failed: %s. File name: %s, size: %d", request.user.username, json.dumps(file_errors), request.GET.get("qqfile"), int(request.META.get('CONTENT_LENGTH'))) return HttpResponse(content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': file_errors })) tmp_file.write(file_content) tmp_file.seek(0) xls_parser_response = XlsFormParser(tmp_file, questionnaire.name, manager).parse() send_email_if_unique_id_type_question_has_no_registered_unique_ids( xls_parser_response, request, questionnaire.name) profile = request.user.get_profile() organization = Organization.objects.get(org_id=profile.org_id) if xls_parser_response.is_multiple_languages: logger.info( "Edit Questionnaire %s with Multi Language support for organization : %s(%s) and email: %s", questionnaire.name, organization.name, profile.org_id, profile.user.email) if xls_parser_response.errors: info_list = list(xls_parser_response.errors) logger.info("User: %s. Edit upload Errors: %s", request.user.username, json.dumps(info_list)) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': info_list, 'message_prefix': _("Sorry! Current version of DataWinners does not support" ), 'message_suffix': _("Update your XLSForm and upload again.") })) mangrove_service = MangroveService( request, questionnaire_code=questionnaire.form_code, project_name=questionnaire.name, xls_parser_response=xls_parser_response) questionnaire.xform = mangrove_service.xform_with_form_code QuestionnaireBuilder(questionnaire, manager).update_questionnaire_with_questions( xls_parser_response.json_xform_data) tmp_file.seek(0) questionnaire.update_media_field_flag() questionnaire.save(process_post_update=False) base_name, extension = os.path.splitext(tmp_file.name) questionnaire.update_attachments(tmp_file, 'questionnaire%s' % extension) self._purge_submissions(manager, questionnaire) self._purge_feed_documents(questionnaire, request) self._purge_media_details_documents(manager, questionnaire) self.recreate_submissions_mapping(manager, questionnaire) if xls_parser_response.info: info_list = list(xls_parser_response.info) logger.info("User: %s. Edit upload Errors: %s", request.user.username, json.dumps(info_list)) return HttpResponse(content_type='application/json', content=json.dumps({ 'success': True, 'information': info_list, })) except PyXFormError as e: logger.info("User: %s. Upload Error: %s", request.user.username, e.message) message = transform_error_message(e.message) if 'name_type_error' in message or 'choice_name_type_error' in message: if 'choice_name_type_error' in message: message_prefix = _( "On your \"choices\" sheet the first and second column must be \"list_name\" and \"name\". Possible errors:" ) else: message_prefix = _( "On your \"survey\" sheet the first and second column must be \"type\" and \"name\". Possible errors:" ) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [ _("Columns are missing"), _("Column name is misspelled"), _("Additional space in column name") ], 'message_prefix': message_prefix, 'message_suffix': _("Update your XLSForm and upload again.") })) else: return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [ message if message else ugettext( "all XLSForm features. Please check the list of unsupported features." ) ] })) except QuestionAlreadyExistsException as e: logger.info("User: %s. Upload Error: %s", request.user.username, e.message) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [ _("Duplicate labels. All questions (labels) must be unique." ) ], 'message_prefix': _("Sorry! Current version of DataWinners does not support" ), 'message_suffix': _("Update your XLSForm and upload again.") })) except UnicodeDecodeError as e: logger.info("User: %s. Upload Error: %s", request.user.username, e.message) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'error_msg': [ _("Check your columns for errors.<br>There are missing symbols (like $ for relevant or calculate) or incorrect characters<br>" ) + _("Update your XLSForm and upload again.") ], })) except Exception as e: logger.info("User: %s. Edit Upload Exception message: %s", request.user.username, e.message) message = e.message if e.message else _("Some error in excel") odk_message = '' if not 'ODK Validate Errors:' in e.message: send_email_on_exception( request.user, "Questionnaire Edit", traceback.format_exc(), additional_details={'file_contents': file_content}) else: odk_message = translate_odk_message(e.message) message = odk_message if odk_message else message return HttpResponse(content_type='application/json', content=json.dumps({ 'error_msg': [message], 'success': False, })) finally: if tmp_file: tmp_file.close() return HttpResponse( json.dumps({ "success": True, "project_name": questionnaire.name, "project_id": questionnaire.id, "file_name": "%s%s" % (slugify(questionnaire.name), extension), # "xls_dict": XlsProjectParser().parse(file_content) }), content_type='application/json')
def _try_parse_xls(manager, request, questionnaire_name, excel_file=None): try: file_validation_results = _perform_file_validations(request) if isinstance(file_validation_results, HttpResponse): return file_validation_results if excel_file: tmp_file = excel_file else: tmp_file = _temp_file(request) xls_parser_response = XlsFormParser(tmp_file, questionnaire_name, manager).parse() profile = request.user.get_profile() organization = Organization.objects.get(org_id=profile.org_id) if xls_parser_response.is_multiple_languages: logger.info( "Edit Questionnaire %s with Multi Language support for organization : %s(%s) and email: %s", questionnaire_name, organization.name, profile.org_id, profile.user.email) if xls_parser_response.errors: info_list = list(xls_parser_response.errors) logger.info("User: %s. Edit upload Errors: %s", request.user.username, json.dumps(info_list)) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'status': 'error', 'error_msg': info_list, 'message_prefix': _("Sorry! Current version of DataWinners does not support" ), 'message_suffix': _("Update your XLSForm and upload again.") })) except BindError as e: error_message = [(e.custom_message + ' for ' + e.attribute + ' in the question ' + e.field.name)] return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'status': 'error', 'error_msg': error_message, 'errors': [{ e.field.name: { e.attribute: e.custom_message } }], 'reason': 'Save Failed', # TODO: i18n translation 'details': '' })) except PyXFormError as e: logger.info("User: %s. Upload Error: %s", request.user.username, e.message) message = transform_error_message(e.message) if 'name_type_error' in message or 'choice_name_type_error' in message: if 'choice_name_type_error' in message: message_prefix = _( "On your \"choices\" sheet the first and second column must be \"list_name\" and \"name\". Possible errors:" ) else: message_prefix = _( "On your \"survey\" sheet the first and second column must be \"type\" and \"name\". Possible errors:" ) return HttpResponse(content_type='application/json', content=json.dumps({ 'success': False, 'status': 'error', 'details': message, 'reason': 'Save Failed', 'error_msg': [ _("Columns are missing"), _("Column name is misspelled"), _("Additional space in column name") ], 'message_prefix': message_prefix, 'message_suffix': _("Update your XLSForm and upload again.") })) else: return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'status': 'error', 'details': message, 'reason': 'Save Failed', 'error_msg': [ message if message else ugettext( "all XLSForm features. Please check the list of unsupported features." ) ] })) except UnicodeDecodeError as e: logger.info("User: %s. Upload Error: %s", request.user.username, e.message) return HttpResponse( content_type='application/json', content=json.dumps({ 'success': False, 'status': 'error', 'details': e.message, 'reason': 'Save Failed', 'error_msg': [ _("Check your columns for errors.<br>There are missing symbols (like $ for relevant or calculate) or incorrect characters<br>" ) + _("Update your XLSForm and upload again.") ], })) except Exception as e: logger.info("User: %s. Edit Upload Exception message: %s", request.user.username, e.message) message = e.message if e.message else _("Some error in excel") odk_message = '' if not 'ODK Validate Errors:' in e.message: send_email_on_exception( request.user, "Questionnaire Edit", traceback.format_exc(), additional_details={'file_contents': request.raw_post_data}) else: odk_message = translate_odk_message(e.message) message = odk_message if odk_message else message return HttpResponse(content_type='application/json', content=json.dumps({ 'error_msg': [message], 'success': False, 'status': 'error', 'details': message, 'reason': 'Save Failed' })) return xls_parser_response
def test_error_message_for_unmatched_begin_statement(self): error_message = transform_error_message( 'Unmatched begin statement: repeat') self.assertEquals(error_message, EXPECTED_UNMATCHED_BEGIN_STATEMENT_ERROR)
def test_error_message_list_name_not_in_choices_sheet(self): error_message = transform_error_message( '[row : 2] List name not in choices sheet: list') self.assertEquals(error_message, EXPECTED_LIST_NAME_NOT_IN_CHOICES_SHEET_ERROR)
def test_error_message_for_duplicate_element_names(self): error_message = transform_error_message( "There are two survey elements named 'ff' in the section named 'tmpgCbbjT'." ) self.assertEquals(error_message, EXPECTED_DUPLICATE_ELEMENT_NAME_ERROR)
def test_error_message_for_duplicate_column_header(self): error_message = transform_error_message( "Duplicate column header:column_name") self.assertEquals(error_message, EXPECTED_DUPLICATE_HEADER_ERROR)