def post(self, request, *args, **kwargs): if "year" not in request.POST or "month" not in request.POST or "day" not in request.POST or "method" not in request.POST: return HttpResponseBadRequest( json.dumps({"message": "missing parameter"})) method_pk = request.POST.get("method") try: new_date = date(int(request.POST.get("year")), int(request.POST.get("month")), int(request.POST.get("day"))) # 指定日以前のを全部チェック if "check_all" in request.POST and request.POST.get( "check_all") == "1": Data.filter_checkeds( Data.get_method_data(Data.get_range_data(None, new_date), method_pk), [False]).update(checked=True) except ValueError: return HttpResponseBadRequest( json.dumps({"message": "date format is invalid"})) try: # チェック日を更新 CheckedDate.set(method_pk, new_date) except CheckedDate.DoesNotExist: return HttpResponseBadRequest( json.dumps({"message": "method id is invalid"})) return HttpResponse()
def get(self, request, *args, **kwargs): # 全データ all_data = Data.get_all_data() # 支払い方法リスト methods = Method.list() # 支払い方法ごとの残高 methods_bd = [] for m in methods: d = Data.get_method_data(all_data, m.pk) # 銀行はチェック済みだけ if m.pk == Method.get_bank().pk: d = Data.get_checked_data(d) methods_bd.append({ 'pk': m.pk, 'name': m.name, 'balance': Data.get_income_sum(d) - Data.get_outgo_sum(d), 'year': CheckedDate.get(m.pk).date.year, 'month': CheckedDate.get(m.pk).date.month, 'day': CheckedDate.get(m.pk).date.day }) return HttpResponse(json.dumps(methods_bd))
def test_get_method_data(self): month_data = Data.get_month_data(2000, 1) data = Data.get_method_data(month_data, 3) expects = [ "PayPayチャージ", "立替分1" ] self._assert_list(data, expects)
def test_get_method_data_empty(self): month_data = Data.get_month_data(1999, 1) data = Data.get_method_data(month_data, 3) self.assertEqual(data.count(), 0)
def test_get_method_data_nothing(self): month_data = Data.get_month_data(2000, 1) data = Data.get_method_data(month_data, 100) self.assertEqual(data.count(), 0)
def get(self, request, *args, **kwargs): # validation if "year" in request.GET and "month" in request.GET: year = request.GET.get("year") month = request.GET.get("month") if not is_valid_date(year, month): return HttpResponseBadRequest("parameter error") else: return HttpResponseBadRequest("parameter error") # 全データ all_data = Data.get_all_data() # 今月のデータ monthly_data = Data.get_month_data(int(year), int(month)) # 支払い方法リスト methods = Method.list() # 支払い方法ごとの残高 methods_iob = [] methods_monthly_iob = [] for m in methods: d = Data.get_method_data(all_data, m.pk) methods_iob.append( InOutBalance(m.name, None, None, Data.get_income_sum(d) - Data.get_outgo_sum(d))) i = Data.get_income_sum(Data.get_method_data(monthly_data, m.pk)) o = Data.get_outgo_sum(Data.get_method_data(monthly_data, m.pk)) methods_monthly_iob.append(InOutBalance(m.name, i, o, None)) # 立替と貯金 monthly_temp_and_deposit = Data.get_temp_and_deposit_sum(monthly_data) # 通常データ monthly_normal_data = Data.get_normal_data(monthly_data) # 今月の収入 monthly_income = Data.get_income_sum( monthly_normal_data) - monthly_temp_and_deposit # 今月の支出 monthly_outgo = Data.get_outgo_sum( monthly_normal_data) - monthly_temp_and_deposit # 生活費 living_cost = Data.get_living_cost(monthly_data) # 変動費 variable_cost = Data.get_variable_cost(monthly_data) # 生活費目標額 living_cost_mark = SeveralCosts.get_living_cost_mark() # 内部移動以外 monthly_data_without_inmove = Data.filter_without_intra_move( monthly_data) context = { 'total_balance': Data.get_income_sum(all_data) - Data.get_outgo_sum(all_data), 'methods_iob': methods_iob, 'monthly_income': monthly_income, 'monthly_outgo': monthly_outgo, 'monthly_inout': monthly_income - monthly_outgo, 'living_cost': living_cost, 'variable_cost': variable_cost, 'living_remain': living_cost_mark - living_cost, 'variable_remain': monthly_income - max(SeveralCosts.get_living_cost_mark(), living_cost) - variable_cost, 'monthly_all_income': Data.get_income_sum(monthly_data_without_inmove), 'monthly_all_outgo': Data.get_outgo_sum(monthly_data_without_inmove), 'methods_monthly_iob': methods_monthly_iob, } return render(request, '_balance_statistic_mini.html', context)