def post(self): data = request.get_json() try: user = User.objects.get(id=ObjectId(get_jwt_identity())) data['value'] = round(float(data['value']), 6) if data['origin'] == data['target']: return errorHandler(code=400, message="Las cuentas deben ser distintas") if data['value'] > 0: origin = Account.objects.get(id=ObjectId(data['origin'])) target = Account.objects.get(id=ObjectId(data['target'])) data['originBalance'] = origin.getBalance() if data['originBalance'] >= data['value']: data['originBalance'] = round( data['originBalance'] - data['value'], 6) data['targetBalance'] = round( target.getBalance() + data['value'], 6) transaction = Transaction(**data) transaction = transaction.save() origin.transactions.append(transaction) target.transactions.append(transaction) origin.save() target.save() else: return errorHandler(code=400, message="Saldo insuficiente") else: return errorHandler(code=400, message="Error de precision") except Exception as e: # catch *all* exceptions return errorHandler(code=400, debug=e) return {'status': 'Success', 'user': user.toJson()}, 201
def get(self): try: user = User.objects.get(id=ObjectId(get_jwt_identity())) if user: return user.toJson() else: return errorHandler(code=404) except Exception as e: # catch *all* exceptions return errorHandler(code=400, debug=e)
def get(self, account): try: account = Account.objects(id=ObjectId(account)).first() if account: return account.toJson() else: return errorHandler(code=404) except Exception as e: # catch *all* exceptions return errorHandler(code=400, debug=e)
def post(self): data = request.get_json() try: user = User.objects.get(username=data['username']) except Exception as e: # catch *all* exceptions return errorHandler(code=400, message="Las credenciales no son válidas!", debug=e) if user and user.verifyPassword(data['password']): ret = {'access_token': create_access_token(identity=str(user.id))} return ret, 200 return errorHandler(code=400, message="Las credenciales no son válidas!")
def get(self): try: res = [] for user in User.objects: res.append(user.toJson()) return res except Exception as e: # catch *all* exceptions return errorHandler(code=400, debug=e)
def get(self): try: res = [] for account in Account.objects: res.append(account.toMinJson()) return res except Exception as e: # catch *all* exceptions return errorHandler(code=400, debug=e)
def post(self): try: user = User.objects.get(id=ObjectId(get_jwt_identity())) account = Account() account.user = user account = account.save() user.accounts.append(account) user.save() except Exception as e: # catch *all* exceptions return errorHandler(code=400, debug=e) return {'status': 'Success'}, 201
def post(self): data = request.get_json() initialFunds = round(float(data['funds']), 6) del data['funds'] try: user = User(**data) user.hashPassword(user.password) user.save() # Se crea una cuenta automaticamente con fines practicos para el testing # y porque no llego con el CRUD de cuentas :) sorry # Además se le asigna una fondo inicial para agilizar el testing account = Account() account.description = "Cuanta de prueba - " + user.username account.user = user account = account.save() initialTransaction = Transaction() initialTransaction.origin = account initialTransaction.target = account initialTransaction.originBalance = initialFunds initialTransaction.targetBalance = initialFunds initialTransaction.value = initialFunds initialTransaction.save() account.transactions.append(initialTransaction) account = account.save() user.accounts.append(account) user.save() except Exception as e: # catch *all* exceptions strError = str(e) message = '' if strError.find("username_1 dup") > -1: message = "El nombre de usuario ya está en uso" elif strError.find("email_1 dup") > -1: message = "El email ya está en uso" return errorHandler(code=400, message=message, debug=e) return {'status': 'Success'}, 201