def query_one_solution(solution_id):
    uid, err = get_oemid(request=request)
    if err is not None:
        return jsonify(UNAUTH_RESULT)

    solution = as_dict(DccaAssSolutionImage.get(solution_id), SolutionSchema)

    if solution['owner_id'] == int(uid):
        solution['is_owner'] = True
    else:
        solution['is_owner'] = False
    del solution['owner_id']

    if solution['model']['owner_id'] == int(uid):
        solution['model']['is_owner'] = True
    else:
        solution['model']['is_owner'] = False
    del solution['model']['owner_id']

    if solution['id'] == solution['model']['default_solution_id']:
        solution['is_default'] = True
    else:
        solution['is_default'] = False
    del solution['model']['default_solution_id']

    return jsonify(solution)
def query_devices_by_solution():
    """
    To-Do: need improvement
    """
    uid, err = get_oemid(request=request)
    if err is not None:
        return jsonify(UNAUTH_RESULT)

    ctx.current_user = DccaUser.get_by_id(uid)

    solution_id = request.args.get('solution_id')
    limit = request.args.get('limit') if request.args.get('limit') else 200
    offset = request.args.get('offset') if request.args.get('offset') else 0
    results = OrderedDict()
    results['limit'] = limit
    results['offset'] = offset
    results['results'] = []

    total, devices = Host.query_ota_devices(solution_id, limit, offset)
    results['total'] = total
    if devices:
        devices = as_dict(devices, DeviceSchema, many=True)
        for device in devices:
            device_status = query_one_device_status(device['name'])
            device.update(device_status)
            results['results'].append(device)

    return jsonify(results)
def query_solutions():
    uid, err = get_oemid(request=request)
    if err is not None:
        return jsonify(UNAUTH_RESULT)

    ctx.current_user = DccaUser.get_by_id(uid)

    limit = request.args.get('limit') or 20
    offset = request.args.get('offset') or 0
    solution_name = request.args.get('solution')
    model_id = request.args.get('model_id')
    image = request.args.get('image')
    version = request.args.get('version')
    my_solution = bool_helper(request.args.get('my_solution'))

    results = OrderedDict()
    results['offset'] = offset
    results['limit'] = limit
    results['results'] = []

    total, solutions = DccaAssSolutionImage.query_solutions(
        solution_name, model_id, image, version, my_solution, limit, offset)
    results['total'] = total
    if solutions:
        solutions = as_dict(solutions, SolutionSchema, many=True)
        for solution in solutions:
            if solution['owner_id'] == int(uid):
                solution['is_owner'] = True
            else:
                solution['is_owner'] = False
            del solution['owner_id']

            if solution['model']['owner_id'] == int(uid):
                solution['model']['is_owner'] = True
            else:
                solution['model']['is_owner'] = False
            del solution['model']['owner_id']

            if solution['model']['default_solution_id'] == solution['id']:
                solution['is_default'] = True
            else:
                solution['is_default'] = False
            del solution['model']['default_solution_id']

            results['results'].append(solution)

    return jsonify(results)
def query_solutions_statistics():
    uid, err = get_oemid(request=request)
    if err is not None:
        return jsonify(UNAUTH_RESULT)

    ctx.current_user = DccaUser.get_by_id(uid)

    results = {}
    statistics = Host.query_ota_statistics()
    if statistics:
        statistics = as_dict(statistics, DeviceShortSchema, many=True)
        for stat in statistics:
            solution = stat['solution']
            if solution["id"] not in results:
                results[solution["id"]] = {}
                results[solution["id"]].update(solution)
                results[solution["id"]]["count"] = 1
            else:
                results[solution["id"]]["count"] += 1

    return jsonify(list(results.values()))
def query_audit_requests():
    uid, err = get_oemid(request=request)
    if err is not None:
        return jsonify(UNAUTH_RESULT)

    ctx.current_user = DccaUser.get_by_id(uid)

    if not ctx.current_user.admin:
        return jsonify({
            'status': 'fail',
            'message': 'Not admin, cannot access'
        })

    limit = request.args.get('limit') or 20
    offset = request.args.get('offset') or 0
    filter_text = request.args.get('filter_text') or ''
    order_by = request.args.get('orderBy') or ''
    order_type = request.args.get('orderType') or 'desc'

    if order_type not in ['asc', 'desc']:
        return jsonify({'status': "fail", 'message': "Invalid order type"})

    if order_by not in ['created_at', 'status', '']:
        return jsonify({'status': "fail", 'message': "Invalid orderBy value"})

    results = OrderedDict()
    results['orderType'] = order_type
    results['orderBy'] = order_by
    results['offset'] = offset
    results['limit'] = limit
    results['results'] = []

    total, audits = DccaSolutionAudit.query_audits(filter_text, order_by,
                                                   order_type, limit, offset)
    results['total'] = total
    if audits:
        results['results'] = as_dict(audits, SolutionAuditSchema, many=True)

    return jsonify(results)