Exemple #1
0
    def _compare_line_for_addition(self, current_line_data, product, supplier,
                                   shop, extra):
        """
        Compare raw line data for coalescing.

        That is, figure out whether the given raw line data is similar enough to product_id
        and extra to coalesce quantity additions.

        This is nice to override in a project-specific basket class.

        :type current_line_data: dict
        :type product: int
        :type extra: dict
        :return:
        """
        if current_line_data.get("line_id", "").startswith("custom_product"):
            return False
        if current_line_data.get("product_id") != product.id:
            return False
        if current_line_data.get("supplier_id") != supplier.id:
            return False
        if current_line_data.get("shop_id") != shop.id:
            return False

        if isinstance(
                extra, dict
        ):  # We have extra data, so compare it to that in this line
            if not compare_partial_dicts(
                    extra, current_line_data
            ):  # Extra data not similar? Okay then. :(
                return False
        return True
Exemple #2
0
def test_product_attributes_form():
    # Come up with a decent product...
    product = create_product("TestProduct", shop=get_default_shop())
    assert product.type.attributes.count()
    product.set_attribute_value("bogomips", 6400)
    product.set_attribute_value("genre", "Kauhu", "fi")
    product.set_attribute_value("genre", "Horror", "en")
    product.set_attribute_value("time_to_finish", 12.05)
    product.set_attribute_value("release_date", VOGONY_DATE)
    assert product.get_attribute_value("bogomips") == 6400

    # English is missing on purpose from the languages list; it'll still be available
    # for `genre` as it has an extant value.
    paf = ProductAttributesForm(product=product,
                                languages=("fi", "sv"),
                                default_language="sv")
    assert paf.languages[0] == "sv"

    assert compare_partial_dicts(
        paf.initial,
        {  # Check that things get loaded.
            "bogomips": 6400,
            "genre__fi": "Kauhu",
            "genre__en": "Horror",
            "release_date": VOGONY_DATE,
            "time_to_finish": Decimal("12.05"),
        },
    )
    form_data = get_form_data(paf)
    form_data.update(
        {  # Change, clear and add fields
            "genre__sv": "Skräck",
            "genre__en": "Terror",
            "bogomips": "",
            "release_date": "",
            "awesome": "True",
            "important_datetime": make_aware(datetime.datetime(2000, 1, 1, 1, 2, 3), utc),
        }
    )
    paf = ProductAttributesForm(product=product,
                                languages=("fi", "sv"),
                                default_language="sv",
                                data=form_data)
    paf.full_clean()
    assert not paf.errors
    paf.save()

    for identifier in ("bogomips", "release_date"):
        # Value should be gone
        assert not product.get_attribute_value(identifier)
        # ... and so should the actual product attribute object
        assert not ProductAttribute.objects.filter(
            attribute__identifier=identifier, product=product).exists()

    # Those other values got updated, right?
    assert product.get_attribute_value("genre", "en") == form_data["genre__en"]
    assert product.get_attribute_value("genre", "sv") == form_data["genre__sv"]
    assert product.get_attribute_value("awesome") is True
    assert str(product.get_attribute_value(
        "important_datetime")) == "2000-01-01 01:02:03+00:00"
Exemple #3
0
def test_product_options_attributes_form():
    """
    Test whether we can select attributes of type options
    """

    product_type = ProductType.objects.create(name="Default Product Type")
    options_attribute = create_attribute_with_options("attribute1",
                                                      ["A", "B", "C"], 1, 3)
    product_type.attributes.add(options_attribute)

    product = create_product("TestProduct",
                             shop=get_default_shop(),
                             type=product_type)
    choices = list(
        options_attribute.choices.all().order_by("translations__name"))
    option_a, option_b, option_c = choices
    product.set_attribute_value("attribute1", [option_c.pk])

    paf = ProductAttributesForm(product=product,
                                languages=("en"),
                                default_language="en")
    assert compare_partial_dicts(paf.initial, {"attribute1": [option_c.pk]})
    form_data = get_form_data(paf)
    form_data.update({"attribute1": [option_b.pk]})

    paf = ProductAttributesForm(product=product,
                                languages=("en"),
                                default_language="en",
                                data=form_data)
    paf.full_clean()
    assert not paf.errors
    paf.save()

    assert product.get_attribute_value("attribute1") == [option_b]
Exemple #4
0
    def _compare_line_for_addition(self, current_line_data, product, supplier, shop, extra):
        """
        Compare raw line data for coalescing.

        That is, figure out whether the given raw line data is similar enough to product_id
        and extra to coalesce quantity additions.

        This is nice to override in a project-specific basket class.

        :type current_line_data: dict
        :type product: int
        :type extra: dict
        :return:
        """
        if current_line_data.get("product_id") != product.id:
            return False
        if current_line_data.get("supplier_id") != supplier.id:
            return False
        if current_line_data.get("shop_id") != shop.id:
            return False

        if isinstance(extra, dict):  # We have extra data, so compare it to that in this line
            if not compare_partial_dicts(extra, current_line_data):  # Extra data not similar? Okay then. :(
                return False
        return True
def test_product_attributes_form():
    # Come up with a decent product...
    product = create_product("TestProduct", shop=get_default_shop())
    assert product.type.attributes.count()
    product.set_attribute_value("bogomips", 6400)
    product.set_attribute_value("genre", "Kauhu", "fi")
    product.set_attribute_value("genre", "Horror", "en")
    product.set_attribute_value("time_to_finish", 12.05)
    product.set_attribute_value("release_date", VOGONY_DATE)
    assert product.get_attribute_value("bogomips") == 6400

    # English is missing on purpose from the languages list; it'll still be available
    # for `genre` as it has an extant value.
    paf = ProductAttributesForm(product=product, languages=("fi", "sv"), default_language="sv")
    assert paf.languages[0] == "sv"

    assert compare_partial_dicts(paf.initial, {  # Check that things get loaded.
        "bogomips": 6400,
        "genre__fi": "Kauhu",
        "genre__en": "Horror",
        "release_date": VOGONY_DATE,
        "time_to_finish": Decimal("12.05")
    })
    form_data = get_form_data(paf)
    form_data.update({  # Change, clear and add fields
        "genre__sv": "Skräck",
        "genre__en": "Terror",
        "bogomips": "",
        "release_date": "",
        "awesome": "True",
        "important_datetime": make_aware(datetime.datetime(2000, 1, 1, 1, 2, 3), utc)
    })
    paf = ProductAttributesForm(product=product, languages=("fi", "sv"), default_language="sv", data=form_data)
    paf.full_clean()
    assert not paf.errors
    paf.save()

    for identifier in ("bogomips", "release_date"):
        # Value should be gone
        assert not product.get_attribute_value(identifier)
        # ... and so should the actual product attribute object
        assert not ProductAttribute.objects.filter(attribute__identifier=identifier, product=product).exists()

    # Those other values got updated, right?
    assert product.get_attribute_value("genre", "en") == form_data["genre__en"]
    assert product.get_attribute_value("genre", "sv") == form_data["genre__sv"]
    assert product.get_attribute_value("awesome") is True
    assert str(product.get_attribute_value("important_datetime")) == "2000-01-01 01:02:03+00:00"