Exemplo n.º 1
0
def report(request):
    context = {}
    tabID = request.GET.get('tabID')
    try:
        data = serialiser.deserialise(request.session[tabID])
    except KeyError:
        return HttpResponseRedirect( '/upload/dev/' + "?error=sessionExpired")
    if request.session[tabID+'_referrer'] == 'errors':
        data.analysis()
        request.session[tabID+'_analysis()'] = True
        request.session[tabID+'_referrer'] = 'viewReport'
        request.session[tabID] = serialiser.serialise( data)
        form = ReportButton(None)
        context['form'] = form
        context['tabID'] = tabID
        return render( request,
            'viewReport/index.html',
            context = context
        )
    elif 'makeReport' in request.POST:
        report = Report(data, "")
        report_string = report.html_report()
        return HttpResponse( report_string )
    elif 'downloadData' in request.POST:
        return HttpResponseRedirect( saveData(data) )
    elif 'datatypetemplate' in request.POST:
        return HttpResponseRedirect( saveDatatypes(data) )
    elif request.session[tabID+'_referrer'] == 'viewReport':
        form = ReportButton(None)
        context['form'] = form
        context['tabID'] = tabID
        return render( request,
            'viewReport/index.html',
            context = context
        )
Exemplo n.º 2
0
def upload(request):
    #Handle file upload
    if request.method == 'GET' and 'error' in request.GET:
        print("error")
        form = DocumentForm() # An empty form
        sessionExpired = True

    elif request.method == 'POST' and "viewReport" in request.POST:
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            dataDoc = Document(docfile = request.FILES['dataFile'])
            dataDoc.save()
            data = None
            if 'templateFile' in request.FILES:
                templateDoc = Document(docfile = request.FILES['templateFile'])
                templateDoc.save()
                templateObject = Template(templateDoc.getPath())
                data = Data(dataDoc.getPath(), templateObject)
                templateDoc.delete()
            else:
                data = Data(dataDoc.getPath())
            # Load data from CSV
            #data = Data(dataDoc.getPath())
            data.filename = dataDoc.getName()
            # Perform Analysis
            data.remove_invalid()
            data.create_columns()
            data.clean()
            data.pre_analysis()
            data.find_errors()
            data.analysis() 
            # Generate Report
            report = Report(data, dataDoc.getPath())
            report_string = report.html_report()
            # Delete uploaded CSV file & report from server
            dataDoc.delete()
            return HttpResponse( report_string )

    elif request.method == 'POST' and "viewOptions" in request.POST:
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            dataDoc = Document(docfile = request.FILES['dataFile'])
            dataDoc.save()
            if 'templateFile' in request.FILES:
                templateDoc = Document(docfile = request.FILES['templateFile'])
                templateDoc.save()
                templateObject = Template(templateDoc.getPath())
                data = Data(dataDoc.getPath(), templateObject)
                templateDoc.delete()
            else:
                data = Data(dataDoc.getPath())
            data.filename = dataDoc.getName()
            dataDoc.delete()
            request.session.set_expiry(0)
            tabID = hasher.makeTabID()
            request.session[tabID] = serialiser.serialise( data)
            request.session[tabID+'_remove_invalid()'] = False
            request.session[tabID+'_create_columns()'] = False
            request.session[tabID+'_clean()'] = False
            request.session[tabID+'_pre_analysis()'] = False
            request.session[tabID+'_find_errors()'] = False
            request.session[tabID+'_analysis()'] = False
            request.session[tabID+'_referrer'] = 'upload'
            #Redirect to the generated report after POST
            return HttpResponseRedirect( '/invalid_rows' + "?tabID=" + tabID)

    else:
        form = DocumentForm() # An empty form

    # Contstruct template context object
    context = {}
    context['form'] = form
    try:
        context['tabID'] = tabID
    except NameError:
        context['tabID'] = None

    try:
        context['sessionExpired'] = sessionExpired
    except:
        context['sessionExpired'] = False
    # Render the form
    return render( request,
        'upload/index.html',
        context = context,
        )