Exemple #1
0
def delete_item(item_id):
    item = Item.query.get(item_id)
    if item is None:
        return jsonify(Res(0, 'item does not exists').raw())
    if item.delete():
        cache.delete_like(cache, 'get_item')  # 删除与item相关的缓存
        return jsonify(Res(1, 'delete item successfully').raw())
Exemple #2
0
def login():
    data = json.loads(str(request.data, encoding='utf-8'))
    if admin_auth(data):
        session['admin'] = True
        return jsonify(Res(1, 'login successfully').raw())
    else:
        return jsonify(Res(0, 'username or password not correct.').raw())
Exemple #3
0
def delete_user(user_id):
    user = User.query.get(user_id)
    if user is None:
        return jsonify(Res(0, 'user does not exists').raw())
    if user.delete():
        return jsonify(Res(1, 'delete user successfully').raw())
    else:
        return jsonify(Res(2, 'something error').raw())
Exemple #4
0
def edit_item(item_id):
    item = Item.query.get(item_id)
    if item is None:
        return jsonify(Res(0, 'item does not exists').raw())
    postData = json.loads(str(request.data, encoding='utf-8'))
    print('edit_item', postData)
    if item.edit(postData):
        return jsonify(Res(1, 'edit item successfully').raw())
    return jsonify(Res(2, 'something error').raw())
Exemple #5
0
def get_advices():
    try:
        data = get_yesterday_advice()
        if isinstance(data, bytes):  # 如果是bytes转成str -> json无法dumps bytes
            data = str(data, encoding='utf-8')
        return jsonify(Res(1, 'get advices successfully', data).raw())
    except Exception as e:
        print(e)
    return jsonify(Res(0, 'something error').raw())
Exemple #6
0
def get_by_userId(user_id):
    try:
        items = Item.query.filter_by(user_id=user_id).order_by(
            Item.time.desc()).all()
        data = [item.raw() for item in items]
        print(data)
        return jsonify(Res(1, 'get items successfully', data).raw())
    except Exception as e:
        print(e)
    return jsonify(Res(0, 'something error').raw())
Exemple #7
0
def get_users():
    try:
        users = User.query.all()
        users = [user.raw() for user in users]
        if users is not None:
            return jsonify(Res(1, 'get users successfully', users).raw())
        return jsonify(Res(0, 'get users failed').raw())
    except Exception as e:
        print(e)
        return jsonify(Res(2, 'something error').raw())
Exemple #8
0
def post(user_id):
    postData = json.loads(str(request.data, encoding='utf-8'))
    print('postData', postData)
    # TODO 数据校验
    user = User.query.get(user_id)
    if Item.createItemByPostData(postData, user_id) and user.update_tel(
            postData['tel']):
        cache.delete_like(cache, 'get_item')  # 有新增数据,删除item相关缓存
        return jsonify(Res(1, 'post item successfully').raw())
    return jsonify(Res(0, 'something error').raw())
Exemple #9
0
def get_course():
    try:
        username = request.form['stu_id']
        password = request.form['stu_pwd']
        res = get_course_info(username, password)
        if not res:
            return jsonify(Res(0, 'fail').raw())
        return jsonify(Res(1, 'success', res).raw())
    except Exception as e:
        return jsonify(Res(2, 'something error').raw())
Exemple #10
0
def update_contact(user_id):
    user = User.query_user_by_id(user_id)
    if user is None:
        return jsonify(Res(0, 'user does not exist').json())

    data = json.loads(str(request.data, encoding='utf-8'))
    print(data)
    if user.update_contact(data):
        return jsonify(Res(1, 'update contact successfully').json())
    else:
        return jsonify(Res(2, 'something error.').json())
Exemple #11
0
def get_items():
    try:
        page = int(request.args.get('page', -1))  # 默认是字符串
        query = Item.query.join(User)
        items = query.order_by(Item.time.desc()).offset(page * 8).limit(8).all() if page != -1 else \
            query.order_by(Item.time.desc()).all()
        data = [item.raw() for item in items]
        return jsonify(Res(1, 'get items successfully', data).raw())
    except Exception as e:
        print(e)
    return jsonify(Res(0, 'something error').raw())
Exemple #12
0
def advice():
    try:
        data = json.loads(str(request.data, encoding='utf-8'))
        user_id = str(data['user_id'])
        fmt_advice = format_advice(user_id, data['advice'])
        advice_route = get_advice_route()
        with open(advice_route, 'a') as f:
            f.write(fmt_advice)
        return jsonify(Res(1, 'post advice successfully').raw())
    except Exception as e:
        print(e)
        return jsonify(Res(2, 'something error').raw())
Exemple #13
0
def search(search_key):
    # TODO 缓存
    try:
        page = int(request.args.get('page'))  # 默认是字符串
        key = '%{}%'.format(search_key)
        print('key', key)
        query = Item.query.join(User)
        items = query.filter(Item.des.like(key)).order_by(
            Item.time.desc()).offset(page * 8).limit(8).all()
        data = [item.raw() for item in items]
        return jsonify(Res(1, 'search items successfully', data).raw())
    except Exception as e:
        print(e)
    return jsonify(Res(0, 'something error').raw())
Exemple #14
0
def search_by_post():
    # TODO  缓存
    try:
        data = json.loads(str(request.data, encoding='utf-8'))
        search_key = urllib.parse.unquote(data['search_key'])
        page = data['search_page']
        key = '%{}%'.format(search_key)
        print('key', key)
        query = Item.query.join(User)
        items = query.filter(Item.des.like(key)).order_by(
            Item.time.desc()).offset(page * 8).limit(8).all()
        data = [item.raw() for item in items]
        return jsonify(Res(1, 'search items successfully', data).raw())
    except Exception as e:
        print(e)
    return jsonify(Res(0, 'something error').raw())
Exemple #15
0
def auth():
    try:
        data = json.loads(str(request.data, encoding='utf-8'))
        print('..')
        user_id = data['user_id']
        print('..', data)
        stu_id = data['stu_id']
        stu_pwd = data['stu_pwd']
        print('...')
        # stu_id = request.form['stu_id']
        # stu_pwd = request.form['stu_pwd']
        if request_auth(stu_id, stu_pwd):
            user = User.query.get(user_id)
            if user.set_auth():
                return jsonify(Res(1, 'success').raw())
        else:
            return jsonify(Res(0, '密码错误').raw())
    except Exception as e:
        print(e)
        return jsonify(Res(2, 'something error').raw())
Exemple #16
0
def upload_img():
    print(request.files)
    if 'img' not in request.files:
        return jsonify(Res(0, '文件名不正确').raw())
    file = request.files['img']
    if file.filename == '':
        return jsonify(Res(0, '请先选择图片').raw())
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        try:
            path = get_fileRoute(filename)
            file.save(path)
            print(path)
            sep = os.path.sep
            ra = path.rsplit(sep, 3)
            print('ra', ra)
            r = url_for('static',
                        filename=(ra[-3] + '/' + ra[-2] + '/' + ra[-1]))
            print(r)
            data = dict(imgServerPath=r)
            return jsonify(Res(1, 'upload img successfully', data).raw())
        except RequestEntityTooLarge as e:
            return jsonify(Res(0, 'the img is too large').raw())
        except Exception as e:
            print(e)
            return jsonify(Res(0, 'something error').raw())
    else:
        return jsonify(Res(0, 'invalid extension').raw())
Exemple #17
0
def get(code):
    # 获得session_key, 用户的openId
    sessionApi = 'https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code'
    targetApi = sessionApi.format(app.config['APP_ID'],
                                  app.config['APP_SECRET'], code)

    # 返回值
    code = 1
    msg = 'user does not exist, but has been registered.'
    data = None

    res = HTTP.get(targetApi)  # dict
    print(res)
    # code出错
    if 'errcode' in res:
        code = 2
        msg = res['errmsg']
        res = Res(code, msg, data).raw()
        return jsonify(res)

    # 正常, 获得openId
    openId = res['openid']
    user = User.query_user_by_openId(openId)

    # 根据user构造返回值
    if user is not None:
        code = 1
        msg = 'user exist'
    else:
        user = User(openId)
        db.session.add(user)
        db.session.commit()

    print(user.id)
    data = user.raw()

    res = Res(code, msg, data).raw()

    return jsonify(res)
Exemple #18
0
def get_image(path, uri):
    sep = os.path.sep
    uri = path + sep + uri
    print('uri ', uri)
    imgPath = os.path.abspath(
        '.') + sep + 'app' + sep + 'static' + sep + 'uploads' + sep
    imgPath = imgPath + uri
    print('imgPath ', imgPath)
    mdict = {
        'jpeg': 'image/jpeg',
        'jpg': 'image/jpeg',
        'png': 'image/png',
        'gif': 'image/gif'
    }
    mime = mdict[((uri.split(sep)[1]).split('.')[1])]
    print('mime ', mime)
    if not os.path.exists(imgPath):
        return jsonify(Res(0, 'image does not exists').raw())
    with open(imgPath, 'rb') as f:
        image = f.read()
    return Response(image, mimetype=mime)
Exemple #19
0
def get_user(user_id):
    user = User.query.get(user_id)
    if user is None:
        return jsonify(Res(0, 'user does not exsits').raw())
    else:
        return jsonify(Res(1, 'get user successfully', user.raw()).raw())