def init_db_and_populate(): """Creates the database tables.""" init_db() ''' TODO > aca deberia realizar una migracion para contar con datos ! ''' currency = Currency('peso', '$') account = Account(currency, 100, 'cuenta pesos') user1 = User('admin', 'admin@localhost') user1.accounts.append(account) db.add(user1) db.commit() currencyUSD = Currency('dollar', 'USD') accountUSD = Account(currencyUSD, 50, 'cuenta dolares') user2 = User('adminYankee', 'adminYankee@localhost') user2.accounts.append(accountUSD) db.add(user2) db.commit() print('Initialized the database.')
def do_transaction(origin, target, operation): response = None try: emitedTransaction = Transaction(operation, OperationType.DEBIT) operation.origin_account.transactions.append(emitedTransaction) operation.origin_account.apply_transaction(emitedTransaction) receivedTransaction = Transaction(operation, OperationType.CREDIT) operation.target_account.transactions.append(receivedTransaction) operation.target_account.apply_transaction(receivedTransaction) db.commit() return emitedTransaction.toJSON() except DecreaceAmountError as err: db.rollback() app.logger.error('An error occurred: {0}'.format(err.message)) raise DecreaceAmountError(operation) except Exception as err: db.rollback() app.logger.error('An error occurred: {0}'.format(err.message)) raise Error() finally: pass return response
def create_account(user_id): app.logger.info('creating a account for a user') response = None if not request.json: raise BadRequestError(request) else: try: currency = db.query(Currency).filter( Currency.id == request.json['currency']['id']).first() account = Account.fromJson(request.json, currency) user = db.query(User).filter(User.id == user_id).first() user.accounts.append(account) db.add(user) db.commit() response = account.toJSON() except Exception as err: db.rollback() app.logger.error('An error occurred: {0}'.format(err.message)) raise Error(request=request) return jsonify(response)
def create_user(): app.logger.info('creating a user') response = None if not request.json: raise BadRequestError(request) else: try: new_user = User(request.json['name'], request.json['email']) db.add(new_user) db.commit() response = new_user.toJSON() except Exception as err: db.rollback() app.logger.error('An error occurred: {0}'.format(err.message)) raise Error(request=request) return jsonify(response)
def update_currency(currency_id): app.logger.info('updating a currency') response = None if not request.json: raise BadRequestError(request) else: try: currency = db.query(Currency).filter( Currency.id == currency_id).first() currency.name = request.json['name'] currency.symbol = request.json['symbol'] currency.enable = request.json['enable'] db.commit() response = currency.toJSON() except Exception as err: db.rollback() app.logger.error('An error occurred: {0}'.format(err.message)) raise Error(request=request) return jsonify(response)
def update_user(user_id): app.logger.info('updating a user') response = None if not request.json: raise BadRequestError(request) else: try: user = db.query(User).filter(User.id == user_id).first() user.name = request.json['name'] user.email = request.json['email'] user.enable = request.json['enable'] #user.__dict__.update(request.json) #db.add(user) db.commit() response = user.toJSON() except Exception as err: db.rollback() app.logger.error('An error occurred: {0}'.format(err.message)) raise Error(request=request) return jsonify(response)
def update_account(user_id, account_id): app.logger.info('updating a account for a user') response = None if not request.json: raise BadRequestError(request) else: try: account = db.query(Account).filter( Account.id == account_id).first() account.name = request.json['name'] account.amount = request.json['amount'] account.enable = request.json['enable'] db.commit() response = account.toJSON() except Exception as err: db.rollback() app.logger.error('An error occurred: {0}'.format(err.message)) raise Error(request=request) return jsonify(response)