Beispiel #1
0
def start():
    args= parseArgs()
    print 'args',args
    try:
        config = utils.load_config(args.config)
    except Exception as e:
        ErrorHandler.DEBUG=True
        eh.handleError(log,"Exception during config initialisation", exception=e)
        return 
    
    #setup the data cache for storing uploaded files
    maxFileSize=config['ui']['maxFileSize']
    datacache = data_cache.DataCache(config['data'], maxFileSize)
    app.config['DATACACHE'] = datacache

    #get the port
    port=config['ui']['port']
    if args.port:
        #cli argument overwrites config port
        port = args.port


    log.info('Starting CSVEngine on http://localhost:{}/'.format(port))

    app.config['MAX_CONTENT_LENGTH'] = maxFileSize
    app.config['navigation_bar'] = parseServices()
    
    app.register_blueprint(csvb, url_prefix='/csvengine')
    app.register_blueprint(datacache_bp,url_prefix='/csvengine')
    app.register_blueprint(csvm_bp,url_prefix='/csvengine')
    
    
    app.run(debug=True, port=port)
def submit():
    file, url, filename = None, None, ''
    try:
        #debug print
        current_app.logger.debug(request.form)
        service = request.form.get('service')

        ### CHECK INPUT PARAMETERS
        ###########################
        
        #check for url
        url = request.form.get('url')

        #check for file uploads
        file = request.files['uploadBtn']
        fileID = request.form.get('fileID')
        if file and file.filename != '':
            filename = file.filename
        elif url:
            filename = url
        else:
            filename = fileID

        #check for copy paste
        textUpload = request.form.get('textUpload')

        #debug
        #current_app.logger.debug('textUpload:'+textUpload+" url:"+url+" fileID:"+fileID+" file:"+file.filename)

        # don't store data checkbox
        store_data = 'check' not in request.form

        datacache = current_app.config['DATACACHE']

        # error handling
        if file and not allowed_file(file.filename):
            data={
              "message":'"'+filename+'" is not a valid filename.',
              'title':"Format Error",
              'exception': ValueError.__class__.__name__,
              "filename": filename
              }
            
            return render_template("input_error.html", **data)
        if fileID:
            if not datacache.exists(fileID):
                data={
                      "message":"Unfortunately we cannot find your file with the ID "+fileID+". Please re-submit the file or URL.",
                      'title':"File not found",
                      'exception': ValueError.__class__.__name__,
                      "filename": filename
                      }
                return render_template("input_error.html", **data)

        if url or (file and file.filename != '') or textUpload:
            # store content in web or tmp folder
            if store_data:
                fileID = datacache.submitToWeb(file=file, url=url, content=textUpload)
            else:
                fileID = datacache.submitToTmp(file=file, url=url, content=textUpload)
            # set parameter in URL
            if url and fileID:
                return redirect(url_for(service, url=url, fileID=fileID))
            else:
                return redirect(url_for(service, fileID=fileID))

        # error if no input is specified
        data={
              "message": 'Arguments missing: fileID or url required',
              'title':"Input Error",
              'exception': "",
              "filename": "No CSV file specified"
        }
        return render_template("input_error.html", **data)

    except RequestEntityTooLarge as e:
        max_in_mb = current_app.config['MAX_CONTENT_LENGTH']/1000000.0
        data={
              "message":'The file '+filename+' exceeds the capacity limit of '+str(max_in_mb)+' MB.',
              'title':"Input File Too Large",
              'exception': e.__class__.__name__,
              "filename": filename
        }
        return render_template("input_error.html", **data)
    except Exception as e:
        eh.handleError(log, "Error", exception=e)
        
        data={
              "message":e.message,
              'title':"Submit Error",
              'exception': e.__class__.__name__,
              "filename": filename
        }
        return render_template("input_error.html", **data )