def approve(self, request, pk=None): """ Marks the Credit Trade as Approved Transfers the Credits Then, marks the Credit Trade as Completed """ credit_trade = self.get_object() credit_trade.trade_effective_date = datetime.date.today() previous_state = credit_trade if credit_trade.compliance_period_id is None: credit_trade.compliance_period_id = \ CreditTradeService.get_compliance_period_id(credit_trade) serializer = self.get_serializer(credit_trade, data=request.data) serializer.is_valid(raise_exception=True) completed_credit_trade = CreditTradeService.approve( credit_trade, request.user ) serializer = self.get_serializer(completed_credit_trade) CreditTradeService.dispatch_notifications(previous_state, completed_credit_trade) return Response(serializer.data, status=status.HTTP_200_OK)
def perform_update(self, serializer): previous_state = self.get_object() credit_trade = serializer.save() CreditTradeService.create_history(credit_trade, False) status_cancelled = CreditTradeStatus.objects.get(status="Cancelled") if serializer.data['status'] != status_cancelled.id: CreditTradeService.dispatch_notifications(previous_state, credit_trade)
def batch_process(self, request): """ Call the approve function on multiple Credit Trades """ status_approved = CreditTradeStatus.objects \ .get(status="Recorded") credit_trades = CreditTrade.objects.filter( status_id=status_approved.id).order_by('id') CreditTradeService.validate_credits(credit_trades) for credit_trade in credit_trades: credit_trade.update_user_id = request.user.id CreditTradeService.approve(credit_trade) CreditTradeService.dispatch_notifications(None, credit_trade) return Response( {"message": "Approved credit transactions have been processed."}, status=status.HTTP_200_OK)
def perform_create(self, serializer): credit_trade = serializer.save() CreditTradeService.create_history(credit_trade, True) CreditTradeService.dispatch_notifications(None, credit_trade)