Ejemplo n.º 1
0
 def put(self, request, username, format=None):
     user = helpers.get_user_by_username(self, request, username)
     serializer = UserSerializer(user, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
 def put(self, request, username, format=None):
     user = helpers.get_user_by_username(self, request, username)
     serializer = UserSerializer(user, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
 def post(self, request, *args, **kwargs):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         # Update the password so that its hashed.
         user = CustomUser.objects.get(username=request.data.get("username"))
         user.set_password(request.data.get("password"))
         user.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 4
0
 def create(self, request, *args, **kwargs):
     user = request.user
     admin = Group.objects.get(name="admin")
     teacher = Group.objects.get(name="teacher")
     serializer = UserSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     if admin in user.groups.all():
         pass
     elif teacher in user.groups.all():
         if request.data.get("type") != "student":
             return Response({"error": "Operation not allowed"},
                             status=status.HTTP_401_UNAUTHORIZED)
     else:
         return Response({"error": "Operation not allowed"},
                         status=status.HTTP_401_UNAUTHORIZED)
     self.perform_create(serializer)
     headers = self.get_success_headers(serializer.data)
     return Response(serializer.data,
                     status=status.HTTP_201_CREATED,
                     headers=headers)
Ejemplo n.º 5
0
 def post(self, request, *args, **kwargs):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         # Update the password so that its hashed.
         user = CustomUser.objects.get(
             username=request.data.get('username'))
         user.set_password(request.data.get('password'))
         user.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 6
0
 def put(self, request, pk, format=None):
     user = self.get_object(pk)
     user_request_data = request.data.copy()
     if user_request_data.get('password'):
         hased_password = Helper.get_hashed_password(
             user_request_data.get('password'))
         user_request_data['password'] = hased_password
     serialized_user = UserSerializer(user, user_request_data)
     if serialized_user.is_valid():
         serialized_user.save()
         return Response(serialized_user.data)
     return Response(serialized_user.errors,
                     status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 7
0
 def post(self, request):
     user_data = request.data.copy()
     if user_data.get('password'):
         hased_password = Helper.get_hashed_password(
             user_data.get('password'))
         user_data['password'] = hased_password
     serialized_user = UserSerializer(data=user_data)
     if serialized_user.is_valid():
         serialized_user.save()
         return Response(serialized_user.data,
                         status=status.HTTP_201_CREATED)
     return Response(serialized_user.errors,
                     status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 8
0
 def post(self, request, format='json'):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         user = authenticate(username=serializer.data['username'],
                             password=serializer.data['password'])
         if user is not None:
             token = Token.objects.get(user=user)
             group = user.groups.all()[0].name
             json = serializer.data
             json['token'] = token.key
             json['type'] = group
             return Response(json, status=status.HTTP_200_OK)
         return Response(status=status.HTTP_401_UNAUTHORIZED)
     return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
Ejemplo n.º 9
0
    def post(self, request, format='json'):
        return Response('hello')

        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save()
            if user:
                "******"
                token = Token.objects.create(user=user)
                json = serializer.data
                json['token'] = token.key
                return Response(json, status=status.HTTP_201_CREATED)
            "creation of token after sucessiful registration"

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 10
0
def registereduser(request):
    """
    List all snippets, or create a new snippet.
    """
    if request.method == 'GET':
        #user = UserProfile.objects.all()
        user = User.objects.all()
        serializer = UserSerializer(user, many=True)
        return Response(serializer.data)
        #return Response('ione');

    elif request.method == 'POST':
        serializer = RegisterSerializer(data=request.data)
        try:
            if serializer.is_valid():
                user = Register.objects.get(
                    phonenumber=request.data['phonenumber'])
                return Response('already register')
        except Register.DoesNotExist:
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)