Esempio n. 1
0
def using_ads():
    dbg('using_ads')
    reply, status_code = {'code': 0, 'msg': ''}, 200

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

    agent_id = current_user.agent_id

    ads = dbapi.get_ad(agent_id=agent_id, using=1)
    for ad in ads:
        ad = dbapi.update_ad(ad, using=0)

    for ad_id in ad_list:
        ad = dbapi.get_ad(id=ad_id)
        if not ad:
            dbg(ad_id)
            raise ApiError('ERROR_AD_NOT_FOUND', error.ERROR_AD_NOT_FOUND)
        ad = dbapi.update_ad(ad, using=1)

    db.session.commit()

    return make_response(jsonify(reply), status_code)
Esempio n. 2
0
def get_ads():
    dbg('get_ads')
    reply, status_code = {'code': 0, 'msg': ''}, 200

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

    if imei:
        ads = dbapi.get_ad(imei=imei)
    else:
        agent_id = current_user.agent_id
        ads = dbapi.get_ad(agent_id=agent_id)

    data = []
    for ad in ads:
        info = {
            'id': ad.id,
            'name': ad.name,
            'desc': ad.desc,
            'img': url_for('static', filename=ad.img),
            'url': ad.url,
            'using': ad.using
        }
        data.append(info)

    reply['data'] = data

    return make_response(jsonify(reply), status_code)
Esempio n. 3
0
def get_ads():
    dbg('get_ads')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    try:
        data = request.get_json()
        imei = data['imei']
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    device = dbapi.get_device(imei=imei)
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND)
    ads = dbapi.get_ad(agent_id=device.owner_agent_id, using=1)
    data = []
    for ad in ads:
        info = {
            'id': ad.id,
            'name': ad.name,
            'desc': ad.desc,
            'img': url_for('static', filename=ad.img),
            'url': ad.url
        }
        data.append(info)

    reply['data'] = data

    return make_response(jsonify(reply), status_code)
Esempio n. 4
0
def update_ad():
    dbg('update_ad')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        ad_id = request.form['ad_id']
        name = request.form.get('name')
        desc = request.form.get('desc')
        url = request.form.get('url')
        using = request.form.get('using')
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    update = {}
    if name:
        update['name'] = name
    if desc:
        update['desc'] = desc
    if url:
        update['url'] = url
    if using:
        update['using'] = using

    agent_id = current_user.agent_id

    imag_file = request.files.get('0')
    if imag_file:
        imag_filename = 'ad%d_%s' % (agent_id,
                                     datetime.now().strftime('%y%m%d%H%M%S'))
        imag_filepath = os.path.join('app/static/img/ad', imag_filename)
        qrcode_img = os.path.join('img/ad', imag_filename)
        if not os.path.exists('app/static/img/ad'):
            os.mkdir('app/static/img/ad')
        dbg("save imag: %s" % imag_filepath)
        imag_file.save(imag_filepath)

        img = 'img/ad/' + imag_filename
        update['img'] = img

    ad = dbapi.get_ad(id=ad_id)
    if not ad:
        raise ApiError('ERROR_AD_NOT_FOUND', error.ERROR_AD_NOT_FOUND)
    ad = dbapi.update_ad(ad, **update)
    db.session.commit()

    return make_response(jsonify(reply), status_code)
Esempio n. 5
0
def delete_ad():
    dbg('delete_ad')
    reply, status_code = {'code': 0, 'msg': ''}, 200

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

    ad = dbapi.get_ad(id=ad_id)
    if not ad:
        raise ApiError('ERROR_AD_NOT_FOUND', error.ERROR_AD_NOT_FOUND)

    ad = dbapi.update_ad(ad, deleted=1)
    db.session.commit()

    return make_response(jsonify(reply), status_code)