add compatible users view

This commit is contained in:
b1ek 2023-05-15 14:54:30 +10:00
parent d98e9f7c75
commit 5cd15a5615
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 46 additions and 6 deletions

View File

@ -23,7 +23,7 @@ class PsychTestAddResultSerializer(serializers.ModelSerializer):
model = User
fields = ("psych_test_result",)
class UserSerializer(serializers.ModelSerializer):
class PublicUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__"
exclude = ('favorites_apartments', 'comparison_apartments')

View File

@ -4,14 +4,18 @@ from rest_framework.views import APIView, View
from rest_framework.request import Request
from django.http import HttpResponseBadRequest
from django.db.models.query import QuerySet
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from .models import Apartament, User
from .serializer import (ApartamentListSerializer,
ApartamentDetailSerializer,
PsychTestAddResultSerializer,
UserSerializer)
PublicUserSerializer)
import json
import json, math, random, re
class ApartamentViewSet(viewsets.ReadOnlyModelViewSet):
"""Вывод списка квартир или отдельной квартиры"""
@ -96,5 +100,41 @@ class PsychTestAddResultViewSet(viewsets.ViewSet):
class CompatibleUsersView(viewsets.ViewSet):
def list(self, req: Request):
user_data = dict(req.data);
users = User.objects.all();
return Response(UserSerializer(users).data);
# TODO: Verify auth
vvsu_login = user_data['openid'];
# Exclude already viewed users
exclude = [];
if ('exclude' in user_data.keys()):
exclude = user_data['exclude'];
try:
validate_email(vvsu_login);
except ValidationError:
return Request({'error': 'bad login'}, 400);
try:
this_user = User.objects.get(openid_addr=vvsu_login);
except User.DoesNotExist:
return Response({'error': 'user not found'}, 404);
score = this_user.psych_test_result;
users_query = User.objects.all();
users = [];
for user in users_query:
if (abs(user.psych_test_result - score) < 20):
users.append(PublicUserSerializer(user).data);
random.shuffle(users);
users = users[:7];
for i in range(3):
users.append(PublicUserSerializer(random.choice(users_query)).data);
random.shuffle(users);
return Response(users);