Exemplo n.º 1
0
def rfq_qfinder(request):
    menu_texts = FrontendTexts('menu')
    instructions = Instructions('rfqs', 'qfinder')
    uploaded_file_url = ''
    try:
        if request.method == 'POST' and request.FILES['myfile']:
            myfile = request.FILES['myfile']
            fs = FileSystemStorage()
            filename = fs.save(myfile.name, myfile)
            uploaded_file_url = fs.url(filename)

            rfq = RFQCreator()
            result = rfq.findQuotesFromCSV('.' + uploaded_file_url)

            # ... print(json.dumps(result))

            backend_host = MachineConfigurator().getBackend()
            r = requests.post(backend_host + '/auth/rfqs/quotes', json=result)

            backend_message = BackendMessage(json.loads(r.text))

            if not backend_message.getErrorInd():

                quoted_materials = json.loads(backend_message.getValue())
                cleanup(uploaded_file_url)

                return render(request, 'rfqs/rfq_qfinder.html', {'menu_text': menu_texts.getComponent(),
                                                                 'view_texts': view_texts.getComponent(),
                                                                 'quoted_materials': quoted_materials,
                                                                 'instructions_title': instructions.getTitle(),
                                                                 'instructions_steps': instructions.getSteps()
                                                                 })
            else:
                print(backend_message.getValue())
                cleanup(uploaded_file_url)
                return render(request, 'rfqs/rfq_qfinder.html', {'menu_text': menu_texts.getComponent(),
                                                                 'view_texts': view_texts.getComponent(),
                                                                 'error_message': backend_message.getValue(),
                                                                 'instructions_title': instructions.getTitle(),
                                                                 'instructions_steps': instructions.getSteps()
                                                                 })

        return render(request, 'rfqs/rfq_qfinder.html', {'menu_text': menu_texts.getComponent(),
                                                         'view_texts': view_texts.getComponent(),
                                                         'instructions_title': instructions.getTitle(),
                                                         'instructions_steps': instructions.getSteps()
                                                         })

    except ConnectionError as exception:
        print("There is a problem with the backend return value")
        print(exception)
        return render(request, 'rfqs/rfq_qfinder.html', {'menu_text': menu_texts.getComponent(),
                                                         'view_texts': view_texts.getComponent(),
                                                         'error_message': 'Backend connection problem',
                                                         'instructions_title': instructions.getTitle(),
                                                         'instructions_steps': instructions.getSteps()
                                                         })

    except Exception as exception:
        cleanup(uploaded_file_url)
        print(exception)
        return render(request, 'rfqs/rfq_qfinder.html', {'menu_text': menu_texts.getComponent(),
                                                         'view_texts': view_texts.getComponent(),
                                                         'error_message': "Frontend Error",
                                                         'instructions_title': instructions.getTitle(),
                                                         'instructions_steps': instructions.getSteps()
                                                         })
Exemplo n.º 2
0
def rfq_basic_analyzer(request, code):
    menu_texts = FrontendTexts('menu')
    instructions = Instructions('rfqs', 'analyze')
    output_file = ''
    try:

        backend_host = MachineConfigurator().getBackend()

        r = requests.get(backend_host + '/auth/rfqs/analysis/' + code)

        backend_message = BackendMessage(json.loads(r.text))

        print(backend_message.getErrorInd())
        
        if not backend_message.getErrorInd():

            rfq = json.loads(backend_message.getValue())

            rfq_service = RFQCreator()
            output_file = rfq_service.runBasicAnalysis(rfq)

            if os.path.exists(output_file):
                with open(output_file, 'rb') as fh:
                    response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
                cleanup('/' + output_file)
                response['Content-Disposition'] = 'inline; filename=' + os.path.basename(output_file)
                return response
            raise Http404

        return render(request, 'rfqs/rfq_basic_analysis.html', {'menu_text': menu_texts.getComponent(),
                                                                'view_texts': view_texts.getComponent(),
                                                                'error_message': 'Backend problem - cannot create file',
                                                                'instructions_title': instructions.getTitle(),
                                                                'instructions_steps': instructions.getSteps()})

    except ValueError as exception:
        print("There is a problem with the backend return value")
        print(exception)
        return render(request, 'rfqs/rfq_basic_analysis.html', {'menu_text': menu_texts.getComponent(),
                                                                'view_texts': view_texts.getComponent(),
                                                                'error_message': 'Backend problem',
                                                                'instructions_title': instructions.getTitle(),
                                                                'instructions_steps': instructions.getSteps()
                                                                })

    except ConnectionError as exception:
        print("There is a problem with the backend return value")
        print(exception)
        return render(request, 'rfqs/rfq_basic_analysis.html', {'menu_text': menu_texts.getComponent(),
                                                                'view_texts': view_texts.getComponent(),
                                                                'error_message': 'Backend connection problem',
                                                                'instructions_title': instructions.getTitle(),
                                                                'instructions_steps': instructions.getSteps()
                                                                })

    except Exception as exception:
        cleanup(output_file)
        print(exception)
        return render(request, 'rfqs/rfq_basic_analysis.html', {'menu_text': menu_texts.getComponent(),
                                                                'view_texts': view_texts.getComponent(),
                                                                'error_message': "Frontend Error",
                                                                'instructions_title': instructions.getTitle(),
                                                                'instructions_steps': instructions.getSteps()
                                                                })
Exemplo n.º 3
0
def rfq_export(request):
    menu_texts = FrontendTexts('menu')
    instructions = Instructions('rfqs', 'export')
    output_file = ''
    try:
        if request.method == 'POST':
            form = RFQInternalCode(request.POST)
            if form.is_valid():

                internal_code = form.cleaned_data['internalcode']

                incoterms = form.cleaned_data['incoterms']

                port = form.cleaned_data['port']

                backend_host = MachineConfigurator().getBackend()

                r = requests.get(backend_host + '/auth/rfqs/' + internal_code)

                backend_message = BackendMessage(json.loads(r.text))

                if not backend_message.getErrorInd():

                    rfq = json.loads(backend_message.getValue())
                    rfq_service = RFQCreator()
                    output_file = rfq_service.exportRFQtoCSV(rfq, incoterms, port)

                    if os.path.exists(output_file):
                        with open(output_file, 'rb') as fh:
                            response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
                        cleanup('/' + output_file)
                        response['Content-Disposition'] = 'inline; filename=' + os.path.basename(output_file)
                        return response
                    raise Http404

                else:
                    print("There is a backend error message")
                    return render(request, 'rfqs/rfq_export.html', {'menu_text': menu_texts.getComponent(),
                                                                    'view_texts': view_texts.getComponent(),
                                                                    'rfqform': form,
                                                                    'error_message': backend_message.getValue(),
                                                                    'instructions_title': instructions.getTitle(),
                                                                    'instructions_steps': instructions.getSteps()
                                                                    })

        else:
            form = RFQInternalCode()

        return render(request, 'rfqs/rfq_export.html', {'menu_text': menu_texts.getComponent(),
                                                        'view_texts': view_texts.getComponent(),
                                                        'rfqform': form,
                                                        'instructions_title': instructions.getTitle(),
                                                        'instructions_steps': instructions.getSteps()
                                                        })

    except ValueError as exception:
        print("There is a problem with the backend return value")
        print(exception)
        return render(request, 'rfqs/rfq_export.html', {'menu_text': menu_texts.getComponent(),
                                                        'view_texts': view_texts.getComponent(),
                                                        'error_message': 'Backend problem',
                                                        'instructions_title': instructions.getTitle(),
                                                        'instructions_steps': instructions.getSteps()
                                                        })

    except ConnectionError as exception:
        print("There is a problem with the backend return value")
        print(exception)
        return render(request, 'rfqs/rfq_upload.html', {'menu_text': menu_texts.getComponent(),
                                                        'view_texts': view_texts.getComponent(),
                                                        'error_message': 'Backend connection problem',
                                                        'instructions_title': instructions.getTitle(),
                                                        'instructions_steps': instructions.getSteps()
                                                        })

    except Exception as exception:
        cleanup(output_file)
        print(exception)
        return render(request, 'rfqs/rfq_upload.html', {'menu_text': menu_texts.getComponent(),
                                                        'view_texts': view_texts.getComponent(),
                                                        'error_message': "Frontend Error",
                                                        'instructions_title': instructions.getTitle(),
                                                        'instructions_steps': instructions.getSteps()
                                                        })