def excel_commit(request, domain): """ Step three of three. This page is submitted with the list of column to case property mappings for this upload. The config variable is an ImporterConfig object that has everything gathered from previous steps, with the addition of all the field data. See that class for more information. """ config = importer_util.ImporterConfig.from_request(request) excel_id = request.session.get(EXCEL_SESSION_ID) case_upload = CaseUpload.get(excel_id) try: case_upload.check_file() except ImporterError as e: return render_error(request, domain, get_importer_error_message(e)) case_upload.trigger_upload(domain, config) request.session.pop(EXCEL_SESSION_ID, None) return HttpResponseRedirect(base.ImportCases.get_url(domain))
def bulk_import_async(config, domain, excel_id): case_upload = CaseUpload.get(excel_id) try: case_upload.check_file() except ImporterError as e: update_task_state(bulk_import_async, states.FAILURE, {'errors': get_importer_error_message(e)}) raise Ignore() try: with case_upload.get_spreadsheet() as spreadsheet: result = do_import(spreadsheet, config, domain, task=bulk_import_async, record_form_callback=case_upload.record_form) _alert_on_result(result, domain) # return compatible with soil return {'messages': result} except ImporterError as e: update_task_state(bulk_import_async, states.FAILURE, {'errors': get_importer_error_message(e)}) raise Ignore() finally: store_task_result.delay(excel_id)
def bulk_import_async(config, domain, excel_id): case_upload = CaseUpload.get(excel_id) try: case_upload.check_file() except ImporterError as e: return {'errors': get_importer_error_message(e)} try: with case_upload.get_spreadsheet() as spreadsheet: result = do_import(spreadsheet, config, domain, task=bulk_import_async, record_form_callback=case_upload.record_form) # return compatible with soil return { 'messages': result } except ImporterError as e: return {'errors': get_importer_error_message(e)} finally: store_task_result.delay(excel_id)
def bulk_import_async(config, domain, excel_id): case_upload = CaseUpload.get(excel_id) try: case_upload.check_file() except ImporterError as e: return {'errors': get_importer_error_message(e)} try: with case_upload.get_spreadsheet() as spreadsheet: result = do_import(spreadsheet, config, domain, task=bulk_import_async) # return compatible with soil return { 'messages': result } except ImporterError as e: return {'errors': get_importer_error_message(e)} finally: store_task_result.delay(excel_id)
def bulk_import_async(config, domain, excel_id): case_upload = CaseUpload.get(excel_id) try: case_upload.check_file() except ImporterError as e: update_task_state(bulk_import_async, states.FAILURE, {'errors': get_importer_error_message(e)}) raise Ignore() try: with case_upload.get_spreadsheet() as spreadsheet: result = do_import(spreadsheet, config, domain, task=bulk_import_async, record_form_callback=case_upload.record_form) _alert_on_result(result, domain) # return compatible with soil return { 'messages': result } except ImporterError as e: update_task_state(bulk_import_async, states.FAILURE, {'errors': get_importer_error_message(e)}) raise Ignore() finally: store_task_result.delay(excel_id)
def excel_fields(request, domain): """ Step two of three. Important values that are grabbed from the POST or defined by the user on this page: case_type: The type of case we are matching to. When creating new cases, this is the type they will be created as. When updating existing cases, this is the type that we will search for. If the wrong case type is used when looking up existing cases, we will not update them. create_new_cases: A boolean that controls whether or not the user wanted to create new cases for any case that doesn't have a matching case id in the upload. search_column: Which column of the Excel file we are using to specify either case ids or external ids. This is, strangely, required. If creating new cases only you would expect these to be blank with the create_new_cases flag set. search_field: Either case id or external id, determines which type of identification we are using to match to cases. """ case_type = request.POST['case_type'] try: search_column = request.POST['search_column'] except MultiValueDictKeyError: # this is only true if your configuration is messed up in an irreparable way messages.error( request, _('The Excel file you are trying to import does not have any headers.' )) return HttpResponseRedirect(base.ImportCases.get_url(domain)) search_field = request.POST['search_field'] create_new_cases = request.POST.get('create_new_cases') == 'on' case_upload = CaseUpload.get(request.session.get(EXCEL_SESSION_ID)) try: case_upload.check_file() except ImporterError as e: return render_error(request, domain, get_importer_error_message(e)) with case_upload.get_spreadsheet() as spreadsheet: columns = spreadsheet.get_header_columns() excel_fields = columns # hide search column and matching case fields from the update list if search_column in excel_fields: excel_fields.remove(search_column) field_specs = get_suggested_case_fields(domain, case_type, exclude=[search_field]) case_field_specs = [field_spec.to_json() for field_spec in field_specs] context = { 'case_type': case_type, 'search_column': search_column, 'search_field': search_field, 'create_new_cases': create_new_cases, 'columns': columns, 'excel_fields': excel_fields, 'case_field_specs': case_field_specs, 'domain': domain, } context.update( _case_importer_breadcrumb_context( _('Match Excel Columns to Case Properties'), domain)) return render(request, "case_importer/excel_fields.html", context)
def excel_fields(request, domain): """ Step two of three. Important values that are grabbed from the POST or defined by the user on this page: case_type: The type of case we are matching to. When creating new cases, this is the type they will be created as. When updating existing cases, this is the type that we will search for. If the wrong case type is used when looking up existing cases, we will not update them. create_new_cases: A boolean that controls whether or not the user wanted to create new cases for any case that doesn't have a matching case id in the upload. search_column: Which column of the excel file we are using to specify either case ids or external ids. This is, strangely, required. If creating new cases only you would expect these to be blank with the create_new_cases flag set. search_field: Either case id or external id, determines which type of identification we are using to match to cases. """ case_type = request.POST['case_type'] try: search_column = request.POST['search_column'] except MultiValueDictKeyError: # this is only true if your configuration is messed up in an irreparable way messages.error(request, _('It looks like you may have accessed this page from a stale page. ' 'Please start over.')) return _spreadsheet_expired(request, domain) search_field = request.POST['search_field'] create_new_cases = request.POST.get('create_new_cases') == 'on' case_upload = CaseUpload.get(request.session.get(EXCEL_SESSION_ID)) try: case_upload.check_file() except ImporterError as e: return render_error(request, domain, get_importer_error_message(e)) with case_upload.get_spreadsheet() as spreadsheet: columns = spreadsheet.get_header_columns() excel_fields = columns # hide search column and matching case fields from the update list if search_column in excel_fields: excel_fields.remove(search_column) field_specs = get_suggested_case_fields( domain, case_type, exclude=[search_field]) case_field_specs = [field_spec.to_json() for field_spec in field_specs] return render( request, "case_importer/excel_fields.html", { 'case_type': case_type, 'search_column': search_column, 'search_field': search_field, 'create_new_cases': create_new_cases, 'columns': columns, 'excel_fields': excel_fields, 'case_field_specs': case_field_specs, 'domain': domain, 'report': { 'name': 'Import: Match columns to fields' }, 'slug': base.ImportCases.slug } )
def store_task_result(upload_id): case_upload = CaseUpload.get(upload_id) case_upload.store_task_result()