def create(request): # TODO Receive a `timestamp` parameter and return all the # notes that is modified after `timestamp`. Because # in rare cases uses open the same page multiple times # , and if we just return the note just added we would # miss some notes submitted in other pages. if request.method == 'POST': form = NewNoteForm(request.POST) if form.is_valid(): note = Note(author=request.user, content=form.cleaned_data['content']) note.save() data = [note.get_serializable()] return HttpResponse(simplejson.dumps(data), mimetype='application/json') return HttpResponse('false', mimetype='application/json')
class NoteTest(TestCase): def setUp(self): self.user = User.objects.create_user('tester', 'tester') self.note = Note(author=self.user, content='#test#') self.note.save() def test_author(self): self.assertEqual(self.note.author, self.user) self.assertEqual(self.user.notes.get(), self.note) def test_get_serializable(self): note_dict = self.note.get_serializable() self.assertEqual(note_dict['id'], self.note.id) self.assertEqual(note_dict['content'], self.note.content.html) from django.utils.dateformat import format self.assertEqual(note_dict['modified'], format(self.note.modified, 'n月j日 G:s')) self.assertEqual(note_dict['timestamp'], self.note.modified.strftime('%Y-%m-%d %H:%M:%S.%f')) def test_update_tags(self): orig_tags = self.note.tags.all() self.assertTrue(Tag.objects.get(content='test') in orig_tags) self.note.content = '#new test# ok #a third one#' self.note.save() new_tags = self.note.tags.all() self.assertFalse(Tag.objects.get(content='test') in new_tags) self.assertTrue(Tag.objects.get(content='new test') in new_tags) self.assertTrue(Tag.objects.get(content='a third one') in new_tags) def test_content_conversion(self): list_url = reverse('note.views.list') self.note.content = '#Tag:># message #Tag2#' self.note.save() self.assertEqual(self.note.content.html, '<a href="'+list_url+ '?tags=Tag%3A%3E">#Tag:>#</a> message <a href="'+ list_url+'?tags=Tag2">#Tag2#</a>')