def follow_location(request, location_id): member_login = get_member_login_object(request) location = Location.objects.get(pk=location_id) location.follow_by.add(member_login) friend_activity_content = get_stream_content( 'follow_location', 'friends_content', { 'member_login': member_login, 'location': location }) user_activity_content = get_stream_content( 'follow_location', 'user_content', { 'member_login': member_login, 'location': location }) list_friends_username = Friendship.objects.friends_of( member_login.user).values_list('username', flat=True) user_streams.add_stream_item(member_login.user, friend_activity_content, user_activity_content) buzz = simplejson.dumps({ 'new_friend_activity': friend_activity_content, 'list_friends_username': list(list_friends_username) }) conn.send(buzz, destination='/update') return simplejson.dumps({ 'username': member_login.user.username, 'name': member_login.user.first_name + " " + member_login.user.last_name, 'avatar': member_login.avatar.url })
def test_unicode_handled_properly(self): user = User.objects.create() message = u'☃' add_stream_item(user, message) items = get_stream_items(user) self.assertEqual(items[0].content, message)
def test_single_user(self): user = User.objects.create() content = 'Test message' add_stream_item(user, content) items = get_stream_items(user) self.assertEqual(len(items), 1) item = items[0] self.assertEqual(item.content, content)
def test_multiple_users(self): user_1 = User.objects.create(username='******') user_2 = User.objects.create(username='******') user_3 = User.objects.create(username='******') content = 'Broadcast message' add_stream_item(User.objects.all(), content) for user in user_1, user_2, user_3: self.assertEqual(get_stream_items(user)[0].content, content)
def test_identical_messages(self): """Check that identical messages are handled properly. Mostly an issue for the Redis backend (which uses sets to store messages)""" user = User.objects.create() message = 'Test message' add_stream_item(user, message) add_stream_item(user, message) items = get_stream_items(user) self.assertEqual(len(items), 2)
def accept_friend_request(request,username): member_add_request = Member.objects.get(user=User.objects.get(username=username)) member_receive_request = get_member_login_object(request) fr = FriendshipRequest.objects.filter(from_user=member_add_request.user, to_user=member_receive_request.user) print fr # In case member A click accept while the other member B already cancel the request # we handle by reload the page if len(fr) != 0: fr[0].accept() notify_type= 'accept_friend_request' content = ("<div class='notify-main-content'>" + "<a href='/" + member_receive_request.user.username + "' >" + member_receive_request.user.first_name + " " + member_receive_request.user.last_name + "</a>" + " accept your friend request" + "</div>") new_notify = Notify.objects.create(notify_type=notify_type,notify_to=member_add_request,notify_from=member_receive_request,status='new',date=datetime.datetime.now(),content=content) new_notify.save() n = Notify.objects.get(notify_type='add_friend',notify_to=member_receive_request,notify_from=member_add_request) new_content = ("<div class='notify-main-content'>" + "<a href='/" + member_add_request.user.username + "' >" + member_add_request.user.first_name + " " + member_add_request.user.last_name + "</a>" + " want to be friend with you" + "</div>" + "<div class='notify-accept-friend-request'>" + "You have accepted this request " + "</div>") n.content = new_content n.notify_type = 'add_friend_response_accept' n.save() new_buzz_type = len(Notify.objects.filter(notify_to=member_add_request,status='new')) new_mail_type = len(Message.objects.filter(member_receive=member_add_request,status='new')) # Add and update streams friend_activity_content1 = get_stream_content('befriend','friends_content',{'member1':member_receive_request,'member2':member_add_request}) user_activity_content1 = get_stream_content('befriend','user_content',{'member1':member_receive_request,'member2':member_add_request}) list_friends_username1 = Friendship.objects.friends_of(member_receive_request.user).values_list('username', flat=True) friend_activity_content2 = get_stream_content('befriend','friends_content',{'member1':member_add_request,'member2':member_receive_request}) user_activity_content2 = get_stream_content('befriend','user_content',{'member1':member_add_request,'member2':member_receive_request}) list_friends_username2 = Friendship.objects.friends_of(member_add_request.user).values_list('username', flat=True) user_exclude = '' common_friend_username = get_duplicate_object(list(list_friends_username1)+list(list_friends_username2)) if len(common_friend_username) > 0: user_exclude=str(common_friend_username) user_streams.add_stream_item(member_receive_request.user, friend_activity_content1, user_activity_content1) user_streams.add_stream_item(member_add_request.user, friend_activity_content2, user_activity_content2, user_exclude) buzz = simplejson.dumps({'new_notify': new_buzz_type+new_mail_type,'new_buzz_type':new_buzz_type,'new_mail_type':new_mail_type, 'username': member_add_request.user.username, 'new_friend_activity1': friend_activity_content1, 'new_friend_activity2': friend_activity_content2, 'list_friends_username1': list(list_friends_username1), 'list_friends_username2': list(list_friends_username2) }) conn.send(buzz,destination='/update') return simplejson.dumps({'username':username,'firstname': member_add_request.user.first_name,'lastname': member_add_request.user.last_name,'reload': "False"}) else: return simplejson.dumps({'reload': "True"})
def update_streams(request): if request.session['allow_upload_stream']: if len( Photo.objects.filter( photo_type=request.session['photo_type'], object_id=request.session['object_id'])) > int( request.session['num_photo']): member_login = get_member_login_object(request) friend_activity_content, user_activity_content = '', '' if request.session['photo_type'] == 'activity': activity = Activity.objects.get( pk=request.session['object_id']) friend_activity_content = get_stream_content( 'upload_activity_photo', 'friends_content', { 'member_login': member_login, 'activity': activity }) user_activity_content = get_stream_content( 'upload_activity_photo', 'user_content', { 'member_login': member_login, 'activity': activity }) else: location = Location.objects.get( pk=request.session['object_id']) friend_activity_content = get_stream_content( 'upload_location_photo', 'friends_content', { 'member_login': member_login, 'location': location }) user_activity_content = get_stream_content( 'upload_location_photo', 'user_content', { 'member_login': member_login, 'location': location }) list_friends_username = Friendship.objects.friends_of( member_login.user).values_list('username', flat=True) user_streams.add_stream_item(member_login.user, friend_activity_content, user_activity_content) buzz = simplejson.dumps({ 'new_friend_activity': friend_activity_content, 'list_friends_username': list(list_friends_username) }) conn.send(buzz, destination='/update') request.session['allow_upload_stream'] = False return simplejson.dumps({})
def add_comment(request, location_id, comment_content): member_login = get_member_login_object(request) location = Location.objects.get(pk=location_id) now = datetime.datetime.now() new_comment = Comment.objects.create(comment_type='location', object_id=location_id, content=comment_content, member=member_login, created_date=now) new_comment.save() result = {} result['comment_id'] = new_comment.pk result['member'] = { 'username': member_login.user.username, 'first_name': member_login.user.first_name, 'last_name': member_login.user.last_name, 'avatar': member_login.avatar.url } result['content'] = new_comment.content_html result['date'] = new_comment.created_elapse_time #if request.session['allow_comment_stream']: friend_activity_content = get_stream_content( 'add_location_comment', 'friends_content', { 'member_login': member_login, 'location': location, 'comment': new_comment, 'comment_content': set_fixed_string(new_comment.content, 30) }) user_activity_content = get_stream_content( 'add_location_comment', 'user_content', { 'member_login': member_login, 'location': location }) list_friends_username = Friendship.objects.friends_of( member_login.user).values_list('username', flat=True) user_streams.add_stream_item(member_login.user, friend_activity_content, user_activity_content) buzz = simplejson.dumps({ 'new_friend_activity': friend_activity_content, 'list_friends_username': list(list_friends_username) }) conn.send(buzz, destination='/update') #request.session['allow_comment_stream'] = False return simplejson.dumps(result)
def add_comment(request,activity_id,comment_content): member_login = get_member_login_object(request) activity = Activity.objects.get(pk=activity_id) now = datetime.datetime.now() new_comment = Comment.objects.create(comment_type='activity',object_id=activity_id,content=comment_content,member=member_login,created_date=now) new_comment.save() result = {} result['comment_id'] = new_comment.pk result['member'] = {'username': member_login.user.username,'first_name':member_login.user.first_name, 'last_name': member_login.user.last_name,'avatar': member_login.avatar.url} result['content'] = new_comment.content_html result['date'] = new_comment.created_elapse_time #if request.session['allow_comment_stream']: friend_activity_content = get_stream_content('add_activity_comment','friends_content',{'member_login':member_login,'activity':activity, 'comment': new_comment, 'comment_content':set_fixed_string(new_comment.content,30)}) user_activity_content = get_stream_content('add_activity_comment','user_content',{'member_login':member_login,'activity':activity}) list_friends_username = Friendship.objects.friends_of(member_login.user).values_list('username', flat=True) user_streams.add_stream_item(member_login.user, friend_activity_content, user_activity_content) buzz = simplejson.dumps({'new_friend_activity': friend_activity_content, 'list_friends_username': list(list_friends_username) }) conn.send(buzz,destination='/update') #request.session['allow_comment_stream'] = False return simplejson.dumps(result)
def test_pagination(self): user = User.objects.create() now = datetime_now() for count in range(100): created_at = now + timedelta(minutes=count) add_stream_item(user, 'Message %s' % count, created_at=created_at) paginator = Paginator(get_stream_items(user), 10) self.assertEqual(paginator.num_pages, 10) page_1 = paginator.page(1) objects = page_1.object_list self.assertEqual(len(objects), 10) self.assertEqual(objects[0].content, 'Message 99') self.assertEqual(objects[9].content, 'Message 90') self.assertEqual(page_1.next_page_number(), 2) page_10 = paginator.page(10) objects = page_10.object_list self.assertEqual(len(objects), 10) self.assertEqual(objects[0].content, 'Message 9') self.assertEqual(objects[9].content, 'Message 0') self.assertFalse(page_10.has_next())
def test_slicing(self): user = User.objects.create() now = datetime_now() for count in range(10): created_at = now + timedelta(minutes=count) add_stream_item(user, 'Message %s' % count, created_at=created_at) stream_items = get_stream_items(user) first_five = stream_items[:5] self.assertEqual(len(first_five), 5) self.assertEqual(first_five[0].content, 'Message 9') self.assertEqual(first_five[4].content, 'Message 5') middle = stream_items[3:7] self.assertEqual(len(middle), 4) self.assertEqual(middle[0].content, 'Message 6') self.assertEqual(middle[3].content, 'Message 3') end = stream_items[6:] self.assertEqual(len(end), 4) self.assertEqual(end[0].content, 'Message 3') self.assertEqual(end[3].content, 'Message 0')
def test_message_ordering(self): user = User.objects.create() now = datetime_now() add_stream_item(user, 'Message 1', created_at=now) add_stream_item(user, 'Message 2', created_at=now + timedelta(minutes=1)) add_stream_item(user, 'Message 3', created_at=now + timedelta(minutes=2)) stream_items = get_stream_items(user) self.assertEqual(stream_items[0].content, 'Message 3') self.assertEqual(stream_items[1].content, 'Message 2') self.assertEqual(stream_items[2].content, 'Message 1')
def notify_users(users, template, context): users = users if isinstance(users, (list, tuple)) else [users] content = render_to_string(template, context) user_streams.add_stream_item(users, content)
def stream_user(id,string): #Stream to all users who follows. user = User.objects.get(id = id) import user_streams user_streams.add_stream_item(user,string) return True
def save_kudos_signal(sender, **kwargs): kudos = kwargs['instance'] user_streams.add_stream_item(kudos.to_user, '%s has awarded a +%d Kudos to %s'%(kudos.from_user.get_full_name(), kudos.rating, kudos.to_user.get_full_name()), kudos)