コード例 #1
0
    def get_context_data(self, context):
        context = dict(context)
        product = context["shop_product"].product

        if product and is_product_valid_mode(product):
            product_rating = get_reviews_aggregation_for_product(product)

            if product_rating["reviews"]:
                rating = product_rating["rating"]
                reviews = product_rating["reviews"]
                (full_stars, empty_stars,
                 half_star) = get_stars_from_rating(rating)
                context.update({
                    "half_star":
                    half_star,
                    "full_stars":
                    full_stars,
                    "empty_stars":
                    empty_stars,
                    "reviews":
                    reviews,
                    "rating":
                    rating,
                    "would_recommend":
                    product_rating["would_recommend"],
                    "would_recommend_perc":
                    product_rating["would_recommend"] / reviews,
                    "show_recommenders":
                    self.config.get("show_recommenders", False),
                    "customer_ratings_title":
                    self.get_translated_value("customer_ratings_title")
                })

        return context
コード例 #2
0
def test_comments_view():
    shop = factories.get_default_shop()
    product = factories.create_product(
        "product", shop=shop, supplier=factories.get_default_supplier())
    product_no_reviews = factories.create_product(
        "product-no", shop=shop, supplier=factories.get_default_supplier())

    # create 15 reviews for the product, it should exist 3 pages of comments
    [create_random_review_for_product(shop, product) for _ in range(15)]

    for test in range(2):
        totals = get_reviews_aggregation_for_product(product)
        rendered = render_product_review_ratings(product)

        expected_rating = """
            <span class="rating">
                %0.1f
                <span class="sr-only">Rating&nbsp;</span>
            </span>
        """ % totals["rating"]
        assert expected_rating in rendered
        assert '%d reviews' % totals["reviews"] in rendered

        rendered = render_product_review_ratings(product_no_reviews)
        assert rendered == ""
コード例 #3
0
    def get_context_data(self, context):
        context = dict(context)
        product = context["shop_product"].product

        if product and product.mode in ACCEPTED_PRODUCT_MODES:
            product_rating = get_reviews_aggregation_for_product(product)

            if product_rating["reviews"]:
                rating = product_rating["rating"]
                reviews = product_rating["reviews"]
                full_stars = math.floor(rating)
                empty_stars = math.floor(5 - rating)
                half_star = (full_stars + empty_stars) < 5
                context.update({
                    "half_star":
                    half_star,
                    "full_stars":
                    int(full_stars),
                    "empty_stars":
                    int(empty_stars),
                    "reviews":
                    reviews,
                    "rating":
                    rating,
                    "would_recommend":
                    product_rating["would_recommend"],
                    "would_recommend_perc":
                    product_rating["would_recommend"] / reviews,
                    "show_recommenders":
                    self.config.get("show_recommenders", False),
                    "customer_ratings_title":
                    self.get_translated_value("customer_ratings_title")
                })

        return context
コード例 #4
0
def test_rejecting_all_reviews():
    shop = factories.get_default_shop()
    product = factories.create_product(
        "product", shop=shop, supplier=factories.get_default_supplier())
    create_random_review_for_product(shop, product, rating=5)

    totals = get_reviews_aggregation_for_product(product)
    assert totals["rating"] == 5
    assert totals["reviews"] == 1

    # Now let's reject all reviews for product and let's see that all cool
    for review in ProductReview.objects.filter(product=product):
        review.reject()

    totals = get_reviews_aggregation_for_product(product)
    assert totals["rating"] is None
    assert totals["reviews"] is None