remade PsychTestAddResultViewSet

This commit is contained in:
Денис Сарапулов 2023-05-17 11:27:38 +10:00
parent 763d2e0779
commit 49268f279f
4 changed files with 47 additions and 62 deletions

View File

@ -1,7 +1,6 @@
from django.contrib import admin
from .models import Apartament, Photo, User, PsychTestAnswers
from .models import Apartament, Photo, User
admin.site.register(Apartament)
admin.site.register(Photo)
admin.site.register(User)
admin.site.register(PsychTestAnswers)

View File

@ -1,6 +1,6 @@
from rest_framework import serializers
from .models import Apartament, User, PsychTestAnswers, AuthToken
from .models import Apartament, User, AuthToken
from .models import Apartament, User, AuthToken
@ -30,10 +30,10 @@ class PublicUserSerializer(serializers.ModelSerializer):
model = User
exclude = ('favorites_apartments', 'comparison_apartments')
class PsychTestReultsSerializer(serializers.ModelSerializer):
class Meta:
model = PsychTestAnswers
fields = "__all__"
# class PsychTestReultsSerializer(serializers.ModelSerializer):
# class Meta:
# model = PsychTestAnswers
# fields = "__all__"
class UserSerializer(serializers.ModelSerializer):
class Meta:

View File

@ -12,9 +12,9 @@ urlpatterns = format_suffix_patterns([
path("apartaments/filters/", views.ApartmentFilter.as_view({'post': 'list'})),
# user
path("user/get_compatible", views.CompatibleUsersView.as_view({'post': 'list'})),
# path("user/get_compatible", views.CompatibleUsersView.as_view({'post': 'list'})),
# пример: psych_test/add_result/1/?result=50 result - результат псих теста пользователя
path("psych_test/add_result/<int:pk>", views.PsychTestAddResultViewSet.as_view({'post': 'create'})),
path("psych_test/add_result/<int:pk>", views.PsychTestAddResultViewSet.as_view({'post': 'update'})),
path('user/get', views.UserGet.as_view()),
# auth

View File

@ -10,12 +10,11 @@ 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, PsychTestAnswers, AuthToken
from .models import Apartament, User, AuthToken
from .serializer import (ApartamentListSerializer,
ApartamentDetailSerializer,
PsychTestAddResultSerializer,
PublicUserSerializer,
PsychTestReultsSerializer,
UserSerializer,
TokenSerializer)
@ -100,72 +99,59 @@ class ApartmentFilter(viewsets.ViewSet):
class PsychTestAddResultViewSet(viewsets.ViewSet):
def get_object(self, pk):
return User.objects.get(pk=pk)
def create(self, request, pk):
def update(self, request, pk):
user = self.get_object(pk)
results = request.data
PsychTestAnswers.objects.create(
user=user,
first_question=results[0],
second_question=results[1],
third_question=results[2],
fourth_question=results[3],
fifth_question=results[4],
sixth_question=results[5],
seventh_question=results[6],
eighth_question=results[7],
nineth_question=results[8],
tenth_question=results[9],
eleventh_question=results[10],
twelfth_question=results[11]
)
user.update(test_results=results)
return Response({'successfully': 'results post'})
# TODO: remake
# class CompatibleUsersView(viewsets.ViewSet):
# def list(self, req: Request):
# user_data = dict(req.data);
class CompatibleUsersView(viewsets.ViewSet):
def list(self, req: Request):
user_data = dict(req.data);
# # TODO: Verify auth
# vvsu_login = user_data['openid'];
# TODO: Verify auth
vvsu_login = user_data['openid'];
# # Exclude already viewed users
# exclude = [];
# if ('exclude' in user_data.keys()):
# exclude = user_data['exclude'];
# 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:
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);
try:
this_user = User.objects.get(openid_addr=vvsu_login);
except User.DoesNotExist:
return Response({'error': 'user not found'}, 404);
# try:
# answers_this_user = PsychTestReultsSerializer(PsychTestAnswers.objects.get(user=this_user)).dict;
# except PsychTestAnswers.DoesNotExist:
# return Response({'error': 'answers not found'}, 404);
try:
answers_this_user = PsychTestReultsSerializer(PsychTestAnswers.objects.get(user=this_user)).dict;
except PsychTestAnswers.DoesNotExist:
return Response({'error': 'answers not found'}, 404);
# users_answers_query = PsychTestReultsSerializer(PsychTestAnswers.objects.all(), many=True).dict
users_answers_query = PsychTestReultsSerializer(PsychTestAnswers.objects.all(), many=True).dict
# users = [];
users = [];
# for user_answers in users_answers_query:
# score = 0
# 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)
for user_answers in users_answers_query:
score = 0
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)
# for user in users_query:
# if (abs(user.psych_test_result - score) < 20):
# users.append(PublicUserSerializer(user).data);
# # for user in users_query:
# # if (abs(user.psych_test_result - score) < 20):
# # users.append(PublicUserSerializer(user).data);
return Response(users);
# return Response(users);
class UserLogin(APIView):