Ejemplo n.º 1
0
def delete_user(token, cache_instance):
    try:
        payload = jwt.decode(token, settings.SECRET_KEY)
        user_id = payload["user_id"]
        if cache_instance.del_value("refresh_" + str(user_id)) == 1:
            return 1
        return 0
    except InvalidSignatureError:
        raise BookstoreError.BookStoreError(
            responses.get_response("InvalidSignatureError"), 401)
    except DecodeError:
        raise BookstoreError.BookStoreError(
            responses.get_response("DecodeError"), 401)
Ejemplo n.º 2
0
def get_books(request, sort="", id=0):
    """
    API to get all the books available or
    book corresponding to an id, author or title of book

    Parameter:
    argument(1): value for sort(name of field to be sorted by)
    argument(2): id to search book details corresponding to it
    argument(3): request body with either title or author of book

    Returns:
    Details of all books available sorted by field name given,
    by default returns books details by insertion order or
    details of book corresponding to the id or title or author
    name passed.
    """
    try:
        books = get_book(request, id, sort)
        serializer = ProductSerializer(books, many=True)
        if serializer.is_valid:
            if serializer.data == []:
                return Response(
                    responses.get_response("EmptyBookList"),
                    status=responses.get_response("EmptyBookList").get("status"),
                )
            else:
                return Response(data=serializer.data, status=status.HTTP_200_OK)
        else:
            logger.error(serializer.errors)
            return Response(
                {
                    "error_message": serializer.errors,
                    "Status_code": status.HTTP_500_INTERNAL_SERVER_ERROR,
                },
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )
    except Product.DoesNotExist:
        return Response(
            responses.get_response("EmptyBookList"),
            status=responses.get_response("EmptyBookList").get("status"),
        )
    except FieldError:
        return Response(
            responses.get_response("FieldError"),
            status=responses.get_response("FieldError").get("status"),
        )
    except Exception as e:
        logger.error(e)
        return Response(
            responses.get_response("GetBookError"),
            status=responses.get_response("GetBookError").get("status"),
        )
Ejemplo n.º 3
0
 def del_value(self, key):
     try:
         return self.redis_instance.delete(key)
     except redis.exceptions.ConnectionError:
         raise BookstoreError.BookStoreError(
             responses.get_response("RedisConnectionError"), 500)
Ejemplo n.º 4
0
 def set_value(self, key, value):
     try:
         self.redis_instance.set(key, value)
     except redis.exceptions.ConnectionError:
         raise BookstoreError.BookStoreError(
             responses.get_response("RedisConnectionError"), 500)