remade CompatibleUsersView

This commit is contained in:
Денис Сарапулов 2023-05-17 03:00:45 +10:00
parent ecc8762c56
commit 8bcd920e52
2 changed files with 63 additions and 15 deletions

View File

@ -1,6 +1,6 @@
from rest_framework import serializers from rest_framework import serializers
from .models import Apartament, User from .models import Apartament, User, PsychTestAnswers
class ApartamentListSerializer(serializers.ModelSerializer): class ApartamentListSerializer(serializers.ModelSerializer):
@ -26,4 +26,14 @@ class PsychTestAddResultSerializer(serializers.ModelSerializer):
class PublicUserSerializer(serializers.ModelSerializer): class PublicUserSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = User model = User
exclude = ('favorites_apartments', 'comparison_apartments') exclude = ('favorites_apartments', 'comparison_apartments')
class PsychTestReultsSerializer(serializers.ModelSerializer):
class Meta:
model = PsychTestAnswers
fields = "__all__"
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__"

View File

@ -14,7 +14,9 @@ from .models import Apartament, User, PsychTestAnswers
from .serializer import (ApartamentListSerializer, from .serializer import (ApartamentListSerializer,
ApartamentDetailSerializer, ApartamentDetailSerializer,
PsychTestAddResultSerializer, PsychTestAddResultSerializer,
PublicUserSerializer) PublicUserSerializer,
PsychTestReultsSerializer,
UserSerializer)
import json, math, random, re, requests, oidc_client, base64, hashlib import json, math, random, re, requests, oidc_client, base64, hashlib
@ -137,23 +139,27 @@ class CompatibleUsersView(viewsets.ViewSet):
except User.DoesNotExist: except User.DoesNotExist:
return Response({'error': 'user not found'}, 404); return Response({'error': 'user not found'}, 404);
score = this_user.psych_test_result; try:
answers_this_user = PsychTestReultsSerializer(PsychTestAnswers.objects.get(user=this_user)).dict;
except PsychTestAnswers.DoesNotExist:
return Response({'error': 'answers not found'}, 404);
users_query = User.objects.all(); users_answers_query = PsychTestReultsSerializer(PsychTestAnswers.objects.all(), many=True).dict
users = []; users = [];
for user in users_query: for user_answers in users_answers_query:
if (abs(user.psych_test_result - score) < 20): score = 0
users.append(PublicUserSerializer(user).data); for i in range(1, 12):
if answers_this_user[i] == user_answers[i]:
score += 1
if score / 12 * 100 > 30:
users.append(UserSerializer(User.objects.get(pk=user_answers[0])).data)
random.shuffle(users); # for user in users_query:
users = users[:7]; # if (abs(user.psych_test_result - score) < 20):
# users.append(PublicUserSerializer(user).data);
for i in range(3):
users.append(PublicUserSerializer(random.choice(users_query)).data);
random.shuffle(users);
return Response(users); return Response(users);
@ -286,4 +292,36 @@ class UserGet(APIView):
res.status_code = 404; res.status_code = 404;
return res; return res;
return JsonResponse(PublicUserSerializer(user).data); return JsonResponse(PublicUserSerializer(user).data);
# for user_answer in users_answers_query:
# score = 0
# if user_answer.first_question == scores_this_user.first_question:
# score += 1
# if user_answer.second_question == scores_this_user.second_question:
# score += 1
# if user_answer.third_question == scores_this_user.third_question:
# score += 1
# if user_answer.fourth_question == scores_this_user.fourth_question:
# score += 1
# if user_answer.fifth_question == scores_this_user.fifth_question:
# score += 1
# if user_answer.sixth_question == scores_this_user.sixth_question:
# score += 1
# if user_answer.seventh_question == scores_this_user.seventh_question:
# score += 1
# if user_answer.eighth_question == scores_this_user.eighth_question:
# score += 1
# if user_answer.nineth_question == scores_this_user.nineth_question:
# score += 1
# if user_answer.tenth_question == scores_this_user.tenth_question:
# score += 1
# if user_answer.eleventh_question == scores_this_user.eleventh_question:
# score += 1
# if user_answer.twelfth_question == scores_this_user.twelfth_question:
# score += 1
# if