def transfer_money(self): """ 0= action, 1=target, 2=amount """ try: _, target, amount = self.content.split(" ") source = self._get_active_account(self.from_number) target = self._get_active_account(self._format_account(target)) result = self._update_balance(source, target, int(amount)) if result == self.ZERO: return JR( set_message( "Não pode transfer 0 Mt. Use um valor superior a 0")) if result == self.NO_BALANCE: return JR( set_message( "Sem saldo suficiente para proceder com a transferência" )) if result == self.OK: return JR( set_message( "Transferência de {} Mt para {} feita com sucesso". format(amount, target.contact))) except Account.DoesNotExist: return JR(set_message("Usuário não existe no sistema"))
def cancel_account(self): try: account = self._get_active_account(self.from_number) self._set_account_state(account, False) return JR(set_message("Usuário cancelado no sistema")) except Account.DoesNotExist: return JR(set_message("Usuário não existe no sistema"))
def check_balance(self): try: balance = self._get_active_account(self.from_number).balance return JR( set_message("O seu saldo é de {} Mt".format( intcomma(balance)))) except Account.DoesNotExist: return JR(set_message("Usuário não existe no sistema"))
def create_account(self): try: _, created = Account.objects.get_or_create( contact=self.from_number) if created: return JR(set_message("Registado com sucesso no sistema")) return JR(set_message("Usuário já existe no sistema")) except: # Notify admin by email return JR( set_message( "Problema temporário no sistema. Tente mais tarde"))
def post(self, request): webhook_secret = os.environ["WEBHOOK_SECRET"] if request.POST.get('secret') != webhook_secret: return HR("Invalid webhook secret", 'text/plain', 403) if request.POST.get('event') == 'incoming_message': from_number = request.POST.get('from_number') content = request.POST.get('content') if not content: return JR(set_message(EMPTY_MSG)) for action, regex in actions: if re.match(regex, content.lower()): executor = AccountExecutor(from_number, content) return getattr(executor, action)() return JR(set_message(INVALID_FORMAT)) else: return HR("Event not implemented", 'text/plain', 403)
def jr_response(message: str, error: str, statud_code: int) -> JsonResponse: response = JR({'message': message, 'error': error}) response.status_code = statud_code return response
def not_found_message(model: Instance) -> JsonResponse: response = JR({'error': model.non_found_message}) response.status_code = 400 return response