def test_new_journal_entry_defaults_to_today(self):
        entry = JournalEntry()
        entry.save()

        entry = JournalEntry.objects.all()[0]

        self.assertEqual(entry.date,date.today())
    def test_creating_journal_entry_checks_for_date(self):
        date_faker = DateFaker()
        try:
            date_faker.fake_date(date(2015,3,15))
            entry = JournalEntry()
            entry.save()

            saved_journal_entries = JournalEntry.objects.all()
            self.assertEqual(saved_journal_entries.count(),1)

            entry = saved_journal_entries[0]
            self.assertEqual(entry.date,date(2015,3,15))
        finally:
            date_faker.reset()
    def test_saving_and_retrieving_journal_entries(self):
        first_journal_entry = JournalEntry()
        first_journal_entry.text = 'The first (ever) journal entry'
        first_journal_entry.save()

        second_journal_entry = JournalEntry()
        second_journal_entry.text = 'Second journal entry'
        second_journal_entry.save()

        saved_journal_entries = JournalEntry.objects.all()
        self.assertEqual(saved_journal_entries.count(),2)

        first_journal_entry = saved_journal_entries[0]
        second_journal_entry = saved_journal_entries[1]
        self.assertEqual(first_journal_entry.text,'The first (ever) journal entry')
        self.assertEqual(second_journal_entry.text,'Second journal entry')
示例#4
0
def home_page(request):
    if request.method == 'POST':
        JournalEntry.objects.create(text=request.POST['entry_text'])
        return redirect('/')

    journal_entries = JournalEntry.objects.order_by('-date')
    no_entry_for_today = not JournalEntry.objects.filter(date=JournalEntry.get_entry_date()).exists()
    return render(request, 'home.html', {
        'journal_entries': journal_entries,
        'no_entry_for_today': no_entry_for_today
    })