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 django.contrib import admin
from .models import Apartament, Photo, User, PsychTestAnswers from .models import Apartament, Photo, User
admin.site.register(Apartament) admin.site.register(Apartament)
admin.site.register(Photo) admin.site.register(Photo)
admin.site.register(User) admin.site.register(User)
admin.site.register(PsychTestAnswers)

View File

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

View File

@ -12,9 +12,9 @@ urlpatterns = format_suffix_patterns([
path("apartaments/filters/", views.ApartmentFilter.as_view({'post': 'list'})), path("apartaments/filters/", views.ApartmentFilter.as_view({'post': 'list'})),
# user # 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 - результат псих теста пользователя # пример: 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()), path('user/get', views.UserGet.as_view()),
# auth # auth

View File

@ -10,12 +10,11 @@ from django.db.models.query import QuerySet
from django.core.validators import validate_email from django.core.validators import validate_email
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from .models import Apartament, User, PsychTestAnswers, AuthToken from .models import Apartament, User, AuthToken
from .serializer import (ApartamentListSerializer, from .serializer import (ApartamentListSerializer,
ApartamentDetailSerializer, ApartamentDetailSerializer,
PsychTestAddResultSerializer, PsychTestAddResultSerializer,
PublicUserSerializer, PublicUserSerializer,
PsychTestReultsSerializer,
UserSerializer, UserSerializer,
TokenSerializer) TokenSerializer)
@ -100,72 +99,59 @@ class ApartmentFilter(viewsets.ViewSet):
class PsychTestAddResultViewSet(viewsets.ViewSet): class PsychTestAddResultViewSet(viewsets.ViewSet):
def get_object(self, pk): def get_object(self, pk):
return User.objects.get(pk=pk) return User.objects.get(pk=pk)
def create(self, request, pk): def update(self, request, pk):
user = self.get_object(pk) user = self.get_object(pk)
results = request.data results = request.data
PsychTestAnswers.objects.create( user.update(test_results=results)
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]
)
return Response({'successfully': 'results post'}) 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): # # TODO: Verify auth
def list(self, req: Request): # vvsu_login = user_data['openid'];
user_data = dict(req.data);
# TODO: Verify auth # # Exclude already viewed users
vvsu_login = user_data['openid']; # exclude = [];
# if ('exclude' in user_data.keys()):
# exclude = user_data['exclude'];
# Exclude already viewed users # try:
exclude = []; # validate_email(vvsu_login);
if ('exclude' in user_data.keys()): # except ValidationError:
exclude = user_data['exclude']; # return Request({'error': 'bad login'}, 400);
try: # try:
validate_email(vvsu_login); # this_user = User.objects.get(openid_addr=vvsu_login);
except ValidationError: # except User.DoesNotExist:
return Request({'error': 'bad login'}, 400); # return Response({'error': 'user not found'}, 404);
try: # try:
this_user = User.objects.get(openid_addr=vvsu_login); # answers_this_user = PsychTestReultsSerializer(PsychTestAnswers.objects.get(user=this_user)).dict;
except User.DoesNotExist: # except PsychTestAnswers.DoesNotExist:
return Response({'error': 'user not found'}, 404); # return Response({'error': 'answers not found'}, 404);
try: # users_answers_query = PsychTestReultsSerializer(PsychTestAnswers.objects.all(), many=True).dict
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 = [];
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: # # for user in users_query:
score = 0 # # if (abs(user.psych_test_result - score) < 20):
for i in range(1, 12): # # users.append(PublicUserSerializer(user).data);
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);
return Response(users); # return Response(users);
class UserLogin(APIView): class UserLogin(APIView):