Beispiel #1
0
    def post(self, request, format=None):
        serializer = UserAccountSerializer(data=request.data)

        if serializer.is_valid():
            data = serializer.validated_data["user"]

            username = data.get("username")
            email = data.get("email")
            password = data.get("password")
            confirm_password = data.get("confirm_password")
            name = data.get("first_name")
            # client should post facebook_id to enable this
            # facebook_id = data.get('facebook_id')
            if password != confirm_password:
                return Response(
                    {"message": "Password and confirm password don't match"}, status=status.HTTP_400_BAD_REQUEST
                )

            UserAccount.objects.create_user(username, name, email, password, **serializer.validated_data)

            return Response(serializer.validated_data, status=status.HTTP_201_CREATED)

        return Response(
            {"status": "Bad request", "message": "Account could not be created with received data."},
            status=status.HTTP_400_BAD_REQUEST,
        )
Beispiel #2
0
    def post(self, request, format=None):
        serializer = UserAccountSerializer(data=request.data)

        if serializer.is_valid():
            data = serializer.validated_data['user']

            username = data.get('username')
            email = data.get('email')
            password = data.get('password')
            confirm_password = data.get('confirm_password')
            name = data.get('first_name')
            # client should post facebook_id to enable this
            # facebook_id = data.get('facebook_id')
            if password != confirm_password:
                return Response(
                    {'message': 'Password and confirm password don\'t match'},
                    status=status.HTTP_400_BAD_REQUEST)

            UserAccount.objects.create_user(username, name, email, password,
                                            **serializer.validated_data)

            return Response(serializer.validated_data,
                            status=status.HTTP_201_CREATED)

        return Response(
            {
                'status': 'Bad request',
                'message': 'Account could not be created with received data.'
            },
            status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
 def get(self, request, username, format=None):
     try:
         user = self.get_object(username)
         serializer = UserAccountSerializer(user)
         return Response(serializer.data)
     except:
         return Response(status=status.HTTP_404_NOT_FOUND)
Beispiel #4
0
class ExamRecordSerializer(serializers.ModelSerializer):
    user = UserAccountSerializer()

    class Meta:
        model = ExamRecord
        fields = ('user', 'score', 'done_time')
        read_only_fields = ('user', 'score')
Beispiel #5
0
class ExamRecordOneUserSerializer(serializers.ModelSerializer):
    user = UserAccountSerializer()
    exercise_records = ExerciseRecordNoUserSerializer(many=True)

    class Meta:
        model = ExamRecord
        fields = ('user', 'exercise_records', 'score', 'done_time')
        read_only_fields = ('user', 'score', 'done_time')
Beispiel #6
0
class ExerciseRecordSerializer(serializers.ModelSerializer):
    exercise = ExerciseSerializer()
    user = UserAccountSerializer()

    class Meta:
        model = ExerciseRecord
        fields = ('exercise', 'user', 'score')
        read_only_fields = ('exercise', 'user')
Beispiel #7
0
    def put(self, request, username, format=None):
        serializer = UserAccountSerializer(data=request.data)

        if serializer.is_valid() is False:
            return Response(
                {
                    'message':
                    'Cannnot update profile with provided information'
                },
                status=status.HTTP_400_BAD_REQUEST)

        data = serializer.validated_data

        password = data['user'].get('password')
        confirm_password = data['user'].get('confirm_password')

        if password != confirm_password:
            return Response(
                {'message': 'Password and confirm password don\'t match'},
                status=status.HTTP_400_BAD_REQUEST)

        user = User.objects.get(username=username)
        user_serializer = UserSerializer(user, data=data['user'])

        if user_serializer.is_valid():
            user_serializer.save()

            account = self.get_object(username)
            account.school = data.get('school')
            account.class_in_school = data.get('class_in_school')
            # client should put facebook_id to enable this
            # account.facebook_id = data.get('facebook_id')
            account.save()

            update_session_auth_hash(request, user)

            return Response(UserAccountSerializer(account).data)

        return Response(user_serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
Beispiel #8
0
    def put(self, request, username, format=None):
        data = UserAccountSerializer(data=request.data).data

        user = User.objects.get(username=username)
        user_serializer = UserSerializer(user,
                                         data=data['user'])

        if user_serializer.is_valid():
            user_serializer.save()

            account = self.get_object(username)
            account.school = data.get('school')
            account.class_in_school = data.get('class_in_school')
            # client should put facebook_id to enable this
            # account.facebook_id = data.get('facebook_id')
            account.save()

            update_session_auth_hash(request, user)

            return Response(UserAccountSerializer(account).data)

        return Response(user_serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
Beispiel #9
0
    def post(self, request, format=None):
        serializer = UserAccountSerializer(data=request.data)

        if serializer.is_valid():
            data = serializer.validated_data['user']

            username = data.get('username')
            email = data.get('email')
            password = data.get('password')
            # client should post facebook_id to enable this
            # facebook_id = data.get('facebook_id')
            UserAccount.objects.create_user(
                username, email, password,
                **serializer.validated_data
            )

            return Response(serializer.validated_data,
                            status=status.HTTP_201_CREATED)

        return Response({
            'status': 'Bad request',
            'message': 'Account could not be created with received data.'
        }, status=status.HTTP_400_BAD_REQUEST)
Beispiel #10
0
    def put(self, request, username, format=None):
        serializer = UserAccountSerializer(data=request.data)

        if serializer.is_valid() is False:
            return Response(
                {"message": "Cannnot update profile with provided information"}, status=status.HTTP_400_BAD_REQUEST
            )

        data = serializer.validated_data

        password = data["user"].get("password")
        confirm_password = data["user"].get("confirm_password")

        if password != confirm_password:
            return Response(
                {"message": "Password and confirm password don't match"}, status=status.HTTP_400_BAD_REQUEST
            )

        user = User.objects.get(username=username)
        user_serializer = UserSerializer(user, data=data["user"])

        if user_serializer.is_valid():
            user_serializer.save()

            account = self.get_object(username)
            account.school = data.get("school")
            account.class_in_school = data.get("class_in_school")
            # client should put facebook_id to enable this
            # account.facebook_id = data.get('facebook_id')
            account.save()

            update_session_auth_hash(request, user)

            return Response(UserAccountSerializer(account).data)

        return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #11
0
 def get(self, request, format=None):
     users = UserAccount.objects.all()
     serializer = UserAccountSerializer(users, many=True)
     return Response(serializer.data)