示例#1
0
def friend_request(request):
    if request.method == "POST":
        received_data = json.loads(request.body)
        try:
            from_profile = Profile.objects.get(uuid=received_data['author']['id'])
        except:
            from_user = User(username=received_data['author']['displayname']+"@"+received_data['author']['host'],
                            password="")
            from_user.save()
            from_profile = Profile.objects.create(user=from_user,uuid=received_data['author']['id'],
            displayname=received_data['author']['displayname'])

        try:
            to_profile = Profile.objects.get(uuid=received_data['friend']['id'])
        except:
            return HttpResponse(status=404)

        try:
            checkFollow = Follow.objects.filter( Q(from_profile_id=from_profile) & Q(to_profile_id=to_profile) ).first()
            if not checkFollow:
                newFollow = Follow(from_profile_id=from_profile, to_profile_id=to_profile, status='PENDING')
                newFollow.save()
        except:
            return HttpResponse(status=500)

        return JsonResponse('Success!', safe=False)
    return HttpResponse(status=405)


    """
示例#2
0
def friend_request(request):
    if request.method == 'POST':
        to_profile_id = request.POST.get('to_profile','')
        current_profile = Profile.objects.get(user_id=request.user.id)
        to_profile = Profile.objects.get(id=to_profile_id)

        host_port = to_profile.host.strip("http://").split(":")
        print(host_port)
        port = host_port[1]

        if str(port) != "8000" and str(port) != "41024":
            print("Not Local")
            #host = Host.objects.get(name="Our own")
            host = Host.objects.filter( Q(host_url__icontains=port) ).first()
            host.post_friend_request([str(current_profile.uuid), str(to_profile.uuid)])
            #print(host)

        checkFollow = Follow.objects.filter( Q(from_profile_id=current_profile) & Q(to_profile_id=to_profile) ).first()
        if checkFollow:
            checkFollow.status ="PENDING"
            checkFollow.save()
        else:
            newFollow = Follow(from_profile_id=current_profile, to_profile_id=to_profile, status='PENDING')
            newFollow.save()

    return redirect('/')
示例#3
0
def follow_author(request):
    current_profile = Profile.objects.get(user_id=request.user.id)

    if request.method == 'POST':
        follow_profile_id = request.POST.get("follow_profile_id"," ")
        follow_profile = Profile.objects.get(id=follow_profile_id)

        newFollow = Follow(from_profile_id=current_profile, to_profile_id=follow_profile, status='FOLLOWING')
        newFollow.save()

    return redirect('/')
示例#4
0
    def test_follow(self):
        profile1 = Profile.objects.get(displayname="user1")
        profile2 = Profile.objects.get(displayname="user2")
        profile3 = Profile.objects.get(displayname="user3")

        testFollow1 = Follow(from_profile_id=profile1, to_profile_id=profile2, status='PENDING')
        testFollow1.save()
        testFollow2 = Follow(from_profile_id=profile2, to_profile_id=profile3, status='FOLLOWING')
        testFollow2.save()

        #See if the two Follow objects are created
        self.assertTrue(Follow.objects.all(),2)

        #A follows B but does not mean B follows A
        self.assertTrue(Follow.objects.get(from_profile_id=profile1, to_profile_id=profile2))

        self.assertFalse(Follow.objects.filter(from_profile_id=profile2, to_profile_id=profile1))