def test_expense_report_form_validation_ok(self): def check(from_date, to_date): data = {'from_date': from_date, 'to_date': to_date} self.assertTrue(ExpenseReportForm(data).is_valid()) check(datetime_util.today(), datetime_util.tmr()) check(datetime_util.today(), datetime_util.today()) check('2021-06-15', '2021-06-30') check('2021-06-15', '2021-06-15')
def test_expense_report_form_validation_fail(self): def check(from_date, to_date): data = {'from_date': from_date, 'to_date': to_date} form = ExpenseReportForm(data) self.assertFalse(form.is_valid()) check(datetime_util.today(), datetime_util.today() - timedelta(days=1)) check('2021-06-15', '2021-06-14') check('2021-06', '2021-06-14') check('2021-06-31', '2021-07-01')
class ExpenseReportForm(forms.Form): from_date = forms.DateField( initial=datetime_util.first_date_current_month(), required=True, widget=forms.DateInput(attrs={ 'type': 'date', 'class': 'form-control', }), ) to_date = forms.DateField( initial=datetime_util.today(), required=True, widget=forms.DateInput(attrs={ 'type': 'date', 'class': 'form-control', }), ) def clean(self): super().clean() from_date = self.cleaned_data.get('from_date') to_date = self.cleaned_data.get('to_date') if from_date and to_date: if from_date > to_date: raise ValidationError( "from_date must not be later than to_date")
class ExpenseReportDefaultView(RedirectView): permanent = True url = reverse_lazy( "homie_admin:expense_report", kwargs={ 'from_date': datetime_util.first_date_current_month().strftime(DATE_FORMAT), 'to_date': datetime_util.today().strftime(DATE_FORMAT), })
def test_get_expenses_in(self): Expense.objects.all().delete() self.assertEqual( 0, Expense.get_expenses_in( start_date=datetime_util.today(), end_date=datetime_util.today(), ).count()) e = Expense.objects.create( wallet=Wallet.objects.first(), category=ExpenseCategory.objects.first(), value=199, ) self.assertEqual( e, Expense.get_expenses_in( start_date=datetime_util.today(), end_date=datetime_util.today(), ).get())
def _get_x_labels() -> List[str]: # labels on x axis # we only show important days important_days = ( datetime_util.first_date_current_month(), datetime_util.today(), datetime_util.last_date_current_month(), ) return [ date.strftime('%d/%m') if date in important_days else '' for date in datetime_util.get_date_iterator( datetime_util.first_date_current_month(), datetime_util.last_date_current_month()) ]
def _get_current_accumulated_expenses(group: ExpenseGroup) -> List[int]: """ Returns a list of accumulated expense. The i-th element is the expense from day 1 of this month until day i-th. """ expense_til_today = [] for date in datetime_util.get_date_iterator( datetime_util.first_date_current_month(), datetime_util.today()): expense = sum([ ex.value for ex in Expense.get_expenses_in( start_date=date, end_date=date).filter(category__group=group) ]) expense_til_today.append(expense if len(expense_til_today) == 0 else expense_til_today[-1] + expense) return expense_til_today
def _init_money_expense(self, wallets: List[Wallet]): food = ExpenseGroup.objects.create(name="Food") beauty = ExpenseGroup.objects.create(name="Beauty") ExpenseCategory.objects.create(group=food, name='Drink') ExpenseCategory.objects.create(group=food, name='Dinner') ExpenseCategory.objects.create(group=food, name='Snack') ExpenseCategory.objects.create(group=beauty, name='Spa') ExpenseCategory.objects.create(group=beauty, name='Product') for category in ExpenseCategory.objects.all(): for i in range(0, 20): Expense.objects.create( wallet=self.fake.random.choice(wallets), category=category, name=self.fake.sentence(nb_words=5), value=self.fake.pyint(1000, 1000000), pay_date=(datetime_util.today() - datetime.timedelta(days=self.fake.pyint(0, 90))), )
def app_index(self, request, app_label, extra_context=None): from_date = datetime_util.first_date_current_month() to_date = datetime_util.today() income = Income.get_income_value_in(from_date, to_date) expense = Expense.get_expense_value_in(from_date, to_date) return super().app_index(request, app_label, extra_context={ 'net_worth': finance_util.get_net_worth(), 'income_this_month': income, 'expense_this_month': expense, 'balance_this_month': income - expense, 'pinned_models': [ model_cls._meta.object_name for model_cls in (Budget, Expense, Income) ] })
def test_expense_report_view_redirect(self): res = self.client.get(_default_expense_report_url) self.assertEqual(301, res.status_code) self.assertEqual( _expense_report_url(datetime_util.first_date_current_month(), datetime_util.today()), res.url)
def setUp(self) -> None: super().setUp() self.month_start = datetime_util.first_date_current_month() self.month_end = datetime_util.today() self.this_month_report_url = _expense_report_url( self.month_start, self.month_end)
def test_get_y_values(self): expenses, index = admin._get_y_values(ExpenseGroup.objects.first()) self.assertEqual(datetime_util.last_date_current_month().day, len(expenses)) self.assertEqual(datetime_util.today().day - 1, index)