Esempio n. 1
0
def replied_topic(user_id):
    # O(k)+O(m*n)
    # rs = Reply.all(user_id=user_id)
    # ts = []
    # for r in rs:
    #     t = Topic.one(id=r.topic_id)
    #     ts.append(t)
    # return ts
    #
    #     sql = """
    # select * from topic
    # join reply on reply.topic_id=topic.id
    # where reply.user_id=1
    # """
    k = 'replied_topic_{}'.format(user_id)
    if cache.exists(k):
        v = cache.get(k)
        ts = json.loads(v)
        return ts
    else:
        rs = Reply.all(user_id=user_id)
        ts = []
        for r in rs:
            t = Topic.one(id=r.topic_id)
            ts.append(t)

        v = json.dumps([t.json() for t in ts])
        cache.set(k, v)

        return ts
Esempio n. 2
0
def reset_view():
    token = request.args.get('token')
    log('reset view', token)

    if cache.exists(token):
        return render_template('reset.html', token=token)
    else:
        return redirect('404')
Esempio n. 3
0
def view():
    token = request.args.get('token', '')
    k = 'csrf_tokens_{}'.format(token)

    if not cache.exists(k):
        abort(401)

    return render_template('reset/view.html', token=token)
Esempio n. 4
0
def reset():
    token = request.form.get('token')
    password = request.form.get('password')
    if cache.exists(token):
        u = User.one(id=cache.get(token))
        u.reset_password(password)
        cache.delete(token)
        return redirect(url_for('index.index'))
    else:
        redirect('404')
Esempio n. 5
0
def update():
    form = request.form.to_dict()
    password = form['password']
    token = form['token']
    if cache.exists(token):
        u = User.one(id=int(cache.get(token)))
        form['password'] = User.salted_password(password)
        form['updated_time'] = int(time.time())
        form.pop('token')
        User.update(u.id, **form)
    return redirect(url_for('.index'))
Esempio n. 6
0
def reset_view():
    token = request.args["token"]
    key = "csrf_token_{}".format(token)
    if cache.exists(key):
        user_id = cache.get(key)
        cache.delete(key)
        user = User.one(id=user_id)
        token = new_csrf_token(user)
        return render_template('reset.html', token=token, user=user)
    else:
        return abort(401)
Esempio n. 7
0
def created_topic(user_id):
    k = 'created_topic_{}'.format(user_id)
    if cache.exists(k):
        v = cache.get(k)
        ts = json.loads(v)
        ts = [Topic(**t) for t in ts]
        return ts
    else:
        ts = Topic.all(user_id=user_id)
        v = json.dumps([t.json() for t in ts])
        cache.set(k, v)
        return ts
Esempio n. 8
0
def update():
    token = request.args["token"]
    key = "csrf_token_{}".format(token)
    username = request.form["username"]
    user = User.one(username=username)
    log("reset update:", user.id, cache.get(key))
    new_ps = request.form['password']
    if cache.exists(key) and int(cache.get(key)) == user.id:
        user.password = User.salted_password(new_ps)
        user.save()
        return redirect('/')
    else:
        return abort(401)
Esempio n. 9
0
def replied_topic(user_id):
    k = 'replied_topic_{}'.format(user_id)
    if cache.exists(k):
        v = cache.get(k)
        ts = json.loads(v)
        return ts
    else:
        rs = Reply.all(user_id=user_id)
        ts = []
        for r in rs:
            t = Topic.one(id=r.topic_id)
            ts.append(t)

        v = json.dumps([t.json() for t in ts])
        cache.set(k, v)

        return ts
Esempio n. 10
0
def replied_topic(user_id):
    # 避免ORM的n+1问题
    k = 'replied_topic_{}'.format(user_id)
    if cache.exists(k):
        v = cache.get(k)
        ts = json.loads(v, object_hook=topichook)
        return ts
    else:
        # 调用SQLAlchemy join拼接table;filter查询;order_by排序;desc()倒序
        # 解决ORM的n+1问题
        ts = Topic.query.join(Reply, Reply.topic_id == Topic.id)\
                        .filter(Reply.user_id == user_id)\
                        .order_by(Reply.created_time.desc())\
                        .all()
        # rs = Reply.all(user_id=user_id)
        # ts = []
        # for r in rs:
        #     t = Topic.one(id=r.topic_id)
        #     ts.append(t)
        # ts = sorted(ts, key=lambda x: x.updated_time, reverse=True)
        v = json.dumps([t.json() for t in ts])
        cache.set(k, v)
        return ts
Esempio n. 11
0
def view():
    token = request.args.get('token')
    if cache.exists(token):
        return render_template('seek_secret.html', token=token)
    else:
        abort(404)