Ejemplo n.º 1
0
def update_user(request, pk=None):
    if pk is None:
        pk = request.user.id

    user = User.objects.get(id=pk)
    serialized = postSerializers.UserSerializer(user,
                                                data=request.data,
                                                partial=True)

    if serialized.is_valid():
        user.username = request.data.get("username")
        user.email = request.data.get("email")
        user.first_name = request.data.get("first_name")
        user.last_name = request.data.get("last_name")

        if request.data.get("password"):
            if request.data.get("password") != request.data.get("repassword"):
                return Response({"password": ["Hasła muszą być takie same"]},
                                status=HTTP_400_BAD_REQUEST)

            user.password = make_password(request.data.get("password"),
                                          salt=None,
                                          hasher='default')

        user.save()
        return Response(postSerializers.UserSerializer(instance=user).data,
                        status=HTTP_202_ACCEPTED)
    else:
        return Response(serialized._errors, status=HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
def api_get_user(request, pk=None):
    if not pk:
        pk = request.user.id

    objects = User.objects.select_related().filter(id=pk)
    serializer = postSerializers.UserSerializer(objects, many=True)
    return Response({"user": serializer.data})
Ejemplo n.º 3
0
    def get(self, request: Request, username: str, *args, **kwargs):
        user = self.get_user_or_404(username)

        following = filters.FollowersFilterSet(request.GET,
                                               queryset=user.following).qs
        return Response([
            serializers.UserSerializer(follow.followee).data
            for follow in following
        ])
Ejemplo n.º 4
0
def api_register(request):
    VALID_USER_FIELDS = [f.name for f in User._meta.fields]
    DEFAULTS = {
        # you can define any defaults that you would like for the user, here
    }
    serialized = postSerializers.UserSerializer(data=request.data)
    if serialized.is_valid():
        user_data = {
            field: data
            for (field, data) in request.data.items()
            if field in VALID_USER_FIELDS
        }
        user_data.update(DEFAULTS)
        user = User.objects.create_user(**user_data)
        user.is_active = False
        user.save()
        return Response(postSerializers.UserSerializer(instance=user).data,
                        status=HTTP_201_CREATED)
    else:
        return Response(serialized._errors, status=HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
def api_login(request):
    username = request.data.get("username")
    password = request.data.get("password")
    if username is None or password is None:
        return Response({'message': 'Proszę podać nazwę użytkownika i hasło'},
                        status=HTTP_400_BAD_REQUEST)
    user = authenticate(username=username, password=password)
    if not user:
        return Response({'message': 'Niepoprawne dane logowania'},
                        status=HTTP_404_NOT_FOUND)

    token, _ = Token.objects.get_or_create(user=user)
    serializer = postSerializers.UserSerializer(user)

    return Response({
        'token': token.key,
        'user': serializer.data
    },
                    status=HTTP_200_OK)
Ejemplo n.º 6
0
def api_get_users(request):
    objects = User.objects.all()
    serializer = postSerializers.UserSerializer(objects, many=True)
    return Response({"users": serializer.data})