Ejemplo n.º 1
0
 def test_follow_posts(self):
     # make four users
     u1 = User(nickname='john', email='*****@*****.**')
     u2 = User(nickname='susan', email='*****@*****.**')
     u3 = User(nickname='mary', email='*****@*****.**')
     u4 = User(nickname='david', email='*****@*****.**')
     db.session.add(u1)
     db.session.add(u2)
     db.session.add(u3)
     db.session.add(u4)
     # make four posts
     utcnow = datetime.utcnow()
     p1 = Post(body="post from john", author=u1,
               timestamp=utcnow + timedelta(seconds=1))
     p2 = Post(body="post from susan", author=u2,
               timestamp=utcnow + timedelta(seconds=2))
     p3 = Post(body="post from mary", author=u3,
               timestamp=utcnow + timedelta(seconds=3))
     p4 = Post(body="post from david", author=u4,
               timestamp=utcnow + timedelta(seconds=4))
     db.session.add(p1)
     db.session.add(p2)
     db.session.add(p3)
     db.session.add(p4)
     db.session.commit()
     # setup the followers
     u1.follow(u1)  # john follows himself
     u1.follow(u2)  # john follows susan
     u1.follow(u4)  # john follows david
     u2.follow(u2)  # susan follows herself
     u2.follow(u3)  # susan follows mary
     u3.follow(u3)  # mary follows herself
     u3.follow(u4)  # mary follows david
     u4.follow(u4)  # david follows himself
     db.session.add(u1)
     db.session.add(u2)
     db.session.add(u3)
     db.session.add(u4)
     db.session.commit()
     # check the followed posts of each user
     f1 = u1.followed_posts().all()
     f2 = u2.followed_posts().all()
     f3 = u3.followed_posts().all()
     f4 = u4.followed_posts().all()
     assert len(f1) == 3
     assert len(f2) == 2
     assert len(f3) == 2
     assert len(f4) == 1
     assert f1 == [p4, p2, p1]
     assert f2 == [p3, p2]
     assert f3 == [p4, p3]
     assert f4 == [p4]
Ejemplo n.º 2
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data,
                    author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('index.html',
                           title=_('Home'),
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Ejemplo n.º 3
0
    def test_follow_post(self):
        # Create four users:
        u1 = User(username='******', email='*****@*****.**')
        u2 = User(username='******', email='*****@*****.**')
        u3 = User(username='******', email='*****@*****.**')
        u4 = User(username='******', email='*****@*****.**')
        db.session.add_all([u1, u2, u3, u4])

        # Create four posts:
        now = datetime.utcnow()
        p1 = Post(body='post from test1',
                  author=u1,
                  timestamp=now + timedelta(seconds=1))
        p2 = Post(body='post from test2',
                  author=u2,
                  timestamp=now + timedelta(seconds=4))
        p3 = Post(body='post from test3',
                  author=u3,
                  timestamp=now + timedelta(seconds=3))
        p4 = Post(body='post from test4',
                  author=u4,
                  timestamp=now + timedelta(seconds=2))
        db.session.add_all([p1, p2, p3, p4])
        db.session.commit()

        # Setup the followers:
        u1.follow(u2)  # 'test1' follows 'test2'
        u1.follow(u4)  # 'test1' follows 'test4'
        u2.follow(u3)  # 'test2' follows 'test3'
        u3.follow(u4)  # 'test3' follows 'test4'
        db.session.commit()

        # Check the followed posts of each user:
        f1 = u1.followed_posts().all()
        f2 = u2.followed_posts().all()
        f3 = u3.followed_posts().all()
        f4 = u4.followed_posts().all()
        self.assertEqual(f1, [p2, p4, p1])
        self.assertEqual(f2, [p2, p3])
        self.assertEqual(f3, [p3, p4])
        self.assertEqual(f4, [p4])
Ejemplo n.º 4
0
    def test_follow_posts(self):
        # create four users
        u1 = User(username='******', email='*****@*****.**')
        u2 = User(username='******', email='*****@*****.**')
        u3 = User(username='******', email='*****@*****.**')
        u4 = User(username='******', email='*****@*****.**')
        db.session.add_all([u1, u2, u3, u4])

        # create four posts
        now = datetime.utcnow()
        p1 = Post(body="post from john",
                  author=u1,
                  timestamp=now + timedelta(seconds=1))
        p2 = Post(body="post from susan",
                  author=u2,
                  timestamp=now + timedelta(seconds=4))
        p3 = Post(body="post from mary",
                  author=u3,
                  timestamp=now + timedelta(seconds=3))
        p4 = Post(body="post from david",
                  author=u4,
                  timestamp=now + timedelta(seconds=2))
        db.session.add_all([p1, p2, p3, p4])
        db.session.commit()

        # setup the followers
        u1.follow(u2)  # john follows susan
        u1.follow(u4)  # john follows david
        u2.follow(u3)  # susan follows mary
        u3.follow(u4)  # mary follows david
        db.session.commit()

        # check the followed posts of each user
        f1 = u1.followed_posts().all()
        f2 = u2.followed_posts().all()
        f3 = u3.followed_posts().all()
        f4 = u4.followed_posts().all()
        self.assertEqual(f1, [p2, p4, p1])
        self.assertEqual(f2, [p2, p3])
        self.assertEqual(f3, [p3, p4])
        self.assertEqual(f4, [p4])
Ejemplo n.º 5
0
def home(page=1):
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, timestamp=datetime.utcnow(),
                    author=g.user)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'), "success")
        return redirect(url_for('.home'))

    posts = g.user.followed_posts().paginate(
        page,
        POST_PER_PAGE,
        False)

    return render_template("frontend/index.html",
                           title=_("Home"),
                           form=form,
                           posts=posts)
Ejemplo n.º 6
0
def create_post(db: Session, item: PostBase):
    post = Post(**item.dict())
    db.add(post)
    db.commit()
    db.refresh(post)
    return post