def test_delete_user_cascades(self): jessica = Profile.objects.get(user__username="******") jack = Profile.objects.get(user__username="******") # create likes (and match) between users Like.objects.create(profile=jessica, profile_liked=jack, is_liked=True) Like.objects.create(profile=jack, profile_liked=jessica, is_liked=True) Message.objects.create(sender=jessica, receiver=jack, message="hi I've just matched you") Message.objects.create(sender=jack, receiver=jessica, message="hi") Message.objects.create(sender=jessica, receiver=jack, message="hi") msgs = Message.objects.filter(Q(sender=jessica) | Q(receiver=jessica)) self.assertEquals(msgs.count(), 3) likes = Like.objects.filter( Q(profile=jessica) | Q(profile_liked=jessica)).count() matches = get_matches(jessica) self.assertEquals(len(matches), 1) # now delete user and check that likes, matches and messages cascade jack.delete() msgs = Message.objects.filter(Q(sender=jessica) | Q(receiver=jessica)) self.assertEquals(msgs.count(), 0) likes_after_delete = Like.objects.filter( Q(profile=jessica) | Q(profile_liked=jessica)).count() self.assertEquals(likes_after_delete, likes - 2) matches = get_matches(jessica) self.assertEquals(len(matches), 0)
def match_profile(request, username): context_dict = {} profile = request.user.profile try: user = User.objects.get(username=username) matches = utils.get_matches(profile) if user.profile not in matches: warning(request, 'You have not been matched with that user.') return redirect('glover:matches') context_dict['profile'] = user.profile except Profile.DoesNotExist: context_dict['profile'] = None return render(request, 'glover/match-profile.html', context=context_dict)
def test_get_matches(self): NUM_MATCHES_EXPECTED = 3 jessica = Profile.objects.get(user__username="******") profiles = Profile.objects.exclude( user__username="******")[:NUM_MATCHES_EXPECTED] for profile in profiles: Like.objects.create(profile=jessica, profile_liked=profile, is_liked=True) Like.objects.create(profile=profile, profile_liked=jessica, is_liked=True) matches = get_matches(jessica) self.assertEqual(len(matches), NUM_MATCHES_EXPECTED)
def matches(request): context_dict = {} qfilter = request.GET.get("filter", None) profile = request.user.profile if qfilter is None: matches = utils.get_matches(profile) elif qfilter == 'name-a': matches = utils.get_matches_by_name_ascending(profile) elif qfilter == 'name-d': matches = utils.get_matches_by_name_descending(profile) elif qfilter == 'messaged': matches = utils.user_chat_profiles(profile) elif qfilter == 'not-messaged': matches = utils.get_matches_not_messaged(profile) context_dict = {'profiles': matches} return render(request, 'glover/matches.html', context=context_dict)
def user_messages(request, username): profile = Profile.objects.get(user__username=username) matches = utils.get_matches(request.user.profile) if profile not in matches: warning(request, 'You cannot message this user.') return redirect('glover:messages') if request.method == "POST": msg = request.POST.get('message-content') Message.objects.create(sender=request.user.profile, receiver=profile, message=msg) context_dict = {} q1 = Q(sender=request.user.profile, receiver=profile) q2 = Q(sender=profile, receiver=request.user.profile) chat = Message.objects.filter(q1 | q2).order_by('time_sent') context_dict['chat'] = chat context_dict['profile'] = profile context_dict['users_messaged'] = utils.user_chat_profiles( request.user.profile) return render(request, 'glover/messages.html', context=context_dict)