Пример #1
0
def get_from_item(cls, item, qty, coupon_codes=[]):
    """
    Returns a list of (discount_policy, discount_coupon) tuples
    applicable for an item, given the quantity of line items and coupons if any.
    """
    automatic_discounts = item.discount_policies.filter(DiscountPolicy.discount_type == DISCOUNT_TYPE.AUTOMATIC,
        DiscountPolicy.item_quantity_min <= qty).all()
    policies = [(discount, None) for discount in automatic_discounts]
    if not coupon_codes:
        return policies
    else:
        coupon_policies = item.discount_policies.filter(DiscountPolicy.discount_type == DISCOUNT_TYPE.COUPON).all()
        coupon_policy_ids = [cp.id for cp in coupon_policies]
        for coupon_code in coupon_codes:
            if DiscountPolicy.is_signed_code_format(coupon_code):
                policy = DiscountPolicy.get_from_signed_code(coupon_code)
                if policy and policy.id in coupon_policy_ids:
                    coupon = DiscountCoupon.query.filter_by(discount_policy=policy, code=coupon_code).one_or_none()
                    if not coupon:
                        coupon = DiscountCoupon(discount_policy=policy, code=coupon_code, usage_limit=policy.bulk_coupon_usage_limit, used_count=0)
                        db.session.add(coupon)
                else:
                    coupon = None
            else:
                coupon = DiscountCoupon.query.filter(DiscountCoupon.discount_policy_id.in_(coupon_policy_ids), DiscountCoupon.code == coupon_code).one_or_none()
            if coupon and coupon.usage_limit > coupon.used_count:
                policies.append((coupon.discount_policy, coupon))

    return policies
    def get_valid_discounts(self, line_items, coupons):
        """
        Returns all the applicable discounts given the quantity of items
        selected and any coupons.
        """
        if not line_items:
            return None

        item = Item.query.get(line_items[0].item_id)
        return DiscountPolicy.get_from_item(item, len(line_items), coupons)
    def get_valid_discounts(self, line_items, coupons):
        """
        Returns all the applicable discounts given the quantity of items
        selected and any coupons.
        """
        if not line_items:
            return None

        item = Item.query.get(line_items[0].item_id)
        return DiscountPolicy.get_from_item(item, len(line_items), coupons)
Пример #4
0
def get_from_item(cls, item, qty, coupon_codes=[]):
    """
    Returns a list of (discount_policy, discount_coupon) tuples
    applicable for an item, given the quantity of line items and coupons if any.
    """
    automatic_discounts = item.discount_policies.filter(
        DiscountPolicy.discount_type == DISCOUNT_TYPE.AUTOMATIC,
        DiscountPolicy.item_quantity_min <= qty).all()
    policies = [(discount, None) for discount in automatic_discounts]
    if not coupon_codes:
        return policies
    else:
        coupon_policies = item.discount_policies.filter(
            DiscountPolicy.discount_type == DISCOUNT_TYPE.COUPON).all()
        coupon_policy_ids = [cp.id for cp in coupon_policies]
        for coupon_code in coupon_codes:
            coupons = []
            if DiscountPolicy.is_signed_code_format(coupon_code):
                policy = DiscountPolicy.get_from_signed_code(coupon_code)
                if policy and policy.id in coupon_policy_ids:
                    coupon = DiscountCoupon.query.filter_by(
                        discount_policy=policy,
                        code=coupon_code).one_or_none()
                    if not coupon:
                        coupon = DiscountCoupon(
                            discount_policy=policy,
                            code=coupon_code,
                            usage_limit=policy.bulk_coupon_usage_limit,
                            used_count=0)
                        db.session.add(coupon)
                    coupons.append(coupon)
            else:
                coupons = DiscountCoupon.query.filter(
                    DiscountCoupon.discount_policy_id.in_(coupon_policy_ids),
                    DiscountCoupon.code == coupon_code).all()

            for coupon in coupons:
                if coupon.usage_limit > coupon.used_count:
                    policies.append((coupon.discount_policy, coupon))
    return policies
Пример #5
0
    def get_valid_discounts(self, line_items, coupons):
        """
        Returns all the applicable discounts given the quantity of items
        selected and any coupons.
        """
        if not line_items:
            return []

        item = Item.query.get(line_items[0].item_id)
        if not item.is_available and not item.is_cancellable():
            # item unavailable, no discounts
            return []

        return DiscountPolicy.get_from_item(item, len(line_items), coupons)
Пример #6
0
    def get_valid_discounts(self, line_items, coupons):
        """
        Returns all the applicable discounts given the quantity of items
        selected and any coupons.
        """
        if not line_items:
            return []

        item = Item.query.get(line_items[0].item_id)
        if not item.is_available and not item.is_cancellable():
            # item unavailable, no discounts
            return []

        return DiscountPolicy.get_from_item(item, len(line_items), coupons)
Пример #7
0
def all_data(db_session):
    user = User(userid="U3_JesHfQ2OUmdihAXaAGQ", email="*****@*****.**")
    db_session.add(user)
    db_session.commit()

    one_month_from_now = date.today() + relativedelta(months=+1)

    rootconf = Organization(
        title='Rootconf',
        userid="U3_JesHfQ2OUmdihAXaAGQ",
        status=0,
        contact_email='*****@*****.**',
        details={
            'service_tax_no':
            'xx',
            'address':
            '<h2 class="company-name">XYZ</h2> <p>Bangalore - 560034</p> <p>India</p>',
            'cin':
            '1234',
            'pan':
            'abc',
            'website':
            'https://www.test.com',
            'refund_policy':
            '<p>We offer full refund.</p>',
            'support_email':
            '*****@*****.**',
            'ticket_faq':
            '<p>To cancel your ticket, please mail <a href="mailto:[email protected]">[email protected]</a> with your receipt number.</p>',
        },
    )
    db_session.add(rootconf)
    db_session.commit()

    rc2016 = ItemCollection(title='2016', organization=rootconf)
    db_session.add(rc2016)
    db_session.commit()

    category_conference = Category(title='Conference',
                                   item_collection=rc2016,
                                   seq=1)
    db_session.add(category_conference)
    category_workshop = Category(title='Workshop',
                                 item_collection=rc2016,
                                 seq=2)
    db_session.add(category_workshop)
    category_merch = Category(title='Merchandise',
                              item_collection=rc2016,
                              seq=3)
    db_session.add(category_merch)
    db_session.commit()

    with db_session.no_autoflush:
        conf_ticket = Item(
            title='Conference ticket',
            description=
            '<p><i class="fa fa-calendar"></i>14 - 15 April 2016</p><p><i class="fa fa-map-marker ticket-venue"></i>MLR Convention Center, JP Nagar</p><p>This ticket gets you access to rootconf conference on 14th and 15th April 2016.</p>',
            item_collection=rc2016,
            category=Category.query.filter_by(name='conference').first(),
            quantity_total=1000,
        )
        rc2016.items.append(conf_ticket)
        db_session.commit()

        expired_ticket = Item(
            title='Expired ticket',
            description=
            '<p><i class="fa fa-calendar"></i>14 - 15 April 2016</p><p><i class="fa fa-map-marker ticket-venue"></i>MLR Convention Center, JP Nagar</p><p>This ticket gets you access to rootconf conference on 14th and 15th April 2016.</p>',
            item_collection=rc2016,
            category=Category.query.filter_by(name='conference').first(),
            quantity_total=1000,
        )
        rc2016.items.append(expired_ticket)
        db_session.commit()

        price = Price(
            item=conf_ticket,
            title='Super Early Geek',
            start_at=utcnow(),
            end_at=one_month_from_now,
            amount=3500,
        )
        db_session.add(price)
        db_session.commit()

        single_day_conf_ticket = Item(
            title='Single Day',
            description=
            '<p><i class="fa fa-calendar"></i>14 April 2016</p><p><i class="fa fa-map-marker ticket-venue"></i>MLR Convention Center, JP Nagar</p><p>This ticket gets you access to rootconf conference on 14th April 2016.</p>',
            item_collection=rc2016,
            category=Category.query.filter_by(name='conference').first(),
            quantity_total=1000,
        )
        rc2016.items.append(single_day_conf_ticket)
        db_session.commit()

        single_day_price = Price(
            item=single_day_conf_ticket,
            title='Single Day',
            start_at=utcnow(),
            end_at=one_month_from_now,
            amount=2500,
        )
        db_session.add(single_day_price)
        db_session.commit()

        tshirt = Item(
            title='T-shirt',
            description='Rootconf',
            item_collection=rc2016,
            category=Category.query.filter_by(name='merchandise').first(),
            quantity_total=1000,
        )
        rc2016.items.append(tshirt)
        db_session.commit()

        tshirt_price = Price(
            item=tshirt,
            title='T-shirt',
            start_at=utcnow(),
            end_at=one_month_from_now,
            amount=500,
        )
        db_session.add(tshirt_price)
        db_session.commit()

        dns_workshop = Item(
            title='DNSSEC workshop',
            description=
            '<p><i class="fa fa-calendar"></i>12 April 2016</p><p><i class="fa fa-map-marker ticket-venue"></i>TERI, Domlur</p><p>This ticket gets you access to DNSSEC workshop 12th April 2016.</p>',
            item_collection=rc2016,
            category=Category.query.filter_by(name='workshop').first(),
            quantity_total=1000,
        )
        rc2016.items.append(dns_workshop)
        db_session.commit()

        dns_workshop_price = Price(
            item=dns_workshop,
            title='DNSSEC workshop early',
            start_at=utcnow(),
            end_at=one_month_from_now,
            amount=2500,
        )
        db_session.add(dns_workshop_price)
        db_session.commit()

        policy = DiscountPolicy(
            title='10% discount on rootconf',
            item_quantity_min=10,
            percentage=10,
            organization=rootconf,
        )
        policy.items.append(conf_ticket)
        db_session.add(policy)
        db_session.commit()

        tshirt_policy = DiscountPolicy(
            title='5% discount on 5 t-shirts',
            item_quantity_min=5,
            percentage=5,
            organization=rootconf,
        )
        tshirt_policy.items.append(tshirt)
        db_session.add(tshirt_policy)
        db_session.commit()

        discount_coupon1 = DiscountPolicy(
            title='15% discount for coupon code with STU',
            item_quantity_min=1,
            percentage=15,
            organization=rootconf,
            discount_type=DISCOUNT_TYPE.COUPON,
        )
        discount_coupon1.items.append(conf_ticket)
        db_session.add(discount_coupon1)
        db_session.commit()

        coupon1 = DiscountCoupon(code='coupon1',
                                 discount_policy=discount_coupon1)
        db_session.add(coupon1)
        db_session.commit()

        discount_coupon_expired_ticket = DiscountPolicy(
            title='15% discount for expired ticket',
            item_quantity_min=1,
            percentage=15,
            organization=rootconf,
            discount_type=DISCOUNT_TYPE.COUPON,
        )
        discount_coupon_expired_ticket.items.append(expired_ticket)
        db_session.add(discount_coupon_expired_ticket)
        db_session.commit()

        discount_coupon_expired_ticket_coupon = DiscountCoupon(
            code='couponex', discount_policy=discount_coupon_expired_ticket)
        db_session.add(discount_coupon_expired_ticket_coupon)
        db_session.commit()

        discount_coupon2 = DiscountPolicy(
            title='100% discount',
            item_quantity_min=1,
            percentage=100,
            organization=rootconf,
            discount_type=DISCOUNT_TYPE.COUPON,
        )
        discount_coupon2.items.append(conf_ticket)
        db_session.add(discount_coupon1)
        db_session.commit()

        coupon2 = DiscountCoupon(code='coupon2',
                                 discount_policy=discount_coupon2)
        db_session.add(coupon2)
        db_session.commit()

        coupon3 = DiscountCoupon(code='coupon3',
                                 discount_policy=discount_coupon2)
        db_session.add(coupon3)
        db_session.commit()

        forever_early_geek = DiscountPolicy(
            title='Forever Early Geek',
            item_quantity_min=1,
            is_price_based=True,
            discount_type=DISCOUNT_TYPE.COUPON,
            organization=rootconf,
        )
        forever_early_geek.items.append(conf_ticket)
        db_session.add(forever_early_geek)
        db_session.commit()

        forever_coupon = DiscountCoupon(code='forever',
                                        discount_policy=forever_early_geek)
        db_session.add(forever_coupon)
        db_session.commit()

        noprice_discount = DiscountPolicy(
            title='noprice',
            item_quantity_min=1,
            is_price_based=True,
            discount_type=DISCOUNT_TYPE.COUPON,
            organization=rootconf,
        )
        noprice_discount.items.append(conf_ticket)
        db_session.add(noprice_discount)
        db_session.commit()

        noprice_coupon = DiscountCoupon(code='noprice',
                                        discount_policy=noprice_discount)
        db_session.add(noprice_coupon)
        db_session.commit()

        forever_unlimited_coupon = DiscountCoupon(
            code='unlimited',
            discount_policy=forever_early_geek,
            usage_limit=500)
        db_session.add(forever_unlimited_coupon)
        db_session.commit()

        discount_price = Price(
            item=conf_ticket,
            discount_policy=forever_early_geek,
            title='Forever Early Geek',
            start_at=utcnow(),
            end_at=one_month_from_now,
            amount=3400,
        )
        db_session.add(discount_price)
        db_session.commit()

        unlimited_geek = DiscountPolicy(
            title='Unlimited Geek',
            item_quantity_min=1,
            discount_type=DISCOUNT_TYPE.COUPON,
            percentage=10,
            organization=rootconf,
        )
        unlimited_geek.items.append(conf_ticket)
        db_session.add(unlimited_geek)
        db_session.commit()

        unlimited_coupon = DiscountCoupon(code='unlimited',
                                          discount_policy=unlimited_geek,
                                          usage_limit=500)
        db_session.add(unlimited_coupon)
        db_session.commit()

        zero_discount = DiscountPolicy(
            title='Zero Discount',
            item_quantity_min=1,
            is_price_based=True,
            discount_type=DISCOUNT_TYPE.COUPON,
            organization=rootconf,
        )
        zero_discount.items.append(conf_ticket)
        db_session.add(zero_discount)
        db_session.commit()

        zero_coupon = DiscountCoupon(code='zerodi',
                                     discount_policy=zero_discount)
        db_session.add(zero_coupon)
        db_session.commit()

        zero_discount_price = Price(
            item=conf_ticket,
            discount_policy=zero_discount,
            title='Zero Discount',
            start_at=utcnow(),
            end_at=one_month_from_now,
            amount=3600,
        )
        db_session.add(zero_discount_price)
        db_session.commit()

        bulk = DiscountPolicy.make_bulk(
            'signed',
            organization=rootconf,
            title='Signed',
            percentage=10,
            bulk_coupon_usage_limit=2,
        )
        bulk.items.append(conf_ticket)
        db_session.add(bulk)
        db_session.commit()

    return SimpleNamespace(**locals())
Пример #8
0
def init_data():
    db.drop_all()
    db.create_all()

    user = User(userid=u"U3_JesHfQ2OUmdihAXaAGQ", email=u"*****@*****.**")
    db.session.add(user)
    db.session.commit()

    one_month_from_now = date.today() + relativedelta(months=+1)

    rootconf = Organization(title=u'Rootconf', userid=u"U3_JesHfQ2OUmdihAXaAGQ",
        status=0, contact_email=u'*****@*****.**',
        details={'service_tax_no': 'xx', 'address': u'<h2 class="company-name">XYZ</h2> <p>Bangalore - 560034</p> <p>India</p>', 'cin': u'1234', 'pan': u'abc', 'website': u'https://www.test.com', 'refund_policy': u'<p>We offer full refund.</p>', 'support_email': '*****@*****.**', 'ticket_faq': '<p>To cancel your ticket, please mail <a href="mailto:[email protected]">[email protected]</a> with your receipt number.</p>'})
    db.session.add(rootconf)
    db.session.commit()

    rc2016 = ItemCollection(title=u'2016', organization=rootconf)
    db.session.add(rc2016)
    db.session.commit()

    category_conference = Category(title=u'Conference', item_collection=rc2016, seq=1)
    db.session.add(category_conference)
    category_workshop = Category(title=u'Workshop', item_collection=rc2016, seq=2)
    db.session.add(category_workshop)
    category_merch = Category(title=u'Merchandise', item_collection=rc2016, seq=3)
    db.session.add(category_merch)
    db.session.commit()

    # import IPython; IPython.embed()
    with db.session.no_autoflush:
        conf_ticket = Item(title=u'Conference ticket', description=u'<p><i class="fa fa-calendar"></i>14 - 15 April 2016</p><p><i class="fa fa-map-marker ticket-venue"></i>MLR Convention Center, JP Nagar</p><p>This ticket gets you access to rootconf conference on 14th and 15th April 2016.</p>', item_collection=rc2016, category=Category.query.filter_by(name=u'conference').first(), quantity_total=1000)
        rc2016.items.append(conf_ticket)
        db.session.commit()

        expired_ticket = Item(title=u'Expired ticket', description=u'<p><i class="fa fa-calendar"></i>14 - 15 April 2016</p><p><i class="fa fa-map-marker ticket-venue"></i>MLR Convention Center, JP Nagar</p><p>This ticket gets you access to rootconf conference on 14th and 15th April 2016.</p>', item_collection=rc2016, category=Category.query.filter_by(name=u'conference').first(), quantity_total=1000)
        rc2016.items.append(expired_ticket)
        db.session.commit()

        price = Price(item=conf_ticket, title=u'Super Early Geek', start_at=datetime.utcnow(), end_at=one_month_from_now, amount=3500)
        db.session.add(price)
        db.session.commit()

        single_day_conf_ticket = Item(title=u'Single Day', description=u'<p><i class="fa fa-calendar"></i>14 April 2016</p><p><i class="fa fa-map-marker ticket-venue"></i>MLR Convention Center, JP Nagar</p><p>This ticket gets you access to rootconf conference on 14th April 2016.</p>', item_collection=rc2016, category=Category.query.filter_by(name=u'conference').first(), quantity_total=1000)
        rc2016.items.append(single_day_conf_ticket)
        db.session.commit()

        single_day_price = Price(item=single_day_conf_ticket, title=u'Single Day', start_at=datetime.utcnow(), end_at=one_month_from_now, amount=2500)
        db.session.add(single_day_price)
        db.session.commit()

        tshirt = Item(title=u'T-shirt', description=u'Rootconf', item_collection=rc2016, category=Category.query.filter_by(name=u'merchandise').first(), quantity_total=1000)
        rc2016.items.append(tshirt)
        db.session.commit()

        tshirt_price = Price(item=tshirt, title=u'T-shirt', start_at=datetime.utcnow(), end_at=one_month_from_now, amount=500)
        db.session.add(tshirt_price)
        db.session.commit()

        dns_workshop = Item(title=u'DNSSEC workshop', description=u'<p><i class="fa fa-calendar"></i>12 April 2016</p><p><i class="fa fa-map-marker ticket-venue"></i>TERI, Domlur</p><p>This ticket gets you access to DNSSEC workshop 12th April 2016.</p>', item_collection=rc2016, category=Category.query.filter_by(name=u'workshop').first(), quantity_total=1000)
        rc2016.items.append(dns_workshop)
        db.session.commit()

        dns_workshop_price = Price(item=dns_workshop, title=u'DNSSEC workshop early', start_at=datetime.utcnow(), end_at=one_month_from_now, amount=2500)
        db.session.add(dns_workshop_price)
        db.session.commit()

        policy = DiscountPolicy(title=u'10% discount on rootconf', item_quantity_min=10, percentage=10, organization=rootconf)
        policy.items.append(conf_ticket)
        db.session.add(policy)
        db.session.commit()

        tshirt_policy = DiscountPolicy(title=u'5% discount on 5 t-shirts', item_quantity_min=5, percentage=5, organization=rootconf)
        tshirt_policy.items.append(tshirt)
        db.session.add(tshirt_policy)
        db.session.commit()

        discount_coupon1 = DiscountPolicy(title=u'15% discount for coupon code with STU', item_quantity_min=1, percentage=15, organization=rootconf, discount_type=DISCOUNT_TYPE.COUPON)
        discount_coupon1.items.append(conf_ticket)
        db.session.add(discount_coupon1)
        db.session.commit()

        coupon1 = DiscountCoupon(code=u'coupon1', discount_policy=discount_coupon1)
        db.session.add(coupon1)
        db.session.commit()

        discount_coupon_expired_ticket = DiscountPolicy(title=u'15% discount for expired ticket', item_quantity_min=1, percentage=15, organization=rootconf, discount_type=DISCOUNT_TYPE.COUPON)
        discount_coupon_expired_ticket.items.append(expired_ticket)
        db.session.add(discount_coupon_expired_ticket)
        db.session.commit()

        discount_coupon_expired_ticket_coupon = DiscountCoupon(code=u'couponex', discount_policy=discount_coupon_expired_ticket)
        db.session.add(discount_coupon_expired_ticket_coupon)
        db.session.commit()

        discount_coupon2 = DiscountPolicy(title=u'100% discount', item_quantity_min=1, percentage=100, organization=rootconf, discount_type=DISCOUNT_TYPE.COUPON)
        discount_coupon2.items.append(conf_ticket)
        db.session.add(discount_coupon1)
        db.session.commit()

        coupon2 = DiscountCoupon(code=u'coupon2', discount_policy=discount_coupon2)
        db.session.add(coupon2)
        db.session.commit()

        coupon3 = DiscountCoupon(code=u'coupon3', discount_policy=discount_coupon2)
        db.session.add(coupon3)
        db.session.commit()

        forever_early_geek = DiscountPolicy(title=u'Forever Early Geek',
            item_quantity_min=1,
            is_price_based=True,
            discount_type=DISCOUNT_TYPE.COUPON,
            organization=rootconf)
        forever_early_geek.items.append(conf_ticket)
        db.session.add(forever_early_geek)
        db.session.commit()

        forever_coupon = DiscountCoupon(code=u'forever', discount_policy=forever_early_geek)
        db.session.add(forever_coupon)
        db.session.commit()

        noprice_discount = DiscountPolicy(title=u'noprice',
            item_quantity_min=1,
            is_price_based=True,
            discount_type=DISCOUNT_TYPE.COUPON,
            organization=rootconf)
        noprice_discount.items.append(conf_ticket)
        db.session.add(noprice_discount)
        db.session.commit()

        noprice_coupon = DiscountCoupon(code=u'noprice', discount_policy=noprice_discount)
        db.session.add(noprice_coupon)
        db.session.commit()

        forever_unlimited_coupon = DiscountCoupon(code=u'unlimited', discount_policy=forever_early_geek,
            usage_limit=500)
        db.session.add(forever_unlimited_coupon)
        db.session.commit()

        discount_price = Price(item=conf_ticket,
            discount_policy=forever_early_geek, title=u'Forever Early Geek',
            start_at=datetime.utcnow(), end_at=one_month_from_now, amount=3400)
        db.session.add(discount_price)
        db.session.commit()

        unlimited_geek = DiscountPolicy(title=u'Unlimited Geek',
            item_quantity_min=1,
            discount_type=DISCOUNT_TYPE.COUPON,
            percentage=10,
            organization=rootconf)
        unlimited_geek.items.append(conf_ticket)
        db.session.add(unlimited_geek)
        db.session.commit()

        unlimited_coupon = DiscountCoupon(code=u'unlimited', discount_policy=unlimited_geek,
            usage_limit=500)
        db.session.add(unlimited_coupon)
        db.session.commit()

        zero_discount = DiscountPolicy(title=u'Zero Discount',
            item_quantity_min=1,
            is_price_based=True,
            discount_type=DISCOUNT_TYPE.COUPON,
            organization=rootconf)
        zero_discount.items.append(conf_ticket)
        db.session.add(zero_discount)
        db.session.commit()

        zero_coupon = DiscountCoupon(code=u'zerodi', discount_policy=zero_discount)
        db.session.add(zero_coupon)
        db.session.commit()

        zero_discount_price = Price(item=conf_ticket,
            discount_policy=zero_discount, title=u'Zero Discount',
            start_at=datetime.utcnow(), end_at=one_month_from_now, amount=3600)
        db.session.add(zero_discount_price)
        db.session.commit()

        bulk = DiscountPolicy.make_bulk(u'signed', organization=rootconf, title=u'Signed', percentage=10, bulk_coupon_usage_limit=2)
        bulk.items.append(conf_ticket)
        db.session.add(bulk)
        db.session.commit()
Пример #9
0
def admin_new_discount_policy(organization):
    discount_policy = DiscountPolicy(organization=organization)
    discount_policy_form = DiscountPolicyForm(model=DiscountPolicy)
    discount_policy_form.populate_obj(discount_policy)
    discount_policy_error_msg = _(
        u"The discount could not be created. Please rectify the indicated issues"
    )

    if discount_policy.is_price_based:
        discount_policy_form = PriceBasedDiscountPolicyForm(
            model=DiscountPolicy, parent=discount_policy.organization)
        with db.session.no_autoflush:
            if not discount_policy_form.validate_on_submit():
                return api_error(message=discount_policy_error_msg,
                                 status_code=400,
                                 errors=discount_policy_form.errors)
            discount_policy_form.populate_obj(discount_policy)
            discount_policy.make_name()
            discount_price_form = DiscountPriceForm(model=Price,
                                                    parent=discount_policy)
            if not discount_price_form.validate_on_submit():
                return api_error(message=_(
                    u"There was an issue with the price. Please rectify the indicated issues"
                ),
                                 status_code=400,
                                 errors=discount_price_form.errors)
            discount_price = Price(discount_policy=discount_policy)
            discount_price_form.populate_obj(discount_price)
            discount_price.make_name()
        db.session.add(discount_price)
        discount_policy.items.append(discount_price.item)
    elif discount_policy.is_coupon:
        discount_policy_form = CouponBasedDiscountPolicyForm(
            model=DiscountPolicy, parent=discount_policy.organization)
        with db.session.no_autoflush:
            if not discount_policy_form.validate_on_submit():
                return api_error(message=discount_policy_error_msg,
                                 status_code=400,
                                 errors=discount_policy_form.errors)
        discount_policy_form.populate_obj(discount_policy)
        discount_policy.make_name()
    elif discount_policy.is_automatic:
        discount_policy_form = AutomaticDiscountPolicyForm(
            model=DiscountPolicy, parent=discount_policy.organization)
        with db.session.no_autoflush:
            if not discount_policy_form.validate_on_submit():
                return api_error(message=discount_policy_error_msg,
                                 status_code=400,
                                 errors=discount_policy_form.errors)
        discount_policy_form.populate_obj(discount_policy)
        discount_policy.make_name()
    else:
        return api_error(message=_(u"Incorrect discount type"),
                         status_code=400)

    db.session.add(discount_policy)
    db.session.commit()
    return api_success(
        result={'discount_policy': jsonify_discount_policy(discount_policy)},
        doc=_(u"New discount policy created"),
        status_code=201)