Exemplo n.º 1
0
def post():
    if request.method == 'POST':
        content = request.form.get('content').strip()
        # 内容不能为空
        if not content:
            return render_template('post.html', error='微博内容不允许为空!')
        else:
            weibo = Weibo(uid=session['uid'], content=content)
            weibo.updated = datetime.datetime.now()
            db.session.add(weibo)
            db.session.commit()
            # 提交之后返回到查看当前微博页面
            return redirect('/weibo/show?wid=%s' % weibo.id)
    else:
        return render_template('post.html')
Exemplo n.º 2
0
 def publish(self, user):
     """ 发布微博 """
     data = self.data
     content = data['content']
     # 保存微博
     weibo_obj = Weibo(
         content=content,
         user=user,
         # user_id=user.id,
         created_at=datetime.now())
     db.session.add(weibo_obj)
     # 保存@关系
     at_users = re.findall(constants.AT_USER_PATTEN, content, re.M)
     for nickname in at_users:
         user = User.query.filter_by(nickname=nickname).first()
         if user:
             weibo_at_user = WeiboAtUser(weibo=weibo_obj, user_id=user.id)
             db.session.add(weibo_at_user)
     # 保存话题关系
     topics = re.findall(constants.TOPIC_PATTEN, content, re.M)
     for name in topics:
         topic = Topic.query.filter_by(name=name).first()
         if topic is None:
             topic = Topic(name=name, )
             db.session.add(topic)
         weibo_rel_topic = WeiboRelTopic(topic=topic, weibo=weibo_obj)
         db.session.add(weibo_rel_topic)
     db.session.commit()
     return weibo_obj
Exemplo n.º 3
0
def post_weibo():
    '''发布微博'''
    if request.method == 'POST':
        uid = session['uid']
        content = request.form.get('content')
        if not content:
            return render_template('/weibo/post.html', error='内容不能为空')
        created = datetime.datetime.now()
        updated = datetime.datetime.now()

        weibo = Weibo(uid=uid,
                      content=content,
                      created=created,
                      updated=updated)
        db.session.add(weibo)

        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)
            return render_template('/weibo/post.html', error='服务器内部错误')
        else:
            return redirect(f'/weibo/show?wid={weibo.id}')  # 微博显示页面
    else:
        return render_template('/weibo/post.html')
Exemplo n.º 4
0
def post():
    if request.mthod == 'POST':
        uid = session['uid']
        content = request.form.get('content', '').strip()
        now = datetime.datetime.now()
        if not content:
            return render_template('post.html', err='微博内容不允许为空')

        weibo = Weibo(uid=uid, content=content, created=now, updated=now)
        db.session.add(weibo)
        db.session.commit()
        return redirect('/')
    else:
        return render_template('post.html')
Exemplo n.º 5
0
Arquivo: views.py Projeto: xm181/weibo
def post_weibo():
    '''发布微博'''
    if request.method == "POST":
        uid = session['uid']
        content = request.form.get('content', '').strip()
        now = datetime.datetime.now()

        # 检查微博内容是否为空
        if not content:
            return render_template('post.html', err='微博内容不允许为空')

        weibo = Weibo(uid=uid, content=content, created=now, updated=now)
        db.session.add(weibo)
        db.session.commit()

        return redirect('/weibo/read?wid=%s' % weibo.id)
    else:
        return render_template('post.html')
Exemplo n.º 6
0
def insert_weibo_data():
    '''插入一些微博数据'''
    # 创建一批测试用户
    users = []
    for i in range(10):
        user = User(
            nickname='test-%x' % random.randint(100, 999),
            password=gen_password('1234567890'),
            gender=random.choice(['male', 'female']),
            bio=rand_content(5),
            city=random.choice(['北京', '上海', '深圳']),
            avatar='/static/img/logo.png',
            birthday='1970-02-%s' % random.randint(1, 28),
        )
        users.append(user)
        print('add user %s' % user.nickname)
    db.session.add_all(users)
    db.session.commit()
    user_id_list = [u.id for u in users]  # 取出这些用户的 ID

    # 添加一些微博
    wb_list = []
    for i in range(10000):
        # 随机的时间
        y = random.randint(2015, 2018)
        m = random.randint(1, 12)
        d = random.randint(1, 28)
        h = random.randint(0, 23)
        M = random.randint(0, 59)
        s = random.randint(0, 59)
        created = datetime.datetime(y, m, d, h, M, s)
        wb = Weibo(uid=random.choice(user_id_list),
                   content=rand_content(20),
                   created=created,
                   updated=created)
        wb_list.append(wb)
        print('add weibo: %s' % wb.content)
    db.session.add_all(wb_list)
    db.session.commit()
Exemplo n.º 7
0
def fake():
    import random
    from User.models import User
    from weibo.models import Weibo


    users = []

    for i in range(20):
        user = User(
            nickname=fake_word().title(),
            gender=random.choice(['male', 'female', 'unknow']),
            city=random.choice(['北京', '上海', '深圳']),
            avatar='/static/upload/125.png',
            birthday='1996-06-13',
            bio=fake_sentence(),
        )
        users.append(user)
    db.session.add_all(users)
    db.session.commit()

    weibo_list = []
    for i in range(1000):
        y = random.randint(2000, 2020)
        m = random.randint(1, 12)
        d = random.randint(1, 28)
        date = '%d-%02d-%02d' % (y, m, d)

        weibo = Weibo(
            uid=random.choice(users).id,
            content=fake_sentence(),
            created=date,
            updated=date,
        )
        weibo_list.append(weibo)

    db.session.add_all(weibo_list)
    db.session.commit()
Exemplo n.º 8
0
def post_weibo():
    '''发布微博'''
    if request.method == "POST":
        content = request.form.get('content', '').strip()
        if not content:
            return render_template('post.html', error='微博内容不能为空')
        uid = session['uid']
        created = updated = datetime.datetime.now()  # 创建时间和修改时间相同
        weibo = Weibo(uid=uid,
                      content=content,
                      created=created,
                      updated=updated)
        db.session.add(weibo)

        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)
            return render_template('post.html', error='服务器内部错误')
        else:
            return redirect(f'/weibo/show?wid={weibo.id}')
    else:
        return render_template('post.html')
Exemplo n.º 9
0
def create_test_weibo():
    '''创建微博测试数据'''
    users = User.fake_users(50)
    uid_list = [u.id for u in users]
    Weibo.fake_weibos(uid_list, 5000)