def do(): with lockorder(pk=kwargs['order_pk']) as order: if order.status == constants.ORDER_LOCKED: order.status = constants.ORDER_INIT return [OrderSerializer(e).data for e in order.process()] else: raise UnlockFailureError("Can't unlock not locked order")
def history_notification(order): if order.status == constants.ORDER_COMPLETED: serializer = OrderSerializer(order) publishment = serializer.data topic = constants.TOPIC_HISTORY.format(pair=order.pair, type=order.type) client = get_crossbar_client() client.publish(topic, **publishment)
def do(): order = Order.lock(pk=kwargs['order_pk']) if order.status in [constants.ORDER_INIT, constants.ORDER_PENDING]: order.status = constants.ORDER_LOCKED order.save() return OrderSerializer(order).data else: raise LockFailureError("Can't lock order in status {}".format( order.status))
def do(): with lockorder(pk=kwargs['order_pk']) as order: if order.need_approve and order.status != constants.ORDER_APPROVED: order.status = constants.ORDER_APPROVED with lockorder(pk=order.link.pk) as opposite: if opposite.need_approve: if opposite.status == constants.ORDER_APPROVED: order.merge(opposite) else: order.merge(opposite) return OrderSerializer(order).data
def do(): with lockorder(pk=kwargs['order_pk']) as order: if order.status not in [ constants.ORDER_CANCELED, constants.ORDER_COMPLETED ]: if order.status in [ constants.ORDER_APPROVING, constants.ORDER_APPROVED ]: with lockorder(pk=order.link.pk) as opposite: opposite.status = constants.ORDER_PENDING order.unfreeze_money() order.status = constants.ORDER_CANCELED return OrderSerializer(order).data else: raise ValidationError("Order already canceled")
def do(): data = kwargs['data'] with lockorder(pk=kwargs['order_pk']) as order: if order.status in [ constants.ORDER_INIT, constants.ORDER_PENDING, constants.ORDER_LOCKED ]: order.unfreeze_money() if data.get('price') is None: data['price'] = str(order.price) data = calculate_total_or_amount(data) order.price = decimal.Decimal(data.get('price')) order.amount = decimal.Decimal(data.get('amount')) order.total = decimal.Decimal(data.get('total')) order.freeze_money() else: raise UpdateFailureError( "Can't update orders in status '{}'".format(order.status)) return OrderSerializer(order).data
def create_order(self, *args, **kwargs): data = kwargs['data'] user_pk = kwargs['user_pk'] serializer = OrderSerializer(data=data) serializer.is_valid(raise_exception=True) order = serializer.object(owner_id=user_pk) if order.total < constants.ORDER_MIN_TOTAL_AMOUNT: raise ValidationError("Total amount lower than {}".format( constants.ORDER_MIN_TOTAL_AMOUNT)) try: with publishment.atomic(): with transaction.atomic(): with lockorder(order=order): order.freeze_money() return [OrderSerializer(e).data for e in order.process()] except OperationalError: raise self.retry(countdown=constants.CELERY_RETRY_COUNTDOWN)