From 49268f279ff3cd3a19b6a8340ac7b16048ac4651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81=20=D0=A1=D0=B0=D1=80=D0=B0?= =?UTF-8?q?=D0=BF=D1=83=D0=BB=D0=BE=D0=B2?= Date: Wed, 17 May 2023 11:27:38 +1000 Subject: [PATCH] remade PsychTestAddResultViewSet --- pairent_backend/pairent_app/admin.py | 3 +- pairent_backend/pairent_app/serializer.py | 10 +-- pairent_backend/pairent_app/urls.py | 4 +- pairent_backend/pairent_app/views.py | 92 ++++++++++------------- 4 files changed, 47 insertions(+), 62 deletions(-) diff --git a/pairent_backend/pairent_app/admin.py b/pairent_backend/pairent_app/admin.py index c1f723c..8e4556b 100644 --- a/pairent_backend/pairent_app/admin.py +++ b/pairent_backend/pairent_app/admin.py @@ -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) diff --git a/pairent_backend/pairent_app/serializer.py b/pairent_backend/pairent_app/serializer.py index fe05f1e..85e20e1 100644 --- a/pairent_backend/pairent_app/serializer.py +++ b/pairent_backend/pairent_app/serializer.py @@ -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: diff --git a/pairent_backend/pairent_app/urls.py b/pairent_backend/pairent_app/urls.py index a834842..824512f 100644 --- a/pairent_backend/pairent_app/urls.py +++ b/pairent_backend/pairent_app/urls.py @@ -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/", views.PsychTestAddResultViewSet.as_view({'post': 'create'})), + path("psych_test/add_result/", views.PsychTestAddResultViewSet.as_view({'post': 'update'})), path('user/get', views.UserGet.as_view()), # auth diff --git a/pairent_backend/pairent_app/views.py b/pairent_backend/pairent_app/views.py index 348a55f..2269e11 100644 --- a/pairent_backend/pairent_app/views.py +++ b/pairent_backend/pairent_app/views.py @@ -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'}) - -class CompatibleUsersView(viewsets.ViewSet): - def list(self, req: Request): - user_data = dict(req.data); +# TODO: remake +# 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):