Commit c55253cb by Vedran Karacic

Change order-basket-lines queries to order-lines.

parent d2749b4a
...@@ -495,7 +495,7 @@ class CouponSerializer(ProductPaymentInfoMixin, serializers.ModelSerializer): ...@@ -495,7 +495,7 @@ class CouponSerializer(ProductPaymentInfoMixin, serializers.ModelSerializer):
return offer.condition.range.catalog_query return offer.condition.range.catalog_query
def get_client(self, obj): def get_client(self, obj):
return Invoice.objects.get(order__basket__lines__product=obj).business_client.name return Invoice.objects.get(order__lines__product=obj).business_client.name
def get_code(self, obj): def get_code(self, obj):
if self.retrieve_quantity(obj) == 1: if self.retrieve_quantity(obj) == 1:
...@@ -544,7 +544,7 @@ class CouponSerializer(ProductPaymentInfoMixin, serializers.ModelSerializer): ...@@ -544,7 +544,7 @@ class CouponSerializer(ProductPaymentInfoMixin, serializers.ModelSerializer):
Currently only invoices are supported, in the event of adding another Currently only invoices are supported, in the event of adding another
payment processor append it to the response dictionary. payment processor append it to the response dictionary.
""" """
invoice = Invoice.objects.filter(order__basket__lines__product=obj).first() invoice = Invoice.objects.filter(order__lines__product=obj).first()
response = {'Invoice': InvoiceSerializer(invoice).data} response = {'Invoice': InvoiceSerializer(invoice).data}
return response return response
......
...@@ -507,7 +507,7 @@ class CouponViewSetFunctionalTest(CouponMixin, CourseCatalogTestMixin, CourseCat ...@@ -507,7 +507,7 @@ class CouponViewSetFunctionalTest(CouponMixin, CourseCatalogTestMixin, CourseCat
self.assertEqual(invoice.business_client.name, client_username) self.assertEqual(invoice.business_client.name, client_username)
def test_update_invoice_data(self): def test_update_invoice_data(self):
invoice = Invoice.objects.get(order__basket__lines__product=self.coupon) invoice = Invoice.objects.get(order__lines__product=self.coupon)
self.assertEqual(invoice.discount_type, Invoice.PERCENTAGE) self.assertEqual(invoice.discount_type, Invoice.PERCENTAGE)
CouponViewSet().update_invoice_data( CouponViewSet().update_invoice_data(
coupon=self.coupon, coupon=self.coupon,
...@@ -516,7 +516,7 @@ class CouponViewSetFunctionalTest(CouponMixin, CourseCatalogTestMixin, CourseCat ...@@ -516,7 +516,7 @@ class CouponViewSetFunctionalTest(CouponMixin, CourseCatalogTestMixin, CourseCat
} }
) )
invoice = Invoice.objects.get(order__basket__lines__product=self.coupon) invoice = Invoice.objects.get(order__lines__product=self.coupon)
self.assertEqual(invoice.discount_type, Invoice.FIXED) self.assertEqual(invoice.discount_type, Invoice.FIXED)
@ddt.data('audit', 'honor') @ddt.data('audit', 'honor')
...@@ -593,7 +593,7 @@ class CouponViewSetFunctionalTest(CouponMixin, CourseCatalogTestMixin, CourseCat ...@@ -593,7 +593,7 @@ class CouponViewSetFunctionalTest(CouponMixin, CourseCatalogTestMixin, CourseCat
""" Verify an invoice is created with the proper data. """ """ Verify an invoice is created with the proper data. """
self.update_prepaid_invoice_data() self.update_prepaid_invoice_data()
response = self.get_response_json('POST', COUPONS_LINK, data=self.data) response = self.get_response_json('POST', COUPONS_LINK, data=self.data)
invoice = Invoice.objects.get(order__basket__lines__product__id=response['coupon_id']) invoice = Invoice.objects.get(order__lines__product__id=response['coupon_id'])
self.assertEqual(invoice.type, self.data['invoice_type']) self.assertEqual(invoice.type, self.data['invoice_type'])
self.assertEqual(invoice.number, self.data['invoice_number']) self.assertEqual(invoice.number, self.data['invoice_number'])
self.assertEqual(invoice.payment_date.isoformat(), self.data['invoice_payment_date']) self.assertEqual(invoice.payment_date.isoformat(), self.data['invoice_payment_date'])
......
...@@ -418,7 +418,7 @@ class CouponViewSet(EdxOrderPlacementMixin, viewsets.ModelViewSet): ...@@ -418,7 +418,7 @@ class CouponViewSet(EdxOrderPlacementMixin, viewsets.ModelViewSet):
invoice_data = self.create_update_data_dict(data=data, fields=Invoice.UPDATEABLE_INVOICE_FIELDS) invoice_data = self.create_update_data_dict(data=data, fields=Invoice.UPDATEABLE_INVOICE_FIELDS)
if invoice_data: if invoice_data:
Invoice.objects.filter(order__basket__lines__product=coupon).update(**invoice_data) Invoice.objects.filter(order__lines__product=coupon).update(**invoice_data)
def destroy(self, request, pk): # pylint: disable=unused-argument def destroy(self, request, pk): # pylint: disable=unused-argument
try: try:
......
import logging
from django.core.management import BaseCommand
from ecommerce.extensions.catalogue.models import Product
from ecommerce.invoice.models import Invoice
logger = logging.getLogger(__name__)
class Command(BaseCommand):
"""
Squash duplicate invoices for coupon orders.
When we moved from re-using a coupon some of them had already more than one invoice,
and we are not having that! Monogamy rulez here, yerr damn hippies!
"""
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
def handle(self, *args, **options):
for coupon in Product.objects.filter(product_class__name='Coupon'):
qs = Invoice.objects.filter(order__basket__lines__product=coupon).order_by('created')
if qs.count() > 1:
qs.exclude(pk=qs.first().id).delete()
logger.info('Deleted douplicate invoices of coupon %s', coupon.id)
from django.core.management import call_command
from oscar.test import factories from oscar.test import factories
from ecommerce.invoice.models import Invoice from ecommerce.invoice.models import Invoice
...@@ -26,36 +25,3 @@ class InvoiceTests(TestCase): ...@@ -26,36 +25,3 @@ class InvoiceTests(TestCase):
def test_total(self): def test_total(self):
"""Test to check invoice total""" """Test to check invoice total"""
self.assertEqual(self.basket.order.total_incl_tax, self.invoice.total) self.assertEqual(self.basket.order.total_incl_tax, self.invoice.total)
class InvoiceCommandTests(TestCase):
"""Tests for the squash_duplicate_invoices command."""
def setUp(self):
super(InvoiceCommandTests, self).setUp()
coupon_pc = factories.ProductClassFactory(name='Coupon')
self.product = factories.ProductFactory(product_class=coupon_pc)
self.basket = factories.BasketFactory()
self.basket.add_product(self.product, 1)
self.order = factories.create_order(basket=self.basket)
self.invoice = Invoice.objects.create(order=self.order)
def assert_unique_invoice(self, product, invoice):
"""Helper method for asserting there is only one invoice for given product."""
invoice_qs = Invoice.objects.filter(order__basket__lines__product=product)
self.assertEqual(invoice_qs.count(), 1)
self.assertEqual(invoice_qs.first(), invoice)
def test_squashing_invoices(self):
"""Verify after calling the command the duplicate invoices are squashed."""
Invoice.objects.create(order=self.order)
self.assertEqual(Invoice.objects.filter(order__basket__lines__product=self.product).count(), 2)
call_command('squash_duplicate_invoices')
self.assert_unique_invoice(self.product, self.invoice)
def test_not_squashing_invoices(self):
"""Verify the non-duplicate invoices are left the same."""
self.assertEqual(Invoice.objects.filter(order__basket__lines__product=self.product).count(), 1)
call_command('squash_duplicate_invoices')
self.assert_unique_invoice(self.product, self.invoice)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment