本文整理匯總了Python中app.users.models.User.follow方法的典型用法代碼示例。如果您正苦於以下問題:Python User.follow方法的具體用法?Python User.follow怎麽用?Python User.follow使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類app.users.models.User
的用法示例。
在下文中一共展示了User.follow方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_follow
# 需要導入模塊: from app.users.models import User [as 別名]
# 或者: from app.users.models.User import follow [as 別名]
def test_follow(self):
user1 = User(name='mark', email='[email protected]')
user2 = User(name='rudy', email='[email protected]')
db.session.add(user1)
db.session.add(user2)
db.session.commit()
self.assertEqual(user1.is_following(user2), False)
self.assertEqual(user1.unfollow(user2), None)
user1 = user1.follow(user2)
db.session.add(user1)
db.session.commit()
# user1 has already followed user2
self.assertEqual(user1.follow(user2), None)
self.assertEqual(user1.followed.count(), 1)
self.assertEqual(user1.followed.first().name, 'rudy')
self.assertEqual(user2.followers.count(), 1)
self.assertEqual(user2.followers.first().name, 'mark')
user1 = user1.unfollow(user2)
db.session.add(user1)
db.session.commit()
self.assertEqual(user1.unfollow(user2), None)
self.assertEqual(user1.is_following(user2), False)
self.assertEqual(user1.followed.count(), 0)
self.assertEqual(user2.followers.count(), 0)
示例2: test_followed_posts
# 需要導入模塊: from app.users.models import User [as 別名]
# 或者: from app.users.models.User import follow [as 別名]
def test_followed_posts(self):
# create 4 users
user1 = User(name='mark', email='[email protected]')
user2 = User(name='rudy', email='[email protected]')
user3 = User(name='jack', email='[email protected]')
user4 = User(name='william', email='[email protected]')
db.session.add(user1)
db.session.add(user2)
db.session.add(user3)
db.session.add(user4)
# create 4 posts
utcnow = datetime.utcnow()
post1 = Post(body='post from mark', author=user1, timestamp=utcnow + timedelta(seconds=1))
post2 = Post(body='post from rudy', author=user2, timestamp=utcnow + timedelta(seconds=2))
post3 = Post(body='post from jack', author=user3, timestamp=utcnow + timedelta(seconds=3))
post4 = Post(body='post from william', author=user4, timestamp=utcnow + timedelta(seconds=4))
db.session.add(post1)
db.session.add(post2)
db.session.add(post3)
db.session.add(post4)
db.session.commit()
# setup the followers
user1 = user1.follow(user2)
user2 = user2.follow(user3)
user2 = user2.follow(user4)
user3 = user3.follow(user1)
user3 = user3.follow(user3)
user4 = user4.follow(user1)
user4 = user4.follow(user3)
user4 = user4.follow(user4)
db.session.add(user1)
db.session.add(user2)
db.session.add(user3)
db.session.add(user4)
db.session.commit()
f1 = user1.followed_posts()
f2 = user2.followed_posts()
f3 = user3.followed_posts()
f4 = user4.followed_posts()
self.assertEqual(f1.count(), 1)
self.assertEqual(f2.count(), 2)
self.assertEqual(f3.count(), 2)
self.assertEqual(f4.count(), 3)
self.assertEqual(f1.all(), [post2])
self.assertEqual(f2.all(), [post4, post3])
self.assertEqual(f3.all(), [post3, post1])
self.assertEqual(f4.all(), [post4, post3, post1])
示例3: after_login
# 需要導入模塊: from app.users.models import User [as 別名]
# 或者: from app.users.models.User import follow [as 別名]
def after_login(resp):
if resp.email is None or resp.email == "":
flash(gettext('Invalid login. Please try again.'))
return redirect(url_for('login_view'))
user = User.query.filter_by(email = resp.email).first()
userdetails = UserDetails(user=user)
if user is None:
nickname = resp.nickname
if nickname is None or nickname == "":
nickname = resp.email.split('@')[0]
nickname = User.make_valid_nickname(nickname)
nickname = User.make_unique_nickname(nickname)
user = User(nickname = nickname, email = resp.email, role = ROLE_USER)
db.session.add(user)
db.session.add(userdetails)
db.session.commit()
# make the user follow him/herself
db.session.add(user.follow(user))
db.session.commit()
userdetails = UserDetails.query.filter_by(user=user).first()
if userdetails is None:
userdetails = UserDetails(user=user)
db.session.add(userdetails)
db.session.commit()
remember_me = False
if 'remember_me' in session:
remember_me = session['remember_me']
session.pop('remember_me', None)
login_user(user, remember = True)
return redirect(request.args.get('next') or url_for('main_flow.index'))