def get_incomes(): op = 0 conn = pg.connect(dbname='postgres', user='******', password='******', host='192.168.99.102') cur = conn.cursor() cur.execute("SELECT * FROM byte_store") trs = cur.fetchall() trs = [pickle.loads(bytes(i[0])) for i in trs] schema = IncomeSchema(many=True) incomes = schema.dump( filter(lambda t: t.type == TransactionType.INCOME, trs) ) resp = json.dumps(incomes.data, sort_keys = True, indent = 4, separators = (',', ': ')) return current_app.response_class(resp, mimetype="application/json")
def add_income(): conn = pg.connect(dbname='postgres', user='******', password='******', host='192.168.99.102') cur = conn.cursor() income = IncomeSchema().load(request.get_json()) sample = pickle.dumps(income.data) cur.execute("INSERT INTO byte_store (trans) VALUES (%s);", (pg.Binary(sample),)) conn.commit() # transactions.append(income.data) return '', 204
def add_income(): # Load an instance of Income based on the JSON data sent by the user, ie, # deserializes the request JSON POST datastructure into an Income object using its schema income = IncomeSchema().load(request.get_json()) # Add the new object to the list of transactions (see variable above) transactions.append(income.data) return "", 204
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) @app.route('/incomes', methods=['POST']) def add_income(): """ 1. POSTは待ちの姿勢のmethodsか?それとも新しいデータを追加する為のmethodsか? 2. request.get_json()で新しい入力データを読み込むのか? 3. 204ってなに?なんで空文字を返すの? """ income = IncomeSchema().load(request.get_json())
def add_income(): income = IncomeSchema().load(request.get_json()) transactions.append(income.data) return "", 204
def get_incomes(): schema = IncomeSchema(many=True) incomes = schema.dump( filter(lambda t: t.type == TransactionType.INCOME, transactions) ) return jsonify(incomes.data)
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
def add_income(): # Add a new item to transaction type income income = IncomeSchema().load(request.get_json()) transactions.append(income.data) return '', 204
def add_income(): income = IncomeSchema().make_income(request.get_json()) transactions.append(income) return "", 204