Esempio n. 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)
Esempio n. 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
Esempio n. 3
0
def update_god_info():
    dbg('update_god_info')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        update = request.form['update']
        update = json.loads(update)
        invalid_keys = ('logo')
        for key in invalid_keys:
            assert (key not in update)

        imag_file = request.files.get('0')
        if imag_file:
            postfix = request.form['postfix']

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

    god = dbapi.get_god(openluat_user_id=current_user.id)
    god_id = god.id

    if not update:
        update = {}

    god_info = dbapi.get_god_info(god_id=god_id)
    if not god_info:
        god_info = dbapi.make_new_god_info(god_id)
        try:
            db.session.commit()
        except:
            db.session.rollback()
            raise

    if imag_file:
        imag_filename = 'logo%d.%s' % (god_id, postfix)
        imag_filepath = os.path.join('app/static/img/logo', imag_filename)
        logo_img = os.path.join('img/logo', imag_filename)
        if not os.path.exists('app/static/img/logo'):
            os.mkdir('app/static/img/logo')
        dbg("save imag: %s" % imag_filepath)
        imag_file.save(imag_filepath)

        update['logo'] = logo_img

    god_info = dbapi.update_god_info(god_info, **update)

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

    return make_response(jsonify(reply), status_code)
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
0
def add_agent_or_god(role):
    dbg('add_agent_or_god')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    try:
        data = request.get_json()
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    role_bit = int(current_user.role[5])

    if role_bit == 2:
        try:
            assert (role == 'god')
            name = data['name']
            email = data['email']
            phone = data['phone']
            remark = data['remark']
        except:
            print_exception_info()
            raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

        target_god = dbapi.get_god(phone=phone)
        if target_god:
            raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS)

        target_god = dbapi.get_god(email=email)
        if target_god:
            raise ApiError('ERROR_EMAIL_EXISTS', error.ERROR_EMAIL_EXISTS)

        openluat_user = dbapi.get_openluat_user(phone=phone)
        if not openluat_user:
            openluat_user = dbapi.make_new_openluat_user(name,
                                                         email,
                                                         phone,
                                                         role='000001000000')
        else:
            openluat_user.role[5] = '1'
            db.session.add(openluat_user)

        db.session.commit()

        openluat_user_id = openluat_user.id
        new_god = dbapi.make_new_god(openluat_user_id, name, phone, email,
                                     remark)
        db.session.commit()

    elif role_bit == 1:
        try:
            assert (role == 'agent')
            level = 4
            name = data['name']
            email = data['email']
            phone = data['phone']
            address = data['address']
            remark = data['remark']
            expandable = 1
            withdrawable = 1
        except:
            print_exception_info()
            raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

        target_agent = dbapi.get_agent(phone=phone)
        if target_agent:
            raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS)

        target_agent = dbapi.get_agent(email=email)
        if target_agent:
            raise ApiError('ERROR_EMAIL_EXISTS', error.ERROR_EMAIL_EXISTS)

        openluat_user = dbapi.get_openluat_user(phone=phone)
        if not openluat_user:
            openluat_user = dbapi.make_new_openluat_user(name, email, phone)
            db.session.commit()

        hook_agent_id = 0
        salesman = 0
        slevel = 0
        new_agent = dbapi.make_new_agent(salesman, openluat_user.id,
                                         hook_agent_id, level, slevel,
                                         expandable, withdrawable, name, phone,
                                         email, address, remark)
        db.session.commit()

        agent_setting = dbapi.make_new_agent_setting(new_agent.id)

        openluat_user_id = current_user.id
        god = dbapi.get_god(openluat_user_id=openluat_user_id)
        god_agent = dbapi.make_new_god_agent(god.id, new_agent.id)
        db.session.commit()

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

    return make_response(jsonify(reply), status_code)
Esempio n. 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)
Esempio n. 8
0
def web_login():
    dbg('web_login')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    try:
        data = request.get_json()
        phone = data['phone']
        pswd = data['pswd']
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    openluat_user = dbapi.get_openluat_user(phone=phone)
    if not openluat_user:
        raise ApiError('ERROR_NO_USER', error.ERROR_NO_USER)

    if not openluat_user.verify_password(pswd):
        raise ApiError('ERROR_WRONG_PSWD', error.ERROR_WRONG_PSWD)

    login_user(openluat_user)

    openluat_role = openluat_user.role[5]
    user_id = openluat_user.id
    data = {'role': 0}
    if openluat_role == '0':
        agent = dbapi.get_agent(user_id=user_id)
        if agent:
            god_info = get_god_info_common(agent)
            data['common'] = {
                'logo': url_for('static', filename=god_info.logo),
                'title': god_info.title
            }
            agent_id = agent.id
            joinup = check_joinup_available(agent_id)
            joinuped = get_joinup_status(agent_id)
            data['role'] = 2
            data['agent'] = {
                'id': agent.id,
                'salesman': agent.salesman,
                'level': agent.level,
                'slevel': agent.slevel,
                'expandable': agent.expandable,
                'withdrawable': agent.withdrawable,
                'name': agent.name,
                'phone': agent.phone,
                'email': agent.email,
                'desc': agent.desc,
                'address': agent.address,
                'remark': agent.remark,
                'joinup': joinup,
                'joinuped': joinuped
            }
        else:
            advertiser = dbapi.get_advertiser(user_id=user_id)
            if advertiser:
                god_info = get_god_info_common(advertiser)
                data['common'] = {
                    'logo': url_for('static', filename=god_info.logo),
                    'title': god_info.title
                }
                top_agent = get_top_agent(advertiser)
                data['role'] = 3
                data['advertiser'] = {
                    'id': advertiser.id,
                    'name': advertiser.name,
                    'phone': advertiser.phone,
                    'email': advertiser.email,
                    'desc': advertiser.desc,
                    'address': advertiser.address,
                    'remark': advertiser.remark
                }
    elif openluat_role in ('1', '2'):
        god = dbapi.get_god(openluat_user_id=user_id)
        if god:
            god_info = get_god_info_common(god)
            data['common'] = {
                'logo': url_for('static', filename=god_info.logo),
                'title': god_info.title
            }
            data['role'] = 1
            data['god'] = {
                'level': int(openluat_role),
                'id': god.id,
                'name': god.name
            }

    reply['data'] = data

    return make_response(jsonify(reply), status_code)
Esempio n. 9
0
def get_cur_user_info():
    dbg('get_cur_user_info')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    """
        role: 1god 2agent 3advertiser 0other
    """

    openluat_role = current_user.role[5]
    user_id = current_user.id
    data = {'role': 0}
    if openluat_role == '0':
        agent = dbapi.get_agent(user_id=user_id)
        if agent:
            god_info = get_god_info_common(agent)
            data['common'] = {
                'logo': url_for('static', filename=god_info.logo),
                'title': god_info.title
            }
            agent_id = agent.id
            joinup = check_joinup_available(agent_id)
            joinuped = get_joinup_status(agent_id)
            data['role'] = 2
            data['agent'] = {
                'id': agent.id,
                'salesman': agent.salesman,
                'level': agent.level,
                'slevel': agent.slevel,
                'expandable': agent.expandable,
                'withdrawable': agent.withdrawable,
                'name': agent.name,
                'phone': agent.phone,
                'email': agent.email,
                'desc': agent.desc,
                'address': agent.address,
                'remark': agent.remark,
                'joinup': joinup,
                'joinuped': joinuped
            }
        else:
            advertiser = dbapi.get_advertiser(user_id=user_id)
            if advertiser:
                god_info = get_god_info_common(advertiser)
                data['common'] = {
                    'logo': url_for('static', filename=god_info.logo),
                    'title': god_info.title
                }
                data['role'] = 3
                data['advertiser'] = {
                    'id': advertiser.id,
                    'name': advertiser.name,
                    'phone': advertiser.phone,
                    'email': advertiser.email,
                    'desc': advertiser.desc,
                    'address': advertiser.address,
                    'remark': advertiser.remark
                }
    elif openluat_role in ('1', '2'):
        god = dbapi.get_god(openluat_user_id=user_id)
        if god:
            god_info = get_god_info_common(god)
            data['common'] = {
                'logo': url_for('static', filename=god_info.logo),
                'title': god_info.title
            }
            data['role'] = 1
            data['god'] = {
                'level': int(openluat_role),
                'id': god.id,
                'name': god.name
            }

    reply['data'] = data

    return make_response(jsonify(reply), status_code)