def test_note_updates_tags(self): """ Tests Tags are automatically updated based on Note content. """ note = Note(title='My first note', content='Noteworthy content for #pyladies') note.save() for i in range(3): if i == 0: note.content = '... but it can be improved. #workshop #python' elif i == 1: note.content = '... but it can be improved some more. #workshop #python' else: pass # test saving twice note.save() self.assertTrue(Tag.objects.get(keyword='workshop')) self.assertTrue(Tag.objects.get(keyword='python')) self.assertEqual(Tag.objects.filter(keyword='pyladies').count(), 0) self.assertEqual(Tag.objects.count(), 2) self.assertEqual(note.tags.count(), 2) self.assertEqual(Note.objects.get().tags.count(), 2)
def test_note_updates_tags(self): """ Tests Tags are automatically updated based on Note content. """ note = Note(title='My first note', content='Noteworthy content for #pyladies') note.save() for i in range(3): if i == 0: note.content = '... but it can be improved. #workshop #python' elif i == 1: note.content = '... but it can be improved some more. #workshop #python' else: pass # test saving twice note.save() self.assertTrue(Tag.objects.get(keyword='workshop')) self.assertTrue(Tag.objects.get(keyword='python')) self.assertEqual(Tag.objects.filter(keyword='pyladies').count(), 0) self.assertEqual(Tag.objects.count(), 2) self.assertEqual(note.tags.count(), 2) self.assertEqual(Note.objects.get().tags.count(), 2)
def notes(request): if request.method == 'GET': notes = Note.objects.all() for i in range(len(notes)): if notes[i].title is None or len(notes[i].title) == 0: notes[i].title = notes[i].content[:MAX_SYMBOLS] title = request.query_params.get('string', None) if title is not None: notes_title = [ notes.filter(title__icontains=title), notes.filter(content__icontains=title) ] res = [] for i in notes_title: for j in i: if j not in res: res.append(j) notes = res note_serializer = NoteSerializer(notes, many=True) return JsonResponse(note_serializer.data, safe=False) elif request.method == 'POST': content = request.data.get('content') note = Note() note.content = content note.title = request.data.get('title') note.save() note_serializer = NoteSerializer(note) return JsonResponse(note_serializer, safe=False, status=status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs): content = request.data.get('content') note = Note() note.content = content note.title = request.data.get('title') note.save() return Response(note)
def post(self, request, format = None): data = request.data note = Note() if data['title'] and data['content']: note.title = data['title'] note.content = data['content'] note.pub_date = timezone.now() + timezone.timedelta(hours=3) note.login = request.user note.save() serializer = NoteSerializer(note, many = False, context = { 'request': request}) return Response(serializer.data)