Exemple #1
0
    def put(self, request, *args, **kwargs):
        """
        takes label data as input and if the data is valid then it replaces the data in database
        :rtype:Response returns success or failure message along with status
        """

        try:
            item = Label.objects.filter(Q(pk=kwargs.get('pk')) &
                                            (Q(user=kwargs.get('user').id))).exclude(is_deleted=True).first()
            if item is None:
                raise MyCustomError(ExceptionType.UnAuthorized, "No such note exist")
            serializer = LabelSerializer(item, data=request.data, partial=True)

            if serializer.is_valid():
                serializer.save()
                return utils.manage_response(status=True, message="Label Update Successfully", data=serializer.data,
                                             status_code=status.HTTP_201_CREATED)
            raise MyCustomError(ExceptionType.LengthError, "title should be less than 50 characters")

        except MyCustomError as e:
            return utils.manage_response(status=False, message=e.message, exception=str(e),
                                         status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(status=False, message='some other issue please try after some time',
                                         exception=str(e),
                                         status_code=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
 def validate_data(data: dict) -> object:
     """
     it takes data as input and returns boolean value
     :rtype: boolean
     """
     if 'email' not in data or data['email'] is '':
         raise MyCustomError(ExceptionType.EmptyField, "email field should not be empty")
     if 'password' not in data or data['password'] is '':
         raise MyCustomError(ExceptionType.EmptyField, "password field should not be empty")
     return True
Exemple #3
0
    def get(self, request, *args, **kwargs):
        """
        this method takes pk from url in kwargs and return the particular trash note if exist. else it will return
        all trash notes of particular user
        :param kwargs: it takes primary key and user account object as input
        :return: all the request trash notes which user have requested
        """
        try:
            if kwargs.get('pk'):
                item = Note.objects.filter(
                    Q(pk=kwargs.get('pk')) & Q(is_deleted=False)
                    & Q(trash=True)
                    & (Q(user=kwargs.get('user').id)
                       | Q(collaborate=kwargs.get('user').id))).first()
                if item is None:
                    raise MyCustomError(ExceptionType.UnAuthorized,
                                        "Note doesn't exist")
                serializer = NoteSerializer(item)
                return utils.manage_response(
                    status=True,
                    message="Trashed note retrieved successfully",
                    data=serializer.data,
                    status_code=status.HTTP_200_OK)

            else:
                item = Note.objects.filter(
                    (Q(user_id=kwargs.get('user').id)
                     | Q(collaborate=kwargs.get('user').id))
                    & Q(trash=True)).exclude(is_deleted=True)
                if item is None:
                    raise MyCustomError(ExceptionType.UnAuthorized,
                                        "Note doesn't exist")
                serializer = NoteSerializer(item, many=True)
                return utils.manage_response(
                    status=True,
                    message="Trashed notes retrieved successfully",
                    data=serializer.data,
                    status_code=status.HTTP_200_OK)
        except MyCustomError as e:
            return utils.manage_response(
                status=False,
                message=e.message,
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(
                status=False,
                message='some other issue please try after some time',
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
Exemple #4
0
 def validate_register(data: dict) -> object:
     '''
     this method takes dictionary data as input and it validates the length of characters
     :return: True if the data is valid. else it raises LengthError Exception.
     '''
     if len(data['email']) > 50:
         raise MyCustomError(ExceptionType.LengthError, "email maximum length should be 50 character")
     if len(data['password']) > 68 or len(data['password']) <= 6:
         raise MyCustomError(ExceptionType.LengthError, "password length should be between 6 to 50 character")
     if len(data['last_name']) > 20:
         raise MyCustomError(ExceptionType.LengthError, "lastname maximum length should be 20 character")
     if len(data['user_name']) > 20:
         raise MyCustomError(ExceptionType.LengthError, "username maximum length should be 20 character")
     return True
    def validate(self, attrs):
        """
        it verifies the credentials, if credentials were matched then returns data in json format, else throws exception
        :return: return json data if credentials are matched
        """
        email = attrs.get('email', '')
        password = attrs.get('password', '')
        user = auth.authenticate(email=email, password=password)
        if not user:
            raise MyCustomError(ExceptionType.InvalidCredentials,
                                "email or password is incorrect")
        if not user.is_verified:
            raise MyCustomError(ExceptionType.UnVerifiedAccount,
                                "please verify your account first to login")

        return {'email': user.email}
Exemple #6
0
    def get(self, *args, **kwargs):
        """
        takes key as input and if data exists for that key then it returns the data from database
        :rtype:Response returns data along if it is success else returns failure message along with status
        """

        try:

            if kwargs.get('pk'):
                item = Label.objects.filter(Q(pk=kwargs.get('pk')) &
                                            (Q(user=kwargs.get('user').id))).exclude(is_deleted=True).first()
                if item is None:
                    raise MyCustomError(ExceptionType.UnAuthorized, "No such note exist")
                serializer = LabelSerializer(item)
                return utils.manage_response(status=True, message="Label retrieved successfully",
                                             data=serializer.data,
                                             status_code=status.HTTP_200_OK)

            else:
                labels = Label.objects.filter(Q(user=kwargs.get('user').id))
                serializer = LabelSerializer(labels, many=True)
                return utils.manage_response(status=True, message="Labels retrieved successfully",
                                             data=serializer.data,
                                             status_code=status.HTTP_200_OK)
        except MyCustomError as e:
            return utils.manage_response(status=False, message=e.message, exception=str(e),
                                         status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(status=False, message='some other issue please try after some time',
                                         exception=str(e),
                                         status_code=status.HTTP_400_BAD_REQUEST)
Exemple #7
0
    def put(self, request, *args, **kwargs):
        """
        this method takes pk from url in kwargs, data from user in request and update the particular note if exist. else it will raise
        exception saying that note doesn't exist
        :param request: takes the note data from the user
        :param kwargs: it takes primary key and user account object as input
        :return: response of the updated note details
        """
        try:

            item = Note.objects.filter(
                Q(pk=kwargs.get('pk'))
                & (Q(user=kwargs.get('user').id)
                   | Q(collaborate=kwargs.get('user').id))).exclude(
                       trash=True).first()
            if item is None:
                raise MyCustomError(ExceptionType.UnAuthorized,
                                    "Note doesn't exist")
            data = request.data
            serializer = NoteSerializer(item, data=data, partial=True)
            if serializer.is_valid():
                serializer.save()
                notes = serializer.data
                notes = Encrypt.encode(notes)
                cache.set_cache("NOTE_" + str(kwargs.get('pk')) + "_DETAIL",
                                notes)
                return utils.manage_response(
                    status=True,
                    message="Note Update Successfully",
                    data=serializer.data,
                    status_code=status.HTTP_201_CREATED)
            raise MyCustomError(ExceptionType.ValidationError,
                                "You have entered invalid details")

        except MyCustomError as e:
            return utils.manage_response(
                status=False,
                message=e.message,
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(
                status=False,
                message='some other issue please try after some time',
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
Exemple #8
0
    def get(self, request, *args, **kwargs):
        """
        this method takes pk from url in kwargs and get the particular note data if exist. else it will raise
        exception saying that note doesn't exist
        :param kwargs: it takes primary key and user account object as input
        :return: response of the requested note details
        """

        try:
            if kwargs.get('pk'):
                if cache.get_cache("NOTE_" + str(kwargs.get('pk')) +
                                   "_DETAIL") is not None:
                    notes = cache.get_cache("NOTE_" + str(kwargs.get('pk')) +
                                            "_DETAIL")
                    notes = Encrypt.decode(notes)
                else:
                    notes = Note.objects.filter(
                        Q(pk=kwargs.get('pk'))
                        & (Q(user=kwargs.get('user').id)
                           | Q(collaborate=kwargs.get('user').id))).exclude(
                               trash=True).first()
                    if notes is None:
                        raise MyCustomError(ExceptionType.UnAuthorized,
                                            "note doesn't exist")
                    serializer = NoteSerializer(notes)
                    notes = serializer.data
                    cache_notes = Encrypt.encode(notes)
                    cache.set_cache(
                        "NOTE_" + str(kwargs.get('pk')) + "_DETAIL",
                        cache_notes)
                return utils.manage_response(
                    status=True,
                    message="data retrieved successfully",
                    data=notes,
                    status_code=status.HTTP_200_OK)

            else:
                notes = Note.objects.filter(
                    Q(user=kwargs.get('user').id)
                    | Q(collaborate=kwargs.get('user').id)).exclude(trash=True)
                serializer = NoteSerializer(notes, many=True)
                return utils.manage_response(
                    status=True,
                    message="data retrieved successfully",
                    data=serializer.data,
                    status_code=status.HTTP_200_OK)
        except MyCustomError as e:
            return utils.manage_response(
                status=False,
                message=e.message,
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(
                status=False,
                message='some other issue please try after some time',
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
Exemple #9
0
 def post(self, request, *args, **kwargs):
     """
     this method takes data from user in request and creates a new note if details provided are valid, else it will raise
     exception
     :param request: takes the note data from the user
     :param kwargs: it takes user account object as input
     :return: response of the created note details
     """
     try:
         utils.set_user(request, kwargs.get('user').id)
         if request.data.get('collaborate'):
             utils.get_collaborator_list(request)
         if request.data.get('label'):
             utils.get_label_list(request)
         data = request.data
         serializer = NoteSerializer(data=data)
         if 'title' not in data or data[
                 'title'] is '' or 'description' not in data or data[
                     'description'] is '':
             raise MyCustomError(ExceptionType.ValidationError,
                                 "title and description required")
         if serializer.is_valid():
             serializer.save()
             serialized_data = serializer.data
             return utils.manage_response(
                 status=True,
                 message='Note Added Successfully',
                 data=serialized_data,
                 status_code=status.HTTP_201_CREATED)
         raise MyCustomError(ExceptionType.LengthError,
                             "title should be less than 150 characters")
     except MyCustomError as e:
         return utils.manage_response(
             status=False,
             message=e.message,
             exception=str(e),
             status_code=status.HTTP_400_BAD_REQUEST)
     except Exception as e:
         return utils.manage_response(
             status=False,
             message='some other issue please try after some time',
             exception=str(e),
             status_code=status.HTTP_400_BAD_REQUEST)
Exemple #10
0
    def validate(self, attrs):
        """
        it verifies credentials, if credentials are in correct format then after email verification it will create account for user
        :rtype: user data and its status if credentials are valid
        """
        email = attrs.get('email', '')
        user_name = attrs.get('user_name', '')

        if not user_name.isalnum():
            raise MyCustomError(
                ExceptionType.ValidationError,
                "username should contain only alphanumeric characters")
        return attrs
Exemple #11
0
    def validate(self, attrs):
        """
        it take new password and confirm password and if the password matches all criteria then it will set new password
        :rtype: data of the user and its success status
        """
        try:
            password = attrs.get('password')
            token = attrs.get('token')
            uidb64 = attrs.get('uidb64')

            id = force_str(urlsafe_base64_decode(uidb64))
            user = Account.objects.get(id=id)
            if not PasswordResetTokenGenerator().check_token(user, token):
                raise MyCustomError(ExceptionType.UnAuthorized,
                                    "The reset link is invalid")

            user.set_password(password)
            user.save()

            return user
        except Exception as e:
            raise MyCustomError(ExceptionType.UnAuthorized,
                                "The reset link is invalid")
Exemple #12
0
    def get(self, request, **kwargs):
        """
        this method takes user id from kwargs and return all the search note if exist.
        :param kwargs: it takes user account object as input
        :return: all the notes which user have requested
        """
        try:
            current_user = kwargs.get('user').id
            search_terms = request.query_params.get('q')
            search_term_list = search_terms.split(' ')

            if search_terms == '':
                raise MyCustomError(
                    ExceptionType.EmptyField,
                    "please enter the note you want to search")

            notes = Note.objects.filter(
                Q(user=current_user)
                | Q(collaborate=current_user)).exclude(trash=True)

            search_query = Q(title__icontains=search_term_list[0]) | Q(
                description__icontains=search_term_list[0])

            for term in search_term_list[1:]:
                search_query.add((Q(title__icontains=term)
                                  | Q(description__icontains=term)),
                                 search_query.AND)

            notes = notes.filter(search_query)

            serializer = NoteSerializer(notes, many=True)
            return utils.manage_response(
                status=True,
                message='retrieved notes on the basis of search terms',
                data=serializer.data,
                status_code=status.HTTP_200_OK)
        except MyCustomError as e:
            return utils.manage_response(
                status=False,
                message=e.message,
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(
                status=False,
                message='some other issue please try after some time',
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
Exemple #13
0
    def delete(self, *args, **kwargs):
        """
        takes key as input inside kwargs and if data exists for that key then it returns the delete that data from database
        :rtype:Response returns success or failure message along with status
        """

        try:
            item = Label.objects.filter(Q(pk=kwargs.get('pk')) &
                                            (Q(user=kwargs.get('user').id))).exclude(is_deleted=True).first()
            if item is None:
                raise MyCustomError(ExceptionType.UnAuthorized, "No such note exist")

            item.soft_delete()
            return utils.manage_response(status=True, message="Label Deleted Successfully",
                                             status_code=status.HTTP_202_ACCEPTED)
        except MyCustomError as e:
            return utils.manage_response(status=False, message=e.message, exception=str(e),
                                         status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(status=False, message='some other issue please try after some time',
                                         exception=str(e),
                                         status_code=status.HTTP_400_BAD_REQUEST)
Exemple #14
0
    def delete(self, request, *args, **kwargs):
        """
        this method takes pk from url in kwargs and delete the particular note if exist. else it will raise
        exception saying that note doesn't exist
        :param kwargs: it takes primary key and user account object as input
        :return: response saying that note successfully deleted
        """
        try:
            note = Note.objects.filter(
                Q(pk=kwargs.get('pk'))
                & (Q(user=kwargs.get('user').id)
                   | Q(collaborate=kwargs.get('user').id))).exclude(
                       trash=True).first()
            if note is None:
                raise MyCustomError(ExceptionType.UnAuthorized,
                                    "Note doesn't exist")

            note.soft_delete()
            if cache.get_cache("NOTE_" + str(kwargs.get('pk')) +
                               "_DETAIL") is not None:
                cache.delete_cache("NOTE_" + str(kwargs.get('pk')) + "_DETAIL")
            return utils.manage_response(
                status=True,
                message="Note Deleted Successfully",
                status_code=status.HTTP_204_NO_CONTENT)

        except MyCustomError as e:
            return utils.manage_response(
                status=False,
                message=e.message,
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(
                status=False,
                message='some other issue please try after some time',
                exception=str(e),
                status_code=status.HTTP_400_BAD_REQUEST)
Exemple #15
0
    def post(self, request, *args, **kwargs):
        """
        takes label data as input and if the data is valid then it stores the data in database
        :rtype:Response returns success or failure message along with status
        """

        try:
            _mutable = request.data._mutable
            request.data._mutable = True
            request.data['user'] = str(kwargs.get('user').id)
            request.data._mutable = _mutable
            serializer = LabelSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                return utils.manage_response(status=True, message='Label Added Successfully', data=serializer.data,
                                             status_code=status.HTTP_201_CREATED)
            raise MyCustomError(ExceptionType.LengthError, "maximum length of label name should be 50 characters")
        except MyCustomError as e:
            return utils.manage_response(status=False, message=e.message, exception=str(e),
                                         status_code=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            return utils.manage_response(status=False, message='some other issue please try after some time',
                                         exception=str(e),
                                         status_code=status.HTTP_400_BAD_REQUEST)