from flask import Flask, jsonify, request from cashman.model.expense import Expense, ExpenseSchema from cashman.model.income import Income, IncomeSchema from cashman.model.transaction_type import TransactionType app = Flask(__name__) transactions = [ Income('Salary', 5000), Income('Dividends', 200), Expense('pizza', 50), Expense('Rock Concert', 100) ] @app.route('/incomes' def get_incomes(): """ 1. manyってなに? 2. filterの挙動を忘れた """ schema = IncomeSchema(many=True) incomes = schema.dump( filter(lambda t: t.type == TransactionType.INCOME, transactions ) ) return jsonify(incomes.data)
from flask import Flask, jsonify, request from cashman.model.expense import Expense, ExpenseSchema from cashman.model.income import Income, IncomeSchema from cashman.model.transaction_type import TransactionType app = Flask(__name__) transactions = [ Income(1, 'Salary', 5000), Income(2, 'Dividends', 200), Expense(1, 'pizza', 50), Expense(2, 'Rock Concert', 100) ] @app.route('/incomes') #GET receitas def get_incomes(): schema = IncomeSchema(many=True) incomes = schema.dump( filter(lambda t: t.type == TransactionType.INCOME, transactions)) return jsonify(incomes.data) @app.route('/incomes', methods=['POST']) #POST receitas def add_income(): income = IncomeSchema().load(request.get_json()) #JSON -> Dados transactions.append(income.data) #Insere dados return "", 204 # Codigo de Status HTTP 204 - The server has successfully fulfilled the request
#main file for RESTful example from flask import Flask, jsonify, request from cashman.model.expense import Expense, ExpenseSchema from cashman.model.income import Income, IncomeSchema from cashman.model.transaction_type import TransactionType app = Flask(__name__) transactions = [ Income('Salary', 5000), Income('Dividends', 200), Expense('Pizza', 50), Expense('Concert', 100) ] #GET for getting dcitoionary @app.route("/incomes") def get_income(): schema = IncomeSchema(many=True) incomes = schema.dump( filter(lambda x: x.type == TransactionType.INCOME, transactions)) return jsonify(incomes.data) #POST to append list of incomes @app.route("/incomes", methods=['POST']) def add_income():
''' Serve income/expense endpoints ''' from flask import Flask, jsonify, request from cashman.model.expense import Expense, ExpenseSchema from cashman.model.income import Income, IncomeSchema from cashman.model.transaction_type import TransactionType app = Flask(__name__) # Dict to store key value entity pairs transactions = [ Income('Salary', 5000), Income('Dividends', 200), Expense('pizza', 50), Expense('Show', 100) ] @app.route('/incomes') def get_incomes(): # retrieve income transactions schema = IncomeSchema(many=True) incomes = schema.dump( filter(lambda t: t.type == TransactionType.INCOME, transactions)) return jsonify(incomes.data) @app.route('/incomes', methods=['POST']) def add_income():