Esempio n. 1
0
def list_of_users(request):
    if request.method == 'GET':
        users = Person.objects.all()
        serializer = PersonSerializer(users, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        u_serializer = UserSerializer(data=request.data.get("user"))
        if u_serializer.is_valid():
            u_serializer.save()
            data = request.data
            rating = Rating.objects.create()
            data['user_id'] = u_serializer.data.get("id")
            data['rating_id'] = rating.id
            p_serializer = PersonSerializer(data=data)
            if p_serializer.is_valid():
                p_serializer.save()
                return Response(p_serializer.data, status=status.HTTP_201_CREATED)
            return Response(p_serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Esempio n. 2
0
def current_user(request):
    try:
        person = Person.objects.get(user=request.user)
        user = User.objects.get(id=person.user.id)
    except Exception as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        if request.method == 'GET':
            serializer = PersonSerializer(person)
            return Response(serializer.data)

    elif request.method == 'PUT':
        data = request.data
        if data.get("user"):
            user_data = data.pop("user")
            user_data["username"] = user_data.get("username") or user.username
            serializer = UserSerializer(instance=user, data=user)
            if serializer.is_valid():
                serializer.save()
            else:
                return Response({'error': serializer.errors})
        data["user_id"] = user.id
        data["gender"] = data.get("gender") or person.gender
        data["birth_date"] = data.get("birth_date") or person.birth_date
        data["availability"] = data.get("availability") or person.availability
        data["reviews"] = data.get("reviews") or person.reviews

        serializer = PersonSerializer(instance=person, data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response({'error': serializer.errors})

    elif request.method == 'DELETE':
        person.delete()
        user.delete()
        return Response({'deleted': True})