예제 #1
0
def loader_excel():
    event = Event('web.content.loader_excel')
    key = request.form['login_key']
    bid = request.form['blibb_id']
    user = get_user_name(key)
    res = dict()
    file = request.files['file']
    if file and allowed_file(file.filename):
        try:
            filename = secure_filename(file.filename)
            excel_file = os.path.join(
                get_config_value('UPLOAD_FOLDER'), filename
            )
            file.save(excel_file)
            if is_valid_id(bid):
                fields = Blibb.get_fields(bid)
                excel_data = loader.excel_to_dict(excel_file, fields)
                current_app.logger.debug(excel_data)
                if len(excel_data):
                    a = Blitem.bulk_insert(bid, user, excel_data)
                    current_app.logger.debug(a)
                    res['result'] = 'ok'
                else:
                    res['error'] = 'No data found in file'
            else:
                if bid == '-1':
                    res['error'] = 'create new blibb from file'

                res['error'] = 'Object Id is not valid'
        # except Exception, e:
            # current_app.logger.error(e)
            # res['error'] = 'Error processing spreadsheet'

        finally:
            if os.path.isfile(filename):
                os.unlink(filename)

    else:
        res['error'] = 'File was not uploaded'

    event.save()
    return jsonify(res)
예제 #2
0
def getBlibbFields(bid=None):
    if bid is not None:
        fields = Blibb.get_fields(bid)
        return jsonify({'fields': fields})