def _render(self, kind, is_mine = False): template_prefix = 'partial/' if self.is_ajax_request else ( 'mine_' if is_mine else 'user_') offset = (int(self.get_argument('page', 1)) - 1) * USERS_PER_PAGE user = self.user if not is_mine else self.current_user if kind == 'following': page_path = '/user/{0}/following'.format(user.username) if not is_mine else '/mine/following' users_title = u'{0}关注的人'.format(user.fullname if not is_mine else u'我') user_ids = models.FollowShip().findall_by_follower_user_id( user.id, limit = USERS_PER_PAGE, offset = offset).get_field('followed_user_id') users = [] for user_id in user_ids: users.append(models.User().find(user_id)) total_items = models.FollowShip().count_by_follower_user_id(user.id) if kind == 'follower': page_path = '/user/{0}/follower'.format(user.username) if not is_mine else '/mine/follower' users_title = u'关注{0}的人'.format(user.fullname if not is_mine else u'我') user_ids = models.FollowShip().findall_by_followed_user_id( user.id, limit = USERS_PER_PAGE, offset = offset).get_field('follower_user_id') users = [] for user_id in user_ids: users.append(models.User().find(user_id)) total_items = models.FollowShip().count_by_followed_user_id(user.id) return self.render('{0}users.html'.format(template_prefix), users_title = users_title, users = users, total_items = total_items, page_path = page_path, )
def post(self, username): if (not self.current_user.is_following(self.user.id) and self.current_user.following_count < MAX_FOLLOW_NUM): models.FollowShip().follow( self.current_user.id, self.user.id) else: models.FollowShip().unfollow( self.current_user.id, self.user.id) return self.send_success_json()
def follower_count(self): return models.FollowShip().count_by_followed_user_id(self.id)
def is_following(self, dest_user_id): return models.FollowShip( ).count_by_follower_user_id_and_followed_user_id( self.id, dest_user_id)