示例#1
0
def product_group_copy(group_id):
    group = ProductGroup.query.get_or_404(group_id)
    form = CopyProductGroupForm()

    if group.children:
        # No recursive copying
        abort(404)

    if request.method != "POST":
        form.name.data = group.name + " (copy)"

    if group.capacity_max is None:
        del form.capacity_max_required

    else:
        del form.capacity_max

    if form.validate_on_submit():
        capacity_max = None
        if group.capacity_max is not None:
            capacity_max = form.capacity_max_required.data
            group.capacity_max -= capacity_max

        new_group = ProductGroup(
            type=group.type,
            name=form.name.data,
            capacity_max=capacity_max,
            expires=form.expires.data,
        )
        for product in group.products:
            new_product = Product(
                name=product.name,
                display_name=product.display_name,
                description=product.description,
            )
            new_group.products.append(new_product)
            for pt in product.price_tiers:
                if not pt.active and not form.include_inactive.data:
                    continue

                new_pt = PriceTier(name=pt.name,
                                   personal_limit=pt.personal_limit,
                                   active=pt.active)
                new_product.price_tiers.append(new_pt)
                for price in pt.prices:
                    new_price = Price(price.currency, price.value)
                    new_pt.prices.append(new_price)

        new_group.parent = group.parent
        db.session.add(new_group)
        db.session.commit()
        flash("ProductGroup copied")
        return redirect(
            url_for(".product_group_details", group_id=new_group.id))

    return render_template("admin/products/product-group-copy.html",
                           group=group,
                           form=form)
def test_validate_capacity_max(db, parent_group):
    group_template = "group{}"
    group_name = group_template.format(random_string(8))
    # Create a product group without a parent so validate_capacity_max returns early
    group = ProductGroup(type="test")
    assert group.name is None
    assert group.id is None

    # Now add a parent
    group.parent = parent_group

    # This should call validate_capacity_max, which may flush the session, which we don't want
    group.capacity_max = 5

    # If that was OK, we can continue
    group.name = group_name
    db.session.flush()
    assert group.id is not None
示例#3
0
def test_validate_capacity_max(db, parent_group):
    group_template = 'group{}'
    group_name = group_template.format(random_string(8))
    # Create a product group without a parent so validate_capacity_max returns early
    group = ProductGroup(type='test')
    assert group.name is None
    assert group.id is None

    # Now add a parent
    group.parent = parent_group

    # This should call validate_capacity_max, which may flush the session, which we don't want
    group.capacity_max = 5

    # If that was OK, we can continue
    group.name = group_name
    db.session.flush()
    assert group.id is not None