def update_friends(user_id, friends): u""" user_id のフレンドを、 friends(intの配列) のユーザで更新する。 """ new_friends = friends[:] new_friends.sort() user = UserProfile.objects.get_or_create(user_id = user_id)[0] db_friends = user.friends_now.order_by('friend') def removed(o): friend_rem = user.friends.create(friend = o.friend, unfollow = True) o.end_date = friend_rem.start_date o.save() def followed(uid): user.friends.create( friend = UserProfile.objects.get_or_create(user_id = uid)[0]) def refollowed(o, uid): if not o.unfollow : return #refollow friend_rem = user.friends.create(friend = o.friend) o.end_date = friend_rem.start_date o.save() m = Marge(db_friends, new_friends, comp_func = lambda l, r: cmp(l.friend.pk, r)) m.full(match = refollowed, left = removed, right = followed)
def users_related(user_id, contains_unfollow = False): u""" user_id の自分自身・フレンド・フォロワーの UserProfileを返す。 contains_unfollowがTrueの時、 過去にフレンド/フォロワーだったものも対象にする。 """ user = UserProfile.objects.get_or_create(user_id = user_id)[0] friend_friends = user.friends_now.order_by('friend') friend_followers = user.followers_now.order_by('user') if not contains_unfollow: # unfolowを除外 friend_friends = friend_friends.exclude(unfollow = True) friend_followers = friend_followers.exclude(unfollow = True) friends = IteratorProxy(friend_friends, lambda o: o.friend) followers = IteratorProxy(friend_followers, lambda o: o.user) users = [user] m = Marge(friends, followers, comp_func = lambda l,r: cmp(l.user_id, r.user_id)) m.full(match = lambda l, r: users.append(l), left = lambda o: users.append(o), right = lambda o: users.append(o)) return users
def attach_friends(report): friends = report.friends user = report.user oldf = user.friends.filter(q_inner(report.start_date), unfollow = False).order_by('friend') newf = user.friends.filter(q_inner(report.end_date), unfollow = False).order_by('friend') m = Marge(oldf, newf, lambda a,b: cmp(a.friend_id, b.friend_id)) m.full(left = lambda l: friends.create(user = l.friend, remove = True), right = lambda r: friends.create(user = r.friend, remove = False)) return friends