def get_discounts_for_cart(cls, cart: Cart) -> List[CourseDiscount]: courses = CourseSelector.get_courses_by_cart(cart=cart) discounts = [ CourseDiscount.get_or_nothing(course=course, cart=cart) for course in courses.iterator() ] return discounts
def get_courses(self, obj): courses = CourseSelector.get_courses_by_cart(cart=obj) return CartCourseSerializer(instance=courses, many=True, context={ "cart": obj }).data
def get_courses(self, obj): cart = self.context["cart"] courses = CourseSelector.get_courses_by_wishlist(wishlist=obj) return CartCourseSerializer(instance=courses, many=True, context={ "cart": cart }).data
def get_courses(self, obj): cart = self.context["cart"] courses = CourseSelector.get_courses_by_saved_for_later( saved_for_later=obj) return CartCourseSerializer(instance=courses, many=True, context={ "cart": cart }).data
def get_discounts_for_saved_for_later( cls, cart: Cart, saved_for_later: SavedForLater) -> List[SavedForLater]: courses = CourseSelector.get_courses_by_saved_for_later( saved_for_later=saved_for_later) discounts = [ CourseDiscount.get_or_nothing(course=course, cart=cart) for course in courses.iterator() ] return discounts
def get_duration_time(self, obj): try: return CourseDurationTimeSerializer( CourseSelector.get_course_duration_time_by_course(course=obj) ).data except CourseContent.DoesNotExist: logger.warning("Course content doesn't exist so duration time won't be set to serialized course object") except CourseDurationTime.DoesNotExist: logger.warning("Course duration time doesn't exist and won't be set to serialized course object") return None
def update_cart_totals(cls, cart: Cart) -> Cart: courses = CourseSelector.get_courses_by_cart(cart=cart) subtotal = 0 total = 0 for course in courses: subtotal += course.price discount = DiscountSelector.get_discount_for_course_or_nothing( course=course, cart=cart) if discount is None: total += course.price else: total += discount.new_price cart.subtotal = subtotal cart.total = total cart.save() return cart
def delete_duplicates_excluding_instance(cls, course_slug: str, instance: Union[Cart, Wishlist, SavedForLater], *args, **kwargs): course = CourseSelector.get_course_by_slug(slug=course_slug) cart = kwargs.get("cart") if cart and cart is not instance and course in cart.courses.all(): cart.courses.remove(course) wishlist = kwargs.get("wishlist") if wishlist and wishlist is not instance and course in wishlist.courses.all( ): wishlist.courses.remove(course) saved_for_later = kwargs.get("saved_for_later") if saved_for_later and saved_for_later is not instance and course in saved_for_later.courses.all( ): saved_for_later.courses.remove(course)
def get_course_info_api(request, *args, **kwargs): """Api to get information about course wihout any real content like video/article...""" try: data = request.GET slug = data["slug"] except KeyError: logger.error("Request object doesn't have a slug field") return Response( {"error": CourseErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400) try: course = CourseSelector.get_course_by_slug(slug=slug) except Course.DoesNotExist: logger.warning(f"Course with slug - {slug} doesn't exist") return Response( {"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=404) serializer = CourseSerializer(instance=course) return Response(serializer.data, status=200)
def fetch_category_courses_api(request, *args, **kwargs): data = request.GET try: slug = data["slug"] except KeyError: logger.error("Request object doesn't have a slug field") return Response( {"error": CategoryErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400) try: category = get_category_by_slug(slug=slug) except Category.DoesNotExist: logger.warning(f"Category with slug - {slug} doesn't exist") return Response( { "error": CategoryErrorMessages.CATEGORY_DOES_NOT_EXIST_ERROR.value }, status=404) courses = CourseSelector.get_courses_by_category(category=category) serializer = CategoryCourseSerializer(courses, many=True) return Response(serializer.data, status=200)
def check_on_course_in_wishlist(cls, wishlist: Wishlist, course_slug) -> bool: course = CourseSelector.get_course_by_slug(slug=course_slug) return course in wishlist.courses.all()
def test_get_course_by_slug(course): test_course = CourseSelector.get_course_by_slug(slug=course.slug) assert test_course == course
def test_get_unpublished_course(course): course.published = False course.save() with pytest.raises(Course.DoesNotExist): CourseSelector.get_course_by_slug(slug=course.save)
def test_get_course_by_wrong_slug(course): with pytest.raises(Course.DoesNotExist): CourseSelector.get_course_by_slug(slug="somerandomslug")
def check_on_course_in_saved_for_later(cls, s_list: SavedForLater, course_slug: str) -> bool: course = CourseSelector.get_course_by_slug(slug=course_slug) return course in s_list.courses.all()
def remove_course_from_saved_for_later(cls, s_list: SavedForLater, course_slug: str) -> SavedForLater: course = CourseSelector.get_course_by_slug(slug=course_slug) if course in s_list.courses.all(): s_list.courses.remove(course) return s_list
def add_course_to_saved_for_later(cls, s_list: SavedForLater, course_slug: str) -> SavedForLater: course = CourseSelector.get_course_by_slug(slug=course_slug) if course not in s_list.courses.all(): s_list.courses.add(course) return s_list
def remove_course_from_wishlist(cls, wishlist: Wishlist, course_slug: str) -> Wishlist: course = CourseSelector.get_course_by_slug(slug=course_slug) if course in wishlist.courses.all(): wishlist.courses.remove(course) return wishlist
def add_course_to_cart(cls, cart: Cart, course_slug: str) -> Cart: course = CourseSelector.get_course_by_slug(slug=course_slug) if course not in cart.courses.all(): cart.courses.add(course) return cart
def remove_course_from_cart(cls, cart: Cart, course_slug: str) -> Cart: course = CourseSelector.get_course_by_slug(slug=course_slug) if course in cart.courses.all(): cart.courses.remove(course) return cart
def check_on_course_in_cart(cls, cart: Cart, course_slug: str) -> bool: course = CourseSelector.get_course_by_slug(slug=course_slug) return course in cart.courses.all()
def get_lectures_count(self, obj): try: return CourseSelector.get_course_lectures_count_by_course(course=obj) except CourseContent.DoesNotExist: logger.warning("Course content doesn't exist so lectures count field won't be set to serialized course object") return None
def add_course_to_wishlist(cls, wishlist: Wishlist, course_slug: str) -> Wishlist: course = CourseSelector.get_course_by_slug(slug=course_slug) if course not in wishlist.courses.all(): wishlist.courses.add(course) return wishlist