def view(request): form = None if request.method == 'POST': if 'cancel' in request.POST: messages.add_message(request, messages.INFO, 'Form submission cancelled.') else: form = ExampleForm(request.POST, request.FILES) if form.is_valid(): choice = form.cleaned_data['on_valid_submit'] if choice == CHOICE_REDIRECT: time.sleep(3) return ajaxform.redirect(request, 'styleguide:index') elif choice == CHOICE_500: raise Exception('Here is the 500 you ordered.') else: return HttpResponse('Here is an unexpected response.') messages.add_message( request, messages.ERROR, 'Uh oh. Please correct the error(s) below and try again.') return ajaxform.render( request, context=create_template_context(ajaxform_form=form), template_name='styleguide_ajaxform.html', ajax_template_name='styleguide_ajaxform_example.html')
def view(request): form = None if request.method == 'POST': if 'cancel' in request.POST: messages.add_message( request, messages.INFO, 'Form submission cancelled.' ) else: form = ExampleForm(request.POST, request.FILES) if form.is_valid(): choice = form.cleaned_data['on_valid_submit'] if choice == CHOICE_REDIRECT: time.sleep(3) return ajaxform.redirect(request, 'styleguide:index') elif choice == CHOICE_500: raise Exception('Here is the 500 you ordered.') else: return HttpResponse('Here is an unexpected response.') messages.add_message( request, messages.ERROR, 'Uh oh. Please correct the error(s) below and try again.' ) return ajaxform.render( request, context=create_template_context(ajaxform_form=form), template_name='styleguide_ajaxform.html', ajax_template_name='styleguide_ajaxform_example.html' )
def step_3(request, step, recorder): if get_step_form_from_session(2, request) is None: return redirect('data_capture:step_2') session_pl = request.session[SESSION_KEY] step_1_data = get_step_form_from_session(1, request).cleaned_data schedule_class = step_1_data['schedule_class'] gleaned_data = get_deserialized_gleaned_data(request) is_file_required = not (gleaned_data and gleaned_data.valid_rows) if request.method == 'GET': existing_filename = None if not is_file_required: existing_filename = session_pl.get('filename') form = forms.PriceListUploadForm( schedule=step_1_data['schedule'], is_file_required=is_file_required, existing_filename=existing_filename, ) else: # POST gleaned_data = get_deserialized_gleaned_data(request) form = forms.PriceListUploadForm( request.POST, request.FILES, schedule=step_1_data['schedule'], is_file_required=is_file_required ) if form.is_valid(): if 'gleaned_data' in form.cleaned_data: gleaned_data = form.cleaned_data['gleaned_data'] session_pl['gleaned_data'] = registry.serialize(gleaned_data) session_pl['filename'] = form.cleaned_data['file'].name request.session.modified = True recorder.on_gleaned_data(gleaned_data) if gleaned_data.invalid_rows: return ajaxform.redirect(request, 'data_capture:step_3_errors') return ajaxform.redirect(request, 'data_capture:step_4') else: add_generic_form_error(request, form) return ajaxform.render( request, context=step.context({ 'form': form, 'upload_example': schedule_class.render_upload_example(request) }, request), template_name=step.template_name, ajax_template_name='data_capture/price_list/upload_form.html', )
def analyze_step_2(request, step): step_1_post_data = get_nested_item(request.session, ( 'data_capture:analyze_price_list', 'step_1_POST' )) if step_1_post_data is None: return redirect('data_capture:analyze_step_1') step_1_form = AnalyzeStep1Form(step_1_post_data) if not step_1_form.is_valid(): raise AssertionError('invalid step 1 data in session') schedule_class = step_1_form.cleaned_data['schedule_class'] form_kwargs = dict( schedule=step_1_form.cleaned_data['schedule'], ) if request.method == 'GET': form = forms.PriceListUploadForm(**form_kwargs) else: form = forms.PriceListUploadForm( request.POST, request.FILES, **form_kwargs ) if form.is_valid(): if 'gleaned_data' in form.cleaned_data: gleaned_data = form.cleaned_data['gleaned_data'] session_pl = request.session['data_capture:analyze_price_list'] session_pl['gleaned_data'] = registry.serialize(gleaned_data) session_pl['filename'] = form.cleaned_data['file'].name request.session.modified = True if gleaned_data.invalid_rows: return ajaxform.redirect(request, 'data_capture:analyze_step_2_errors') return ajaxform.redirect(request, 'data_capture:analyze_step_3') else: add_generic_form_error(request, form) return ajaxform.render( request, context=step.context({ 'form': form, 'upload_example': schedule_class.render_upload_example(request) }), template_name=step.template_name, ajax_template_name='data_capture/analyze_price_list/upload_form.html', )
def replace_step_1(request, id, step): price_list = price_list = get_object_or_404(SubmittedPriceList, pk=id) schedule_class = registry.get_class(price_list.schedule) if request.method == 'GET': form = forms.PriceListUploadForm(schedule=price_list.schedule) else: # POST form = forms.PriceListUploadForm(request.POST, request.FILES, schedule=price_list.schedule) if form.is_valid(): gleaned_data = form.cleaned_data['gleaned_data'] request.session[SESSION_KEY] = { 'gleaned_data': registry.serialize(gleaned_data), 'uploaded_filename': form.cleaned_data['file'].name, # save the id in session to make sure flow is not messed with 'price_list_id': id, } if gleaned_data.invalid_rows: return ajaxform.redirect(request, 'data_capture:replace_step_1_errors', id=price_list.pk) return ajaxform.redirect(request, 'data_capture:replace_step_2', id=price_list.pk) else: add_generic_form_error(request, form) return ajaxform.render( request, context=step.context({ 'form': form, 'price_list': price_list, 'upload_example': schedule_class.render_upload_example(request), }), template_name=step.template_name, ajax_template_name='data_capture/replace_price_list/upload_form.html', )
def bulk_region_10_step_1(request, step): ''' Start of Region 10 Bulk Upload - Upload the spreadsheet ''' if request.method == 'GET': form = forms.Region10BulkUploadForm() elif request.method == 'POST': form = forms.Region10BulkUploadForm(request.POST, request.FILES) if form.is_valid(): file = form.cleaned_data['file'] upload_source = BulkUploadContractSource.objects.create( submitter=request.user, procurement_center=BulkUploadContractSource.REGION_10, has_been_loaded=False, original_file=file.read(), file_mime_type=file.content_type ) request.session['data_capture:upload_source_id'] = upload_source.pk return ajaxform.redirect( request, 'data_capture:bulk_region_10_step_2' ) else: add_generic_form_error(request, form) return ajaxform.render( request, context=step.context({ 'form': form, 'upload_example': render_r10_spreadsheet_example(request), }), template_name=step.template_name, ajax_template_name='data_capture/bulk_upload/' 'region_10_step_1_form.html', )
def bulk_region_10_step_1(request, step): ''' Start of Region 10 Bulk Upload - Upload the spreadsheet ''' if request.method == 'GET': form = forms.Region10BulkUploadForm() elif request.method == 'POST': form = forms.Region10BulkUploadForm(request.POST, request.FILES) if form.is_valid(): file = form.cleaned_data['file'] upload_source = BulkUploadContractSource.objects.create( submitter=request.user, procurement_center=BulkUploadContractSource.REGION_10, has_been_loaded=False, original_file=file.read(), file_mime_type=file.content_type) request.session['data_capture:upload_source_id'] = upload_source.pk return ajaxform.redirect(request, 'data_capture:bulk_region_10_step_2') else: add_generic_form_error(request, form) return ajaxform.render( request, context=step.context({ 'form': form, 'upload_example': render_r10_spreadsheet_example(request), }), template_name=step.template_name, ajax_template_name='data_capture/bulk_upload/' 'region_10_step_1_form.html', )