def create(request): post_data = request.POST if not post_data: return redirect(new) expense = Expense(name=post_data['name'], source=post_data.get('source', None), amount=post_data['amount'], date=post_data['date']) try: expense.full_clean() except ValidationError as e: for message in e.messages: messages.error(request, message) context = RequestContext( request, { 'expense': expense, 'F_checked': 'checked' if expense.source == "F" else None, 'C_checked': 'checked' if expense.source == "C" else None, 'H_checked': 'checked' if expense.source == "H" else None }) return render(request, 'expenses/new.html', context) else: expense.save() return redirect(index)
def create(request): post_data = request.POST if not post_data: return redirect(new) expense = Expense(name=post_data['name'], source=post_data.get('source', None), amount=post_data['amount'], date=post_data['date'] ) try: expense.full_clean() except ValidationError as e: for message in e.messages: messages.error(request, message) context = RequestContext(request, { 'expense': expense, 'F_checked': 'checked' if expense.source=="F" else None, 'C_checked': 'checked' if expense.source=="C" else None, 'H_checked': 'checked' if expense.source=="H" else None }) return render(request, 'expenses/new.html', context) else: expense.save() return redirect(index)
def test_expense(self): o = Expense( title="Mercedes", amount=212_000, date=f"2020-02-23", description="", ) o.full_clean() o.save() self.assertTrue(o.is_expensive())
def handle(self, *args, **options): while User.objects.count() < 10: User.objects.create_user( username=silly.firstname().lower(), password="******", ) users = list(User.objects.all()) n = 10 for i in range(n): o = Expense( user=random.choice(users), created_at=silly.datetime(), title="{} {}".format(silly.adjective(), silly.noun()), amount=str(silly.number() + silly.number() / 10)[:5], description=silly.sentence() + "\n" + silly.sentence() ) o.full_clean() o.save() o.created_at = silly.datetime() o.save()