-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6151 from edx/adam/add-defunct-states-for-cart
Adam/add defunct states for cart
- Loading branch information
Showing
8 changed files
with
265 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
44 changes: 44 additions & 0 deletions
44
lms/djangoapps/shoppingcart/management/commands/retire_order.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
Script for retiring order that went through cybersource but weren't | ||
marked as "purchased" in the db | ||
""" | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
from shoppingcart.models import Order | ||
from shoppingcart.exceptions import UnexpectedOrderItemStatus, InvalidStatusToRetire | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Retire orders that went through cybersource but weren't updated | ||
appropriately in the db | ||
""" | ||
help = """ | ||
Retire orders that went through cybersource but weren't updated appropriately in the db. | ||
Takes a file of orders to be retired, one order per line | ||
""" | ||
|
||
def handle(self, *args, **options): | ||
"Execute the command" | ||
if len(args) != 1: | ||
raise CommandError("retire_order requires one argument: <orders file>") | ||
|
||
with open(args[0]) as orders_file: | ||
order_ids = [int(line.strip()) for line in orders_file.readlines()] | ||
|
||
orders = Order.objects.filter(id__in=order_ids) | ||
|
||
for order in orders: | ||
old_status = order.status | ||
try: | ||
order.retire() | ||
except (UnexpectedOrderItemStatus, InvalidStatusToRetire) as err: | ||
print "Did not retire order {order}: {message}".format( | ||
order=order.id, message=err.message | ||
) | ||
else: | ||
print "retired order {order_id} from status {old_status} to status {new_status}".format( | ||
order_id=order.id, | ||
old_status=old_status, | ||
new_status=order.status, | ||
) |
Empty file.
76 changes: 76 additions & 0 deletions
76
lms/djangoapps/shoppingcart/management/tests/test_retire_order.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""Tests for the retire_order command""" | ||
|
||
from tempfile import NamedTemporaryFile | ||
from django.core.management import call_command | ||
|
||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase | ||
from xmodule.modulestore.tests.factories import CourseFactory | ||
from shoppingcart.models import Order, CertificateItem | ||
from student.tests.factories import UserFactory | ||
|
||
|
||
class TestRetireOrder(ModuleStoreTestCase): | ||
"""Test the retire_order command""" | ||
def setUp(self): | ||
course = CourseFactory.create() | ||
self.course_key = course.id | ||
|
||
# set up test carts | ||
self.cart, __ = self._create_cart() | ||
|
||
self.paying, __ = self._create_cart() | ||
self.paying.start_purchase() | ||
|
||
self.already_defunct_cart, __ = self._create_cart() | ||
self.already_defunct_cart.retire() | ||
|
||
self.purchased, self.purchased_item = self._create_cart() | ||
self.purchased.status = "purchased" | ||
self.purchased.save() | ||
self.purchased_item.status = "purchased" | ||
self.purchased.save() | ||
|
||
def test_retire_order(self): | ||
"""Test the retire_order command""" | ||
nonexistent_id = max(order.id for order in Order.objects.all()) + 1 | ||
order_ids = [ | ||
self.cart.id, | ||
self.paying.id, | ||
self.already_defunct_cart.id, | ||
self.purchased.id, | ||
nonexistent_id | ||
] | ||
|
||
self._create_tempfile_and_call_command(order_ids) | ||
|
||
self.assertEqual( | ||
Order.objects.get(id=self.cart.id).status, "defunct-cart" | ||
) | ||
self.assertEqual( | ||
Order.objects.get(id=self.paying.id).status, "defunct-paying" | ||
) | ||
self.assertEqual( | ||
Order.objects.get(id=self.already_defunct_cart.id).status, | ||
"defunct-cart" | ||
) | ||
self.assertEqual( | ||
Order.objects.get(id=self.purchased.id).status, "purchased" | ||
) | ||
|
||
def _create_tempfile_and_call_command(self, order_ids): | ||
""" | ||
Takes a list of order_ids, writes them to a tempfile, and then runs the | ||
"retire_order" command on the tempfile | ||
""" | ||
with NamedTemporaryFile() as temp: | ||
temp.write("\n".join(str(order_id) for order_id in order_ids)) | ||
temp.seek(0) | ||
call_command('retire_order', temp.name) | ||
|
||
def _create_cart(self): | ||
"""Creates a cart and adds a CertificateItem to it""" | ||
cart = Order.get_cart_for_user(UserFactory.create()) | ||
item = CertificateItem.add_to_order( | ||
cart, self.course_key, 10, 'honor', currency='usd' | ||
) | ||
return cart, item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters