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 test_get_range_data(self): start = date(2000, 1, 1) end = date(2000, 1, 10) data = Data.get_range_data(start, end) expects = [ "給与", "コンビニ", "その他1", "必需品1", "必需品2", "現金収入" ] self._assert_list(data, expects)
def get(self, request, *args, **kwargs): now = datetime.now() context = { 'app_name': settings.APP_NAME, 'username': request.user, 'draw_graph': False } # パラメータ取得 if 'start_year' not in request.GET or 'start_month' not in request.GET or \ 'end_year' not in request.GET or 'end_month' not in request.GET: # パラメータが無いときはデフォルト値で去年~今月を入れる start_year = now.year - 1 start_month = 1 end_year = now.year end_month = now.month else: try: start_year = int(request.GET.get('start_year')) start_month = int(request.GET.get('start_month')) end_year = int(request.GET.get('end_year')) end_month = int(request.GET.get('end_month')) except ValueError: return render(request, 'period_balances.html', context) i_date = date(start_year, start_month, 1) end_date = date(end_year, end_month, 1) period_balances = [] while True: last_day = i_date + relativedelta(months=1) - timedelta(days=1) d = Data.get_range_data(None, last_day) period_balances.append( LabelValue(datetime.strftime(i_date, '%Y-%m'), Data.get_income_sum(d) - Data.get_outgo_sum(d))) if i_date == end_date: break i_date = i_date + relativedelta(months=1) context['draw_graph'] = True context['period_balances'] = period_balances context['start_year'] = start_year context['start_month'] = start_month context['end_year'] = end_year context['end_month'] = end_month return render(request, 'period_balances.html', context)
def test_get_range_data_nothing(self): start = date(1999, 1, 1) end = date(1999, 12, 30) data = Data.get_range_data(start, end) self.assertEqual(data.count(), 0)
def get(self, request, *args, **kwargs): year = kwargs['year'] month_list = list(range(1, 13)) # 月ごとのデータ monthly_context = [] for i_month in month_list: tomonth_context = {} tomonth_context['label'] = i_month monthly_data = Data.get_month_data(year, i_month) monthly_normal_data = Data.get_normal_data(monthly_data) next_month_first = date(year, i_month, 1) + relativedelta(months=1) # 収支 td = Data.get_temp_and_deposit_sum(monthly_data) tomonth_context['income'] = Data.get_income_sum( monthly_normal_data) - td tomonth_context['outgo'] = Data.get_outgo_sum( monthly_normal_data) - td tomonth_context['balance'] = tomonth_context[ 'income'] - tomonth_context['outgo'] # 給与 tomonth_context['salary'] = Data.get_income_sum( Data.get_keyword_data(monthly_data, "給与")) # 生活費 tomonth_context['living_cost'] = Data.get_living_cost(monthly_data) # 食費 tomonth_context['food_cost'] = Data.get_food_costs(monthly_data) # 電気代 tomonth_context['electricity_cost'] = Data.get_outgo_sum( Data.get_keyword_data(monthly_data, "電気代")) # ガス代 tomonth_context['gus_cost'] = Data.get_outgo_sum( Data.get_keyword_data(monthly_data, "ガス代")) # 水道代 w = Data.get_outgo_sum(Data.get_keyword_data(monthly_data, "水道代")) / 2 if (w == 0): d = Data.get_month_data(next_month_first.year, next_month_first.month) w = Data.get_outgo_sum(Data.get_keyword_data(d, "水道代")) / 2 tomonth_context['water_cost'] = w tomonth_context['infra_cost'] = tomonth_context['electricity_cost'] + \ tomonth_context['gus_cost'] + tomonth_context['water_cost'] # 全収支 monthly_data_without_in_move = Data.filter_without_intra_move( monthly_data) t = Data.get_temp_sum(monthly_data) tomonth_context['all_income'] = Data.get_income_sum( monthly_data_without_in_move) - t tomonth_context['all_outgo'] = Data.get_outgo_sum( monthly_data_without_in_move) - t tomonth_context['all_balance'] = tomonth_context[ 'all_income'] - tomonth_context['all_outgo'] # 途中残高 last_day = next_month_first - timedelta(days=1) rd = Data.get_range_data(None, last_day) tomonth_context['period_balance'] = Data.get_income_sum( rd) - Data.get_outgo_sum(rd) monthly_context.append(tomonth_context) context = { 'app_name': settings.APP_NAME, 'username': request.user, 'year': year, 'monthly_context': monthly_context } return render(request, 'statistics.html', context)
def get(self, request, *args, **kwargs): context = { 'app_name': settings.APP_NAME, 'username': request.user, 'directions': Direction.list(), 'methods': Method.list(), 'unused_methods': Method.un_used_list(), 'first_categories': Category.first_list(), 'latter_categories': Category.latter_list(), 'temps': { 0: "No", 1: "Yes" }, 'checkeds': { 0: "No", 1: "Yes" }, } # 入力値を維持するための変数 query_context = {} query_list = [ "start_year", "start_month", "start_day", "end_year", "end_month", "end_day", "item", "lower_price", "upper_price" ] for q in query_list: if q in request.GET: query_context[q] = request.GET.get(q) context.update(query_context) query_context = {} query_list = ["direction", "method", "category", "temp", "checked"] for q in query_list: if q in request.GET and request.GET.get(q) != '': query_context[q] = list(map(int, request.GET.getlist(q))) context.update(query_context) # 条件に沿うデータを検索 if "is_query" in request.GET: # 日付 start_date = None if "start_year" in request.GET and "start_month" in request.GET \ and "start_day" in request.GET: if request.GET.get("start_year") != "" \ and request.GET.get("start_month") != "" \ and request.GET.get("start_day") != "": try: date(int(request.GET.get("start_year")), int(request.GET.get("start_month")), int(request.GET.get("start_day"))) start_date = request.GET.get("start_year") + "-" + request.GET.get("start_month") + \ "-" + request.GET.get("start_day") except: start_date = None end_date = None if "end_year" in request.GET and "end_month" in request.GET and "end_day" in request.GET: if request.GET.get("end_year") != "" and request.GET.get( "end_month") != "" and request.GET.get( "end_day") != "": try: date(int(request.GET.get("end_year")), int(request.GET.get("end_month")), int(request.GET.get("end_day"))) end_date = request.GET.get( "end_year") + "-" + request.GET.get( "end_month") + "-" + request.GET.get("end_day") except: end_date = None data = Data.get_range_data(start_date, end_date) # 品目 if "item" in request.GET: item = request.GET.get("item") if item != "": data = Data.get_keyword_data(data, item) # 金額 if "lower_price" in request.GET and request.GET.get( "lower_price") != "": try: data = Data.filter_price( data, int(request.GET.get("lower_price")), None) except: data = data if "upper_price" in request.GET and request.GET.get( "upper_price") != "": try: data = Data.filter_price( data, None, int(request.GET.get("upper_price"))) except: data = data # 方向 if "direction" in request.GET: try: data = Data.filter_directions(data, context["direction"]) except: data = data # 支払い方法 if "method" in request.GET: try: data = Data.filter_methods(data, context["method"]) except: data = data # 分類 if "category" in request.GET: try: data = Data.filter_categories(data, context["category"]) except: data = data # 立替 if "temp" in request.GET: try: data = Data.filter_temps(data, context["temp"]) except: data = data # チェック済み if "checked" in request.GET: try: data = Data.filter_checkeds(data, context["checked"]) except: data = data # 日付順にソート data = Data.sort_ascending(data) context.update({ "show_data": data, "income_sum": Data.get_income_sum(data), "outgo_sum": Data.get_outgo_sum(data), "is_show": True, }) else: context.update({"is_show": False}) return render(request, 'search.html', context)