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_post_guest(self): self.assertEqual(CheckedDate.get(2).date, date(2000, 1, 5)) unchecked_data = Data.get_unchecked_data(Data.get_all_data()) expects = [ '必需品1', 'スーパー', '計算外', '貯金', 'PayPayチャージ', '立替分1', '内部移動1', '内部移動2' ] self._assert_list(unchecked_data, expects) response = self.client.post(reverse('moneybook:checked_date'), { 'year': 2001, 'month': 2, 'day': 20, 'method': 2 }) self.assertEqual(response.status_code, 403) self.assertEqual(CheckedDate.get(2).date, date(2000, 1, 5)) unchecked_data = Data.get_unchecked_data(Data.get_all_data()) self._assert_list(unchecked_data, expects)
def test_post_missing_method(self): self.client.force_login(User.objects.create_user(self.username)) self.assertEqual(CheckedDate.get(2).date, date(2000, 1, 5)) unchecked_data = Data.get_unchecked_data(Data.get_all_data()) expects = [ '必需品1', 'スーパー', '計算外', '貯金', 'PayPayチャージ', '立替分1', '内部移動1', '内部移動2' ] self._assert_list(unchecked_data, expects) response = self.client.post(reverse('moneybook:checked_date'), { 'year': 2000, 'month': 1, 'day': 20 }) self.assertEqual(response.status_code, 400) self.assertEqual(CheckedDate.get(2).date, date(2000, 1, 5)) unchecked_data = Data.get_unchecked_data(Data.get_all_data()) self._assert_list(unchecked_data, expects)
def test_set(self): CheckedDate.set(1, date(2001, 1, 1)) self.assertEqual(CheckedDate.get(1).date, date(2001, 1, 1))
def test_get(self): self.assertEqual(CheckedDate.get(1).date, date(2000, 1, 2)) self.assertEqual(CheckedDate.get(2).date, date(2000, 1, 5)) self.assertEqual(CheckedDate.get(3).date, date(2000, 2, 2))