def test_search(self): user = User.objects.create(username='******', email='*****@*****.**') Question.objects.create(subject='test', body='test', user=user) Question.objects.create(subject='egg test', body='egg test', user=user) Question.objects.create(subject='test spam', body='test spam', user=user) Question.objects.create(subject='spam egg', body='spam egg', user=user) query_test = Question.search('test') self.assertEqual(query_test.count(), 3) query_test = Question.search('egg') self.assertEqual(query_test.count(), 2) query_test = Question.search('spam') self.assertEqual(query_test.count(), 2)
def get_list(request): """ Get the JSON response of list of questions. """ if not request.user.is_authenticated: return HttpResponseForbidden(json.dumps( {'message': 'User must be authenticated'}), content_type='application/json') iterator = Question.objects.filter(status=Question.PUBLIC) \ if not request.GET.get('q') else Question.search(request.GET.get('q')) data = [{ 'subject': question.subject, 'body': decrypt(question.body, settings.TEXT_SECRET_CODE), 'id': question.id, 'user__first_name': question.user.first_name, 'user__last_name': question.user.last_name, } for question in iterator] return HttpResponse(json.dumps(data), content_type='application/json')