コード例 #1
0
ファイル: devices.py プロジェクト: imfht/flaskapp1
def export_devices():
    device_count = db.session.query(func.count(Device.id)) \
        .filter(Device.tenantID == g.tenant_uid).scalar()
    if device_count and device_count > 10000:
        raise ResourceLimited(field='devices')
    if device_count == 0:
        raise DataNotFound(field='devices')
    export_url = current_app.config.get('EXPORT_EXCEL_TASK_URL')
    request_json = {
        'tenantID': g.tenant_uid,
        'language': g.language
    }
    with SyncHttp() as sync_http:
        response = sync_http.post(export_url, json=request_json)
    handled_response = handle_task_scheduler_response(response)
    if handled_response.get('status') == 3:
        query_status_url = url_for('base.get_task_scheduler_status')[7:]
        task_id = handled_response['taskID']
        record = {
            'status': 3,
            'taskID': task_id,
            'message': 'Devices export is in progress',
            'result': {
                'statusUrl': f"{query_status_url}?taskID={task_id}"
            }
        }
    else:
        record = {
            'status': 4,
            'message': handled_response.get('error') or 'Devices export failed',
        }
    return jsonify(record)
コード例 #2
0
ファイル: devices.py プロジェクト: imfht/flaskapp1
def devices_import():
    try:
        file_prefix = 'device_import_' + g.tenant_uid
        file_name = excels.save(request.files['file'], name=file_prefix + '.')
    except UploadNotAllowed:
        error = {'Upload': 'Upload file format error'}
        raise APIException(errors=error)
    file_path = excels.path(file_name)
    import_url = current_app.config.get('IMPORT_EXCEL_TASK_URL')
    request_json = {
        'filePath': file_path,
        'tenantID': g.tenant_uid,
        'userIntID': g.user_id,
        'language': g.language
    }
    with SyncHttp() as sync_http:
        response = sync_http.post(import_url, json=request_json)
    handled_response = handle_task_scheduler_response(response)
    if handled_response.get('status') == 3:
        query_status_url = url_for('base.get_task_scheduler_status')[7:]
        task_id = handled_response['taskID']
        record = {
            'status': 3,
            'taskID': task_id,
            'message': 'Devices import is in progress',
            'result': {
                'statusUrl': f"{query_status_url}?taskID={task_id}"
            }
        }
    else:
        record = {
            'status': 4,
            'message': handled_response.get('error') or 'Devices import failed',
        }
    return jsonify(record)
コード例 #3
0
ファイル: _device.py プロジェクト: imfht/flaskapp1
async def devices_publish(publish_info: List[Dict]) -> Dict:
    url = project_config['PUBLISH_TASK_URL']
    async with AsyncHttp() as async_http:
        responses = await async_http.post_url_args(url=url,
                                                   requests_json=publish_info)
    success, failed = 0, 0
    for response in responses:
        handled_response = handle_task_scheduler_response(response)
        if handled_response.get('status') == 3:
            success += 1
        else:
            failed += 1
    publish_result = {'success': success, 'failed': failed}
    return publish_result