def post(self, request, *args, **kwargs): written_bank_data = Data.get_checked_data( Data.get_bank_data(Data.get_all_data())) bank_sum = 0 bb = BankBalance.get_all() cc = CreditCheckedDate.get_all() # フォーマットチェック try: for b in bb: key = "bank-" + str(b.pk) if key in request.POST: int(request.POST.get(key)) for c in cc: key = "credit-" + str(c.pk) if key in request.POST: int(request.POST.get(key)) except ValueError: return HttpResponseBadRequest( json.dumps({"message": "invalid parameter"})) # 更新と計算 for b in bb: key = "bank-" + str(b.pk) if key in request.POST: value = int(request.POST.get(key)) BankBalance.set(b.pk, value) bank_sum += BankBalance.get_price(b.pk) for c in cc: key = "credit-" + str(c.pk) if key in request.POST: value = int(request.POST.get(key)) CreditCheckedDate.set_price(c.pk, value) bank_sum -= CreditCheckedDate.get_price(c.pk) return HttpResponse( json.dumps({ "balance": Data.get_income_sum(written_bank_data) - Data.get_outgo_sum(written_bank_data) - bank_sum }))
def test_set(self): BankBalance.set(2, 10001) data = BankBalance.get_all() self.assertEqual(data[0].name, "三井住友") self.assertEqual(data[0].price, 10001)