Esempio n. 1
0
def generate_fake_data():
    # 创建用户
    user1_form = dict(username='******', password='******')
    u1 = User.register(user1_form)
    user_form = dict(username='******', password='******')
    User.register(user_form)

    # 创建板块
    board1_form = dict(title='交流')
    b1 = Board.new(board1_form)
    board2_form = dict(title='灌水')
    Board.new(board2_form)

    # 创建话题
    # 创建大规模文本假数据,将假数据放到单独文件里
    with open('markdown_demo.md', encoding='utf8') as f:
        content = f.read()
    topic_form = dict(title='markdown demo', content=content, board_id=b1.id)
    t = Topic.add(topic_form, u1.id)

    # 创建回复
    reply_form = dict(
        content='reply test',
        topic_id=t.id,
    )
    for j in range(3):
        Reply.add(reply_form, u1.id)
Esempio n. 2
0
File: reset.py Progetto: MLong54/bbs
def generate_fake_date():
    form = dict(
        username='******',
        password='******',
    )
    guest = dict(
        username='******',
        password='******',
        signature='喜欢的话,就注册账号来玩吧',
    )
    User.register(guest)
    u = User.register(form)

    form = dict(title='Python')
    b = Board.new(form)
    with open('markdown_demo.md', encoding='utf8') as f:
        content = f.read()
    topic_form = dict(title='markdown demo', board_id=b.id, content=content)

    # for i in range(100):
    # print('begin topic <{}>'.format())
    t = Topic.new(topic_form, u.id)

    reply_form = dict(
        content='reply test',
        topic_id=t.id,
    )
    for j in range(2):
        Reply.new(reply_form, u.id)
Esempio n. 3
0
def generate_fake_data():
    u = User.new(username='******', password='******')

    b = Board.new(title='demo')
    Board.new(title='demo2')

    with open('markdown_demo.md', encoding='utf8') as f:
        content = f.read()
    form = dict(title='markdown demo', content=content, board_id=b.id)
    Topic.new(form, u.id)
    form = dict(title='板块测试 demo2', content='**biu biu biu**', board_id='2')
    Topic.new(form, u.id)
    form = dict(
        topic_id=1,
        content='**hello world**',
    )

    u = User.new(
        username='******',
        password='******',
    )

    Reply.new(form, u.id)
    form = dict(
        topic_id=2,
        content='**hello world2**',
    )
    Reply.new(form, u.id)

    form = dict(title='hey', content='happy', sender_id=1, receiver_id=2)
    Messages.new(**form)
Esempio n. 4
0
def generate_fake_date():
    form = dict(
        username='******',
        password='******',
    )
    u, result = User.register(form)

    form = dict(
        username='******',
        password='******',
    )
    u, result = User.register(form)

    form = dict(title='日常吐槽')
    b = Board.new(form)
    with open('markdown_demo.md', encoding='utf8') as f:
        content = f.read()
    topic_form = dict(title='markdown 示例', board_id=b.id, content=content)

    for i in range(2):
        print('begin topic <{}>'.format(i))
        t = Topic.new(topic_form, u.id)

        reply_form = dict(
            content='测试',
            topic_id=t.id,
        )
        for j in range(3):
            Reply.new(reply_form, u.id)
Esempio n. 5
0
def generate_fake_date():

    form = dict(username='******', password='******')
    u = User.register(form)

    form = dict(username='******', password='******')
    u = User.register(form)

    form = dict(title='篮球')
    b = Board.new(form)

    form = dict(title='面经')
    b = Board.new(form)

    form = dict(title='问答')
    b = Board.new(form)

    with open('markdown_demo.md', encoding='utf8') as f:
        content = f.read()
    form = dict(title='markdown demo', board_id=b.id, content=content)

    for i in range(20):
        log('begin topic <{}>'.format(i))
        t = Topic.add(form, u.id)

        reply_form = dict(
            content='reply test',
            topic_id=t.id,
        )
        for j in range(10):
            Reply.new(reply_form, u.id)
Esempio n. 6
0
def generate_fake_date():
    form = dict(username='******',
                password='******',
                email='*****@*****.**')
    u = User.register(form)

    form = dict(username='******',
                password='******',
                email='*****@*****.**')
    u = User.register(form)

    form = dict(title='all')
    b = Board.new(form)
    with open('markdown_demo.md', encoding='utf8') as f:
        content = f.read()
    topic_form = dict(title='markdown demo', board_id=b.id, content=content)

    for i in range(10):
        print('begin topic <{}>'.format(i))
        t = Topic.add(topic_form, u.id)

        reply_form = dict(
            content='I am Iron Man',
            topic_id=t.id,
        )
        for j in range(5):
            Reply.add(reply_form, u.id)
Esempio n. 7
0
def generate_fake_date():
    form = dict(
        username='******',
        password='******',
    )
    u = User.register(form)

    form = dict(
        username='******',
        password='******',
    )
    u = User.register(form)

    board_titles = ['问答', '精华', '水区']
    forms = [{'title': title} for title in board_titles]
    for form in forms:
        b = Board.new(form)

    with open('static/markdown.md', encoding='utf8') as f:
        content = f.read()
    topic_form = dict(title='markdown demo', board_id=1, content=content)

    for i in range(10):
        print('begin topic <{}>'.format(i))
        t = Topic.new(topic_form, u.id)

        reply_form = dict(
            content='reply test',
            topic_id=t.id,
        )
        for j in range(5):
            Reply.new(reply_form, u.id)
Esempio n. 8
0
def generate_fake_date():
    form = dict(
        username='******',
        password='******',
    )
    u = User.register(form)

    form = dict(
        username='******',
        password='******',
    )
    u = User.register(form)

    form = dict(
        username='******',
        password='******',
    )
    u = User.register(form)

    form = dict(title='all')
    b = Board.new(form)
    with open('markdown_demo.md', encoding='utf8') as f:
        content = f.read()
    topic_form = dict(title='markdown demo', board_id=b.id, content=content)

    for i in range(10):
        print('begin topic <{}>'.format(i))
        t = Topic.new(topic_form, u.id)

        reply_form = dict(
            content='reply test',
            topic_id=t.id,
        )
        for j in range(5):
            Reply.new(reply_form, u.id)
Esempio n. 9
0
def delete(reply_id):
    r = Reply.find_by(id=reply_id)
    u = current_user()
    if u is None or u.id != r.user_id:
        return redirect(url_for('index.index'))
    Reply.delete(reply_id)
    return redirect(url_for('topic.detail', id=r.topic_id, u=u))
Esempio n. 10
0
def cached_replies_by_topic_id(topic_id):
    """
    根据Topic id ,返回属于该Topic的所有Reply对象。

    如果缓存命中,从缓存中拿到包含了多个字典的列表,每个字典都生成一个对应的Reply对象。
    如果缓存穿透,从数据库中查询,拿到多个Reply对象,将每个Reply经json序列化后建立缓存。
    """
    key = 'topic_id_{}.replies'.format(topic_id)
    try:
        # 缓存命中
        replies_json = data_cache[key]
    except KeyError:
        # 缓存未命中,数据库中拉取数据
        replies = list(Reply.all(topic_id=topic_id))
        replies.sort(key=lambda x: x.created_time)
        # 数据库中拿到的数据json序列化后建立缓存,一个Reply对应一个字典
        data_cache.set(key, json.dumps([r.json() for r in replies]), 900)
        log('缓存丢失,向数据库拉取数据,重建缓存')
        return replies
    else:
        # 反序列化后得到字典,使用字典生成Reply对象
        replies = [Reply.get_model(r) for r in json.loads(replies_json)]
        replies.sort(key=lambda x: x.created_time)
        log('缓存命中,直接使用')
        return replies
Esempio n. 11
0
 def delete_model(self, model):
     ts = Topic.all(user_id=model.id)
     for t in ts:
         Topic.delete(t.id)
     Reply.delete(user_id=model.id)
     User.delete(id=model.id)
     return True
Esempio n. 12
0
 def delete(cls, id):
     # 删除帖子下所有评论
     rs = Reply.all(topic_id=id)
     for r in rs:
         Reply.delete(r.id)
     # 删除帖子
     super().delete(id)
     return '成功删除'
Esempio n. 13
0
def delete():
    u = current_user()
    topic = Topic.one(id=int(request.args['topic_id']))
    reply = Reply.one(id=int(request.args['reply_id']))
    if u.id == int(topic.user_id) or u.id == int(reply.user_id):
        Reply.delete(reply.id)
        return redirect(url_for('route_topic.detail', id=topic.id))
    else:
        return abort(Response('您无权进行此操作'))
Esempio n. 14
0
def delete():
    id = int(request.args.get('id'))
    u = current_user()
    print('删除 topic 用户是', u, id)
    Topic.delete(id)
    m = Reply.all(topic_id=id)
    for i in m:
        Reply.delete(i.id)
    return redirect(url_for('.index'))
Esempio n. 15
0
 def delete(cls, id):
     """
     删除话题,并且将对应的回复删除
     """
     from models.reply import Reply
     m = Topic.find_by(id=id)
     rs = m.replies()
     for r in rs:
         Reply.delete(r.id)
     super().delete(id)
     return m
Esempio n. 16
0
def reply_add():
    u = current_user()

    form = turn_to_dict()
    form['writer_id'] = u.id

    Reply.new(form)
    return redirect(
        url_for('.topic_content',
                topic_id=form['topic_id'],
                _anchor='reply_form'))
Esempio n. 17
0
def replies():
    u_id = int(request.args.get('id'))
    u = User.find_by(id=u_id)
    if u is None:
        abort(404)
    else:
        page = int(request.args.get('page', 1))
        t_ids = Reply.find_distinct(key='topic_id', page=page, user_id=u.id)
        ts = [Topic.find_by(id=t_id) for t_id in t_ids]
        pages = Reply.pages(__distinct='topic_id', user_id=u.id)
        return render_template('user/replies.html', user=u, ts=ts, page=page, pages=pages)
Esempio n. 18
0
def delete():
    reply_id = request.args['reply_id']
    r = Reply.one(id=reply_id)
    topic_id = r.topic_id
    u = current_user()
    if u.id == r.user_id or u.role == 'admin':
        Reply.delete(id=reply_id)
        r = '删除评论成功'
        return redirect(url_for('topic.detail', id=topic_id, result=r))
    else:
        r = '没有权限'
        return redirect(url_for('topic.detail', id=topic_id, result=r))
Esempio n. 19
0
def delete():
    id = int(request.args.get('id'))
    token = request.args.get('token')
    u = current_user()
    if u.id == 1 or u.id == Topic.find(id).user_id:
        # 管理员或话题创建者才有权限
        if token in csrf_tokens and csrf_tokens[token] == u.id:
            # 验证token
            Topic.delete(id)
            Reply.delete_all(dict(topic_id=id))
    csrf_tokens.pop(token)
    return redirect(url_for('index.index'))
Esempio n. 20
0
def delete_topic():
    topic_title = request.form['title']
    t = Topic.one(title=topic_title)
    topic_id = t.id
    user_id = t.user_id
    content = '您的帖子《{}》已被管理员删除'.format(t.title)
    form = dict(receiver_id=user_id,
                sender_id=current_user().id,
                content=content)
    Messages.new(form)
    Topic.delete(id=topic_id)
    Reply.delete(topic_id=topic_id)
    return redirect(url_for('.index'))
Esempio n. 21
0
def delete():
    reply_id = request.args.get('id')
    r = Reply.one(id=reply_id)
    topic_id = r.topic_id
    topic = Topic.one(id=topic_id)

    u = current_user()
    # 进行删帖权限判断:
    if u.is_admin() or u.id in [topic.user_id, r.user_id]:
        Reply.delete(reply_id)
        return redirect(url_for('topic.detail', id=topic_id))
    else:
        return render_template('no_permission.html', u=u)
Esempio n. 22
0
def generate_fake_data():
    Role.new(dict(role='guest'))
    Role.new(dict(role='normal'))
    guest = dict(
        username='******',
        password='',
        image='/static/avatar/guest.jpg',
        role_id=1,
    )
    User.register(guest)

    form1 = dict(
        username='******',
        password='******',
        image='/static/avatar/avatar.jpg',
        signature='No code, no life',
        role_id=2,
    )
    u = User.register(form1)
    form2 = dict(
        username='******',
        password='******',
        image='/static/avatar/avatar2.jpg',
        role_id=2,
    )
    User.register(form2)

    board = dict(title='all')
    b = Board.new(board)

    with open('markdown_demo.md', encoding='utf8') as f:
        content = f.read()
    topic_form = dict(
        title='markdown demo',
        content=content,
        board_id=b.id,
        user_id=u.id,
    )

    for i in range(10):
        print('begin topic <{}>'.format(i))
        t = Topic.new(topic_form)

        reply_form = dict(
            content='reply test',
            topic_id=t.id,
            user_id=u.id,
        )
        for j in range(5):
            Reply.new(reply_form)
Esempio n. 23
0
File: topic.py Progetto: Jimyfar/bbs
def delete():
    u = current_user()
    created = 'created_topic_{}'.format(u.id)
    replied = 'replied_topic_{}'.format(u.id)
    cache.delete(created, replied)

    id = int(request.args.get('id'))
    print('删除 topic 用户是', u, id)
    t = Topic.one(id=id)
    for r in t.replies():
        Reply.delete(r.id)

    Topic.delete(id)
    return redirect(url_for('.index'))
Esempio n. 24
0
def generate_fake_data():
    # cn_fake = Faker(locale='zh_CN')
    fake = Faker()
    form = dict(
        username='******',
        password='******',
    )
    test_user = User.register(form)
    if not test_user:
        print('test user create fail')

    form = dict(title='all')
    b = Board.new(form)

    for i in range(50):
        form = dict(username=fake.name(), password=fake.password())
        User.register(form)

    for i in range(100):
        print('fake data:', i)
        topic_form = dict(
            title=fake.sentence(nb_words=randint(4, 6)),
            board_id=b.id,
            content=fake.text(max_nb_chars=400),
            views=randint(10, 100),
        )
        t = Topic.new(topic_form, randint(1, 51))

        for j in range(randint(4, 10)):
            reply_form = dict(
                content=fake.text(max_nb_chars=200),
                topic_id=t.id,
            )
            Reply.new(reply_form, randint(1, 51))

    with open('static/doc/markdown_demo.md', encoding='utf8') as f:
        content = f.read()
        b = Board.new(dict(title='demo'))
        topic_form = dict(
            title='markdown 示例',
            board_id=b.id,
            content=content,
            views=randint(10, 100),
        )
        t = Topic.new(topic_form, randint(1, 51))
        reply_form = dict(
            content=content,
            topic_id=t.id,
        )
        Reply.new(reply_form, randint(1, 51))
Esempio n. 25
0
def delete():
    id = int(request.args.get('id'))
    u = current_user()
    print('删除 topic 用户是', u, id)
    topic = Topic.one(id=id)
    uid = topic.user_id
    if u.id == uid:
        rs = topic.replies()
        for r in rs:
            Reply.delete(r.id)
        Topic.delete(id)
        return redirect(url_for('.index'))
    else:
        flash('没有权限', 'err')
        return redirect(url_for('.index'))
Esempio n. 26
0
def add():
    form = request.form
    u = current_user()
    print(form)
    m = Reply.new(form)
    m.set_user_id(u.id, form)
    return redirect(url_for('topic.detail', id=m.topic_id))
Esempio n. 27
0
def add():
    uid = session.get('user_id')
    if uid is None:
        user = User.guest()
    else:
        user = User.one(id=uid)
        if user is None:
            user = User.guest()

    id = user.id
    form = request.form

    data = {
        'content': form['content'],
        'user_id': id,
        'topic_id': form['topic_id'],
    }
    tid = form.get('target_id')
    if tid is not None:
        data['target_id'] = tid

    r = Reply.new(data)

    # 添加 at 的信息
    users = users_from_content(form['content'])
    for u in users:
        d = {
            'user_id': id,
            'receiver_id': u.id,
            'reply_id': r.id,
            'title': '你被 {} at 了'.format(user.username),
        }
        Message.new(d)

    return jsonify(r.json())
Esempio n. 28
0
def update():
    form = request.form
    r_id = int(form.get('id'))
    r = Reply.find_by(id=r_id)
    r.update_reply(form)
    flash('修改回复成功', 'success')
    return redirect(form.get('next'))
Esempio n. 29
0
def add():
    form = request.form
    u = current_user()
    if u is None:
        u = Visitor.singleton()
    t = Reply.new(form, user_id=u.id)
    return redirect(url_for('topic.detail', id=t.topic_id))
Esempio n. 30
0
def cached_replied_topics(user_id):
    """
    根据user id 返回该用户回复的Topic对象。

    如果缓存命中,从缓存中拉取包含多个Topic id的列表,根据每个Topic id调用cached_topic_id2topic得到Topic model。
    如果缓存穿透,则从数据库查询,拿到多个Topic对象,将所有Topic id 序列化后存储到redis,返回多个Topic对象。
    """
    key = 'user_id_{}.replied_topics'.format(user_id)
    try:
        # 缓存命中
        topics_ids_json = data_cache[key]
    except KeyError:
        # 缓存未命中,数据库中拉取数据
        # ORM 的n+1老问题。。。
        replies = list(Reply.all(user_id=user_id))
        topics_ids = [reply.topic_id for reply in replies]
        # 加到redis缓存中的是topic id
        data_cache.set(key, json.dumps(topics_ids), 1800)
        topics_models = [
            cached_topic_id2topic(topic_id) for topic_id in topics_ids
        ]
        topics_models.sort(key=lambda x: x.last_active_time, reverse=True)
        log('缓存丢失,向数据库拉取数据,重建缓存')
        return topics_models
    else:
        # 缓存命中则根据缓存中的多个Topic id,再调用 cached_topic_id2topic 获得多个Topic对象,排序后返回。
        topics_models = [
            cached_topic_id2topic(topic_id)
            for topic_id in json.loads(topics_ids_json)
        ]
        topics_models.sort(key=lambda x: x.last_active_time, reverse=True)
        log('缓存命中,直接使用')
        return topics_models
Esempio n. 31
0
def user_detail(username):
    u = User.find_by(username=username)
    user_id = u.id
    ms = Topic.find_all(user_id=user_id)
    ms.reverse()
    rs = Reply.find_all(user_id=user_id)
    rs.reverse()
    if u is None:
        abort(404)
    else:
        return render_template('new_profile.html', u=u, ms=ms, rs=rs)
Esempio n. 32
0
def add():
    log('reply add')
    form = request.form
    u = current_user()
    # 发邮件
    log('before send mail', form)
    content = form.get('r_content')
    log('reply', content)
    users = users_from_content(content)
    log('reply', users)
    send_mails(u, users, content)
    m = Reply.new(form,content = content, user_id=u.id)
    log('after send mail', m)
    return redirect(url_for('topic.detail', id=m.topic_id))