Пример #1
0
def god_recycle_device():
    dbg('god_recycle_device')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imei = data['imei']
    except:
        print_exception_info()
        raise ApiError('update devices error', error.ERROR_PARAM)

    god = dbapi.get_god(openluat_user_id=current_user.id)
    if not god:
        raise ApiError('ERROR_GOD_NOT_FOUND', error.ERROR_GOD_NOT_FOUND)

    role_bit = int(current_user.role[5])
    if role_bit not in (1, 2):
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    device = dbapi.get_device(imei=imei)
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND)
    if role_bit == 1:
        god_agents = dbapi.get_god_agent(god_id=god.id)
        g = (god_agent.agent_id for god_agent in god_agents)
        if device.agent_id not in g:
            raise ApiError('ERROR_DEVICE_NOT_FOUND',
                           error.ERROR_DEVICE_NOT_FOUND)

    update = {}
    update['agent_id'] = 0
    update['address_id'] = 0
    update['salesman_agent_id'] = 0
    update['l4'] = 1
    update['l3'] = 0
    update['l2'] = 0
    update['l1'] = 0

    device = dbapi.update_device(device, **update)

    # device_distribution
    dbapi.delete_device_distribution_total(imei)

    # device_distribution_salesman
    dbapi.delete_device_distribution_salesman(imei)

    # device_product
    dbapi.delete_device_product(device.id)

    # pay
    dbapi.delete_pays_and_records(imei)

    try:
        db.session.commit()
    except:
        db.session.rollback()
        raise

    return make_response(jsonify(reply), status_code)
Пример #2
0
def get_god_info_common(agent):
    dbg('get_god_info_common')
    God = dbapi.God
    if 'God' in str(agent):
        god = agent
        god_id = god.id
    else:
        # 1. 先查agent_info是否存在
        agent_info = dbapi.get_agent_info(agent_id=agent.id)
        if agent_info:
            return agent_info

        # 2. 若不存在,则查god_info
        while agent.hook_agent_id != 0:
            agent = dbapi.get_agent(id=agent.hook_agent_id)
        god_agent = dbapi.get_god_agent(agent_id=agent.id)
        god = dbapi.get_god(id=god_agent.god_id)
        god_id = god.id

    god_info = dbapi.get_god_info(god_id=god_id)
    if not god_info:
        god_info = dbapi.make_new_god_info(god_id)
        dbapi.db.session.commit()

    return god_info
Пример #3
0
def god_get_device():
    dbg('god_get_device')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imei = data.get('imei')
        device_id = data.get('device_id')
        dbg(data)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    god = dbapi.get_god(openluat_user_id=current_user.id)
    if not god:
        raise ApiError('ERROR_GOD_NOT_FOUND', error.ERROR_GOD_NOT_FOUND)

    role_bit = int(current_user.role[5])
    if role_bit not in (1, 2):
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    device = None
    if imei:
        device = dbapi.get_device(imei=imei)
    elif device_id:
        device = dbapi.get_device(id=int(device_id))
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND)
    if role_bit == 1:
        god_agents = dbapi.get_god_agent(god_id=god.id)
        g = (god_agent.agent_id for god_agent in god_agents)
        if device.agent_id not in g:
            raise ApiError('ERROR_DEVICE_NOT_FOUND',
                           error.ERROR_DEVICE_NOT_FOUND)

    device_address = dbapi.get_device_address(id=device.address_id)
    address = ""
    if device_address:
        address = device_address.address
    agentname = ''
    agent = dbapi.get_agent(id=device.owner_agent_id)
    if agent:
        agentname = agent.name

    reply['data'] = {
        'id': device.id,
        'imei': device.imei,
        'address': address,
        'operator': agentname,
        'remark': device.remark
    }

    return make_response(jsonify(reply), status_code)
Пример #4
0
def update_agent():
    dbg('update_agent')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        agent_id = data['agent_id']
        update = data['update']

        keys = ('name', 'desc', 'address', 'remark')
        for key in update:
            assert (key in keys)

    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    role_bit = int(current_user.role[5])
    if role_bit != 1:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    agent = dbapi.get_agent(id=agent_id)
    god = dbapi.get_god(openluat_user_id=current_user.id)
    god_agent = dbapi.get_god_agent(agent_id=agent_id)
    if god.id != god_agent.god_id:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    agent = dbapi.update_agent(agent, **update)

    try:
        db.session.commit()
    except:
        db.session.rollback()
        raise

    return make_response(jsonify(reply), status_code)
Пример #5
0
def get_agent_admin(agent):
    dbg('get_top_agent')
    top_agent = get_top_agent(agent)
    god_agent = dbapi.get_god_agent(agent_id=agent.id)
    return god_agent
Пример #6
0
def get_god_devices():
    dbg('get_god_devices')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        god_id = data['god_id']
        page = request.args.get('page', 1, type=int)
        psize = request.args.get('psize', 10, type=int)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    role_bit = int(current_user.role[5])
    if role_bit != 2:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    count = 0
    data = []

    ct1, agents = dbapi.get_god_agent(god_id=god_id, page=1, psize=999)
    for agent in agents:
        agent_id = agent.id
        ct2, devices = dbapi.get_device(agent_id=agent_id,
                                        page=page,
                                        psize=psize)
        if ct2:
            count += ct2
            for device in devices:
                device_address = dbapi.get_device_address(id=device.address_id)
                address = ""
                if device_address:
                    address = device_address.address
                operator = ''
                operator_level = 0
                agent = dbapi.get_agent(id=device.owner_agent_id)
                if agent:
                    operator = agent.name
                    operator_level = agent.level
                imei = device.imei
                status = dbapi.get_cws_device_status(imei)
                online = -1
                if status:
                    online = status.get('online', -1)
                latest_report = dbapi.get_device_latestreport(imei)
                if not latest_report:
                    online = 0
                else:
                    if imei == '868575021151255':
                        dbg(latest_report['time'])
                    nowts = datetime.now().timestamp()
                    lrts = latest_report['time'].timestamp()
                    if nowts - lrts > 150:
                        if online == 1:
                            online = 0
                info = {
                    'id': device.id,
                    'imei': device.imei,
                    'cat': device.cat,
                    'address': address,
                    'use_state': 0,  # 0空闲,1使用
                    'comm_state': online,  # 0关机,1在线
                    'operator': operator,
                    'operator_level': operator_level,
                    'owner_agent_id': device.owner_agent_id,
                    'map_display': device.map_display,
                    'remark': device.remark,
                    'l4': device.l4,
                    'l3': device.l3,
                    'l2': device.l2,
                    'l1': device.l1
                }
                data.append(info)

    reply['data'] = {'count': count, 'devices': data}

    return make_response(jsonify(reply), status_code)
Пример #7
0
def get_agent_or_god(role):
    dbg('get_agent_or_god')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    page = int(request.args.get('page', 1))
    psize = int(request.args.get('psize', 10))

    role_bit = int(current_user.role[5])

    if role_bit == 2:
        try:
            assert (role == 'god')
        except:
            print_exception_info()
            raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

        count, gods = dbapi.get_god(role=1, page=page, psize=psize)
        data = []
        if count and gods:
            for god in gods:
                info = {
                    'id': god.id,
                    'name': god.name,
                    'phone': god.phone,
                    'email': god.email,
                    'remark': god.remark
                }
                data.append(info)

        reply['data'] = {'count': count, 'gods': data}

    elif role_bit == 1:
        try:
            assert (role == 'agent')
        except:
            print_exception_info()
            raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

        openluat_user_id = current_user.id
        god = dbapi.get_god(openluat_user_id=openluat_user_id)
        count, res = dbapi.get_god_agent(god_id=god.id, page=page, psize=psize)
        data = []
        if count and res:
            for god_agent in res:
                agent_id = god_agent.agent_id
                agent = dbapi.get_agent(id=agent_id)
                if not agent:
                    continue
                info = {
                    'id': agent.id,
                    'name': agent.name,
                    'level': agent.level,
                    'phone': agent.phone,
                    'address': agent.address,
                    'expandable': agent.expandable,
                    'remark': agent.remark
                }
                data.append(info)

        reply['data'] = {'count': count, 'agents': data}

    elif role_bit == 0:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    return make_response(jsonify(reply), status_code)