Example #1
0
    def partial_update(self, request, pk=None):
        user = get_or_none(User, id=pk)

        if not isinstance(user, User):
            return Response({"status": "User Doesn't exists"},
                            status=status.HTTP_404_NOT_FOUND)

        user_serializer = UserSerializer(instance=user,
                                         data=request.data,
                                         partial=True)
        if user_serializer.is_valid():
            user_serializer.save()
            response = {
                "status_code": status.HTTP_202_ACCEPTED,
                "status": "Success",
                "msg": "User Updated",
                "data": user_serializer.data
            }
            return Response(data=response, status=status.HTTP_202_ACCEPTED)
        response = {
            "status_code": status.HTTP_400_BAD_REQUEST,
            "status": "Error",
            "msg": "User not Created",
            "data": user_serializer.errors
        }
        return Response(data=response, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
    def create(self, request):
        if "created_by" not in request.data:
            request.data["created_by"] = self.request.user.username
        if "changed_by" not in request.data:
            request.data["changed_by"] = self.request.user.username
        try:

            profile_data = {
                "name": request.data["name"],
                "phone": request.data["phone"],
                "email": request.data.get("email",None),
                "contact_person_name": request.data.get("contact_person_name", None),
                "contact_person_phone": request.data.get("contact_person_phone", None),
                "comment": request.data.get("remarks", None)
            }
        except KeyError:
            return Response({"error": "Key Error"}, status=status.HTTP_400_BAD_REQUEST)

        username = generate_username(profile_data["name"], profile_data["phone"])
        password = "******"

        user_serializer = UserSerializer(data={"username": username, "password": password})
        if user_serializer.is_valid():
            user = user_serializer.save()
            profile_data["user"] = user.id
            profile_serializer = ProfileSerializer(data=profile_data)
            if profile_serializer.is_valid():
                profile_serializer.save()
                request.data["name"] = user.id
                request.data["owner_phone"] = profile_data["phone"]
                owner_serializer = OwnerSerializer(data=request.data)
                if owner_serializer.is_valid():
                    owner = owner_serializer.save()
                    Vehicle.objects.filter(id__in=request.data.get("vehicles_detail", [])).update(owner=owner)
                    response = {
                        "status_code": status.HTTP_201_CREATED,
                        "status": "Success",
                        "msg": "Owner Created",
                        "data": owner_serializer.data
                    }
                    return Response(data=response, status=status.HTTP_201_CREATED)
                user.delete()
                response = {
                    "status_code": status.HTTP_400_BAD_REQUEST,
                    "status": "Error",
                    "msg": "Owner not Created",
                    "data": owner_serializer.errors
                }
                return Response(data=response, status=status.HTTP_400_BAD_REQUEST)
            user.delete()
            return Response(profile_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #3
0
 def post(self, request, *args, **kwargs):
     serializer = self.serializer_class(data=request.data,
                                        context={'request': request})
     if serializer.is_valid():
         user = serializer.validated_data['user']
         user_serializer = UserSerializer(user)
         token, created = Token.objects.get_or_create(user=user)
         response = Response(
             {
                 'status': 'success',
                 'msg': 'Login Successful',
                 'token': token.key,
                 'data': user_serializer.data,
                 'status_code': status.HTTP_200_OK
             },
             status=status.HTTP_200_OK)
         return response
     response = Response(
         {
             'status': 'failure',
             'msg': 'Login Unsuccessful',
             'data': serializer.errors,
             'status_code': status.HTTP_400_BAD_REQUEST
         },
         status=status.HTTP_400_BAD_REQUEST)
     return response
Example #4
0
 def create_user(self, request):
     user_serializer = UserSerializer(data=request.data)
     if not request.data.get('profile', None):
         response = {
             "status_code": status.HTTP_400_BAD_REQUEST,
             "status": "failure",
             "msg": "Profile field is mandatory",
             "data": {}
         }
         return Response(data=response, status=status.HTTP_400_BAD_REQUEST)
     if not user_serializer.is_valid():
         response = {
             "status_code": status.HTTP_400_BAD_REQUEST,
             "status": "failure",
             "msg": "User Register not Created",
             "data": user_serializer.errors
         }
         return Response(data=response, status=status.HTTP_400_BAD_REQUEST)
     user_serializer.save()
     if type(request.data['profile']) is not dict:
         response = {
             "status_code": status.HTTP_400_BAD_REQUEST,
             "status": "failure",
             "msg": "Profile field is not dictionary",
             "data": user_serializer.errors
         }
         return Response(data=response, status=status.HTTP_400_BAD_REQUEST)
     request.data['profile']['user'] = user_serializer.data['id']
     profile_serializer = ProfileSerializer(data=request.data['profile'])
     if not profile_serializer.is_valid():
         response = {
             "status_code": status.HTTP_400_BAD_REQUEST,
             "status": "failure",
             "msg": "User Register not Created",
             "data": profile_serializer.errors
         }
         return Response(data=response, status=status.HTTP_400_BAD_REQUEST)
     profile_serializer.save()
     # Add entry in respective tables as per user category
     response = {
         "status_code": status.HTTP_201_CREATED,
         "status": "success",
         "msg": "User Register Created",
         "data": user_serializer.data
     }
     return Response(data=response, status=status.HTTP_201_CREATED)
Example #5
0
 def create(self, request):
     user_serializer = UserSerializer(data=request.data)
     if user_serializer.is_valid():
         user_serializer.save()
         response = {
             "status_code": status.HTTP_201_CREATED,
             "status": "Success",
             "msg": "User Created",
             "data": user_serializer.data
         }
         return Response(data=response, status=status.HTTP_201_CREATED)
     response = {
         "status_code": status.HTTP_400_BAD_REQUEST,
         "status": "Error",
         "msg": "User not Created",
         "data": user_serializer.errors
     }
     return Response(data=response, status=status.HTTP_400_BAD_REQUEST)
Example #6
0
 def retrieve(self, request, pk=None):
     user = get_or_none(User, id=pk)
     if not isinstance(user, User):
         return Response({"error": "User does not exist"},
                         status=status.HTTP_404_NOT_FOUND)
     user_serializers = UserSerializer(user).data
     if Profile.objects.filter(user=user).exists():
         profile_serializer = ProfileSerializer(user.profile).data
     else:
         profile_serializer = {}
     user_serializers["profile"] = profile_serializer
     return Response(user_serializers, status=status.HTTP_200_OK)
Example #7
0
 def post(self, request):
     token = request.data.get('token')
     token_obj = get_or_none(Token, key=token)
     if isinstance(token_obj, Token) and isinstance(token_obj.user, User):
         user_serializer = UserSerializer(token_obj.user)
         return Response(
             {
                 'status': 'success',
                 'msg': 'Login Successful',
                 'token': token_obj.key,
                 'data': user_serializer.data,
                 'status_code': status.HTTP_200_OK
             },
             status=status.HTTP_200_OK)
     return error_response(status=status.HTTP_400_BAD_REQUEST,
                           msg='User doesnot exits',
                           data={})