def test_favorite_follows(self): u1 = User.create(email='*****@*****.**', password='******') u2 = User.create(email='*****@*****.**', password='******') favor = Favorite.create(title='美术', user=u2) db.session.add_all([u1, u2, favor]) db.session.commit() self.assertFalse(u1.is_following_favorite(favor)) self.assertFalse(favor.is_followed_by(u1)) timestamp_before = datetime.datetime.utcnow() u1.follow(favor) db.session.add(u1) db.session.commit() timestamp_after = datetime.datetime.utcnow() self.assertTrue(u1.is_following_favorite(favor)) self.assertTrue(favor.is_followed_by(u1)) self.assertTrue(u1.followed_favorites.count() == 1) self.assertTrue(favor.followers.count() == 1) f = u1.followed_favorites.all()[0] self.assertTrue(f.followed == favor) self.assertTrue(timestamp_before <= f.timestamp <= timestamp_after) u1.unfollow(favor) db.session.add(u1) db.session.commit() self.assertFalse(u1.is_following_favorite(favor)) self.assertFalse(favor.is_followed_by(u1)) self.assertTrue(u1.followed_favorites.count() == 0) self.assertTrue(favor.followers.count() == 0) self.assertTrue(FollowFavorite.query.count() == 0)
def create_favorite(username): s = search() if s: return s user = User.query.filter_by(username=username).first() if current_user != user and not current_user.can(Permission.ADMINISTER): flash('没有权限') return redirect(url_for('.user', username=user.username)) form = FavoriteForm() if form.validate_on_submit(): Favorite.create(title=form.title.data, description=form.description.data, public=form.public.data, user=current_user._get_current_object()) flash('新建了{}收藏夹'.format(form.title.data), category='info') return redirect(url_for('.user', username=user.username)) return render_template('favorite/create_favorite.html', form=form) # 渲染表单
def post(self, post_id): username = self.current_user f = yield gen.maybe_future(Favorite.get_by_user_post(username, post_id)) if f: raise exceptions.PostAlreadyFavorited() else: yield gen.maybe_future(Favorite.create(username, post_id)) count = yield gen.maybe_future(Favorite.count_by_post(post_id)) # Update gold. update_gold.apply_async(("post_be_favorite", post_id)) raise gen.Return({"count": count})
def test_collect_question(self): q = Question.create(title='wdffd') f = Favorite.create(title='数学') self.assertListEqual(f.questions.all(), []) self.assertTrue(f.questions.count() == 0) self.assertTrue(q.favorites.count() == 0) f.collect(q) self.assertListEqual([i.question for i in f.questions.all()], [q]) self.assertTrue(f.questions.count() == 1) self.assertTrue(q.favorites.count() == 1) self.assertTrue(f.has_collect_question(q)) f.uncollect(q) self.assertTrue(f.questions.count() == 0) self.assertTrue(q.favorites.count() == 0) self.assertFalse(f.has_collect_question(q))
def test_collect_post(self): p = Post.create(title='haha') f = Favorite.create(title='数学') self.assertListEqual(f.posts.all(), []) self.assertTrue(f.posts.count() == 0) self.assertTrue(p.favorites.count() == 0) f.collect(p) self.assertListEqual([i.post for i in f.posts.all()], [p]) self.assertTrue(f.posts.count() == 1) self.assertTrue(p.favorites.count() == 1) self.assertTrue(f.has_collect_post(p)) f.uncollect(p) self.assertTrue(f.posts.count() == 0) self.assertTrue(p.favorites.count() == 0) self.assertFalse(f.has_collect_post(p))
def test_collect_answer(self): q = Question.create(title='dwwf') a = Answer.create(body='dwwdf', question=q) f = Favorite.create(title='数学') self.assertListEqual(f.answers.all(), []) self.assertTrue(f.answers.count() == 0) self.assertTrue(a.favorites.count() == 0) f.collect(a) self.assertListEqual([i.answer for i in f.answers.all()], [a]) self.assertTrue(f.answers.count() == 1) self.assertTrue(a.favorites.count() == 1) self.assertTrue(f.has_collect_answer(a)) f.uncollect(a) self.assertTrue(f.answers.count() == 0) self.assertTrue(a.favorites.count() == 0) self.assertFalse(f.has_collect_answer(a))