class OrderFactory(factory.DjangoModelFactory): class Meta: model = get_model('order', 'Order') exclude = ('basket', ) if hasattr(settings, 'OSCAR_INITIAL_ORDER_STATUS'): status = settings.OSCAR_INITIAL_ORDER_STATUS site_id = settings.SITE_ID number = factory.LazyAttribute(lambda o: '%d' % (100000 + o.basket.pk)) basket = factory.SubFactory('oscar.test.factories.BasketFactory') shipping_code = 'delivery' shipping_incl_tax = D('4.95') shipping_excl_tax = factory.LazyAttribute( lambda o: tax_subtract(o.shipping_incl_tax)) total_incl_tax = factory.LazyAttribute(lambda o: o.basket.total_incl_tax) total_excl_tax = factory.LazyAttribute(lambda o: o.basket.total_excl_tax) guest_email = factory.LazyAttribute(lambda o: ('*****@*****.**' % ( o.billing_address.first_name[0], o.billing_address.last_name)).lower()) shipping_address = factory.SubFactory(ShippingAddressFactory) billing_address = factory.SubFactory(BillingAddressFactory) @classmethod def _create(cls, target_class, *args, **kwargs): date_placed = kwargs.pop('date_placed', None) instance = super(OrderFactory, cls)._create(target_class, *args, **kwargs) if date_placed: instance.date_placed = date_placed return instance
class OrderFactory(factory.django.DjangoModelFactory): class Meta: model = get_model('order', 'Order') if hasattr(settings, 'OSCAR_INITIAL_ORDER_STATUS'): status = settings.OSCAR_INITIAL_ORDER_STATUS site_id = settings.SITE_ID number = factory.LazyAttribute(lambda o: '%d' % (100000 + o.basket.pk)) basket = factory.SubFactory('oscar.test.factories.BasketFactory') shipping_code = 'delivery' shipping_incl_tax = D('4.95') shipping_excl_tax = factory.LazyAttribute( lambda o: tax_subtract(o.shipping_incl_tax)) total_incl_tax = factory.LazyAttribute(lambda o: o.basket.total_incl_tax) total_excl_tax = factory.LazyAttribute(lambda o: o.basket.total_excl_tax) guest_email = factory.LazyAttribute(lambda o: ('*****@*****.**' % ( o.billing_address.first_name[0], o.billing_address.last_name)).lower()) shipping_address = factory.SubFactory(ShippingAddressFactory) billing_address = factory.SubFactory(BillingAddressFactory) @classmethod def _create(cls, target_class, *args, **kwargs): date_placed = kwargs.pop('date_placed', None) instance = super(OrderFactory, cls)._create(target_class, *args, **kwargs) if date_placed: instance.date_placed = date_placed return instance @factory.post_generation def create_line_models(obj, create, extracted, **kwargs): if not create: return if extracted: if not obj.basket.all_lines().exists(): product = factories.ProductFactory(stockrecords=None) factories.StockRecordFactory( product=product, price_currency=settings.OSCAR_DEFAULT_CURRENCY) obj.basket.add_product(product) for line in obj.basket.all_lines(): OrderCreator().create_line_models(obj, line)