def test_deleting_duplicates_excluding_cart(cart_lists_with_same_course): cart = cart_lists_with_same_course["cart"] course = cart.courses.first() CartListsToolkit.delete_duplicates_excluding_instance( course_slug=course.slug, instance=cart, **cart_lists_with_same_course) assert cart.courses.count() == 1 assert cart_lists_with_same_course["wishlist"].courses.count() == 0 assert cart_lists_with_same_course["saved_for_later"].courses.count() == 0 assert cart.courses.first() == course
def load_cart_api(request, *args, **kwargs): user = request.user ids = CartListsToolkit.get_cart_lists_ids_from_request(request) cart_lists = CartListsSelector.get_cart_lists_by_user_and_ids(user=request.user, ids=ids) cart = cart_lists["cart"] serializer = CartSerializer(instance=cart) return Response(serializer.data, status=200)
def test_getting_cart_ids_from_post_request(request_data, request): request.POST = request_data["POST"] request.GET = {} ids = CartListsToolkit.get_cart_lists_ids_from_request(request) assert ids["cart_id"] == request.POST.get("cart_id") assert ids["saved_for_later_id"] == request.POST.get("saved_for_later_id")
def remove_applied_coupon_api(request, *args, **kwargs): try: coupon_code = request.POST["coupon_code"] except KeyError: logger.error("Request object doesn't have a coupon code") return Response( {"error": DiscountErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400) ids = CartListsToolkit.get_cart_lists_ids_from_request(request) cart_lists = CartListsSelector.get_cart_lists_by_user_and_ids( user=request.user, ids=ids) cart = cart_lists["cart"] wishlist = cart_lists["wishlist"] saved_for_later = cart_lists["saved_for_later"] CouponToolkit.remove_applied_coupon(code=coupon_code, cart=cart) context = { "saved_for_later": saved_for_later, "wishlist": wishlist, } serializer = CartDiscountsInfoSerializer(instance=cart, context=context) return Response(serializer.data, status=200)
def load_saved_for_later_api(request, *args, **kwargs): user = request.user ids = CartListsToolkit.get_cart_lists_ids_from_request(request) cart_lists = CartListsSelector.get_cart_lists_by_user_and_ids(user=request.user, ids=ids) saved_for_later = cart_lists["saved_for_later"] cart = cart_lists["cart"] serializer = SavedForLaterSerializer(instance=saved_for_later, context={"cart": cart}) return Response(serializer.data, status=200)
def add_course_to_wishlist_api(request, *args, **kwargs): try: course_slug = request.POST["course_slug"] except KeyError: logger.error("Request object doesn't have a slug field") return Response({"error": CartErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400) user = request.user ids = CartListsToolkit.get_cart_lists_ids_from_request(request) cart_lists = CartListsSelector.get_cart_lists_by_user_and_ids(user=request.user, ids=ids) try: wishlist = WishlistToolkit.add_course_to_wishlist(wishlist=cart_lists["wishlist"], course_slug=course_slug) except Course.DoesNotExist: return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400) CartListsToolkit.delete_duplicates_excluding_instance(course_slug=course_slug, instance=wishlist, **cart_lists) serializer = CartOnlyInfoSerializer(instance=cart_lists["cart"]) return Response(serializer.data, status=200)
def remove_course_from_saved_for_later(request, *args, **kwargs): try: course_slug = request.POST["course_slug"] except KeyError: logger.error("Request object doesn't have a slug field") return Response({"error": CartErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400) user = request.user ids = CartListsToolkit.get_cart_lists_ids_from_request(request) cart_lists = CartListsSelector.get_cart_lists_by_user_and_ids(user=request.user, ids=ids) try: SavedForLaterToolkit.remove_course_from_saved_for_later(s_list=cart_lists["saved_for_later"], course_slug=course_slug) except Course.DoesNotExist: return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400) return Response({}, status=200)
def get_cart_info_api(request, *args, **kwargs): user = request.user ids = CartListsToolkit.get_cart_lists_ids_from_request(request) cart = CartToolkit.load_cart(user=user, cart_id=ids["cart_id"]) serializer = CartOnlyInfoSerializer(instance=cart) return Response(serializer.data, status=200)