def handle(self, *args, **options): """ Update the status. """ # At -v2 SOAP requests are outputted. do_all = options['all'] only_status = options['status'] verbosity = int(options['verbosity']) # Apply verbosity logging.getLogger('oscar_docdata.interface').setLevel('WARNING' if verbosity < 2 else 'DEBUG') logging.getLogger('suds.transport').setLevel('INFO' if verbosity < 3 else 'DEBUG') qs = DocdataOrder.objects.active_merchants() facade = get_facade() if do_all: orders = qs.all() order_count = orders.count() self.stdout.write("There are %i orders." % order_count) if only_status: orders = orders.filter(status=only_status) if args: raise CommandError("No order numbers have to be provided for --all") else: # First get all orders, check them. orders = [] for order_number in options["oscar_order_number"]: try: order = qs.get(merchant_order_id=order_number) except DocdataOrder.DoesNotExist: self.stderr.write(u"- Order does not exist: {0}\n".format(order_number)) continue else: if only_status and order.status != only_status: self.stderr.write(u"- Order {0} does not have status {1}, but {2}\n".format(order_number, only_status, order.status)) continue orders.append(order) self.stdout.write("Collect %i orders." % len(orders)) for order in orders: self.stdout.write(u"- Checking {0}\n".format(order.merchant_order_id)) with transaction.atomic(): # First request the order at docdata, avoid expiring an order which missed an update (very unlikely) old_status = order.status try: facade.update_order(order) except DocdataStatusError as e: self.stderr.write(u"{0}\n".format(e)) continue if order.status == old_status: self.stderr.write(u" Order {0} status unchanged, remained: {1}".format(order.merchant_order_id, order.status)) else: self.stdout.write(u" Order {0} status changed from {1} to {2}".format(order.merchant_order_id, old_status, order.status))
def handle(self, *args, **options): """ Update the status. """ is_dry_run = options.get('dry-run', False) expire_status_choices = (DocdataOrder.STATUS_NEW, DocdataOrder.STATUS_IN_PROGRESS) # At -v2 SOAP requests are outputted. verbosity = int(options['verbosity']) logging.getLogger('suds.transport').setLevel('INFO' if verbosity < 2 else 'DEBUG') qs = DocdataOrder.objects.active_merchants() \ .filter(status__in=expire_status_choices) \ .filter(created__lt=(now() - timedelta(days=21))) # 3 weeks, based on manual testing. order_count = qs.count() self.stdout.write("Collect %i orders." % order_count) if order_count == 0: return facade = get_facade() if is_dry_run: self.stdout.write(u"Expiring orders (DRY-RUN):") else: self.stdout.write(u"Expiring orders:") for order in qs.iterator(): # Will loop through all one by one, so signals can be properly fired: self.stdout.write(u"- {0}\t(created {1:%Y-%m-%d}, still {2})".format(order.merchant_order_id, order.created, order.status)) # Will update old_status = order.status order.status = DocdataOrder.STATUS_EXPIRED if not is_dry_run: with transaction.atomic(): # First request the order at docdata, avoid expiring an order which missed an update (very unlikely) facade.update_order(order) if order.status not in expire_status_choices: if order.status == DocdataOrder.STATUS_EXPIRED: self.stdout.write(u" Updated order {0} via status API, detected expired state".format(order.merchant_order_id)) else: self.stderr.write(u" Skipping order {0}, status changed to: {1}".format(order.merchant_order_id, order.status)) else: # More efficient SQL DocdataOrder.objects.filter(id=order.id).update(status=DocdataOrder.STATUS_EXPIRED) try: # Make sure Oscar is updated, and the signal is sent. facade.order_status_changed(order, old_status, order.status) except Exception as e: self.stderr.write(u"Failed to update order {0}: {1}".format(order.id, e)) DocdataOrder.objects.filter(id=order.id).update(status=old_status)
def get(self, request, *args, **kwargs): """ Fetch the latest status from docdata. """ self.object = self.get_object() # Perform update. try: facade = get_facade() facade.update_order(self.object) except DocdataStatusError as e: messages.error(request, e.value) else: messages.info(request, u"Order status is updated") return HttpResponseRedirect(reverse('docdata-order-detail', args=(self.object.pk,)))
def delete(self, request, *args, **kwargs): """ Cancel the object in Docdata """ self.object = self.get_object() # Perform cancel try: facade = get_facade() facade.cancel_order(self.object) except DocdataCancelError as e: messages.error(request, e.value) else: messages.info(request, _(u'The order "{order_id}" was cancelled successfully.').format(order_id=self.object.merchant_order_id)) return HttpResponseRedirect(reverse('docdata-order-detail', args=(self.object.pk,)))
def get(self, request, *args, **kwargs): """ Fetch the latest status from docdata. """ self.object = self.get_object() # Perform update. try: facade = get_facade() facade.update_order(self.object) except DocdataStatusError as e: messages.error(request, e.value) else: messages.info(request, u"Order status is updated") return HttpResponseRedirect( reverse('docdata-order-detail', args=(self.object.pk, )))
def delete(self, request, *args, **kwargs): """ Cancel the object in Docdata """ self.object = self.get_object() # Perform cancel try: facade = get_facade() facade.cancel_order(self.object) except DocdataCancelError as e: messages.error(request, e.value) else: messages.info( request, _(u'The order "{order_id}" was cancelled successfully.'). format(order_id=self.object.merchant_order_id)) return HttpResponseRedirect( reverse('docdata-order-detail', args=(self.object.pk, )))
def get_facade(self): if self.facade_class is not None: return self.facade_class() else: # Making sure the object is lazy-imported. return get_facade()