def post(self, request, format=json): print '----------------------------------------' data = JSONParser.parse(request) serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=200)
def get(self, request): filter = request.query_params.dict() if filter == {}: response = '' try: questions = Question.objects.all() except: raise ObjectDoesNotExist('Question does not exist') try: serializer = QuestionSerializer(questions, many=True) response = generateresponse('Success', 'questions', serializer.data) except Exception as e: print e else: if 'category' in filter and 'is_play' in filter: if filter['is_play'] == 'false': category = filter['category'] questions = Question.objects.filter( question_category__id=category) serializer = QuestionSerializer(questions, many=True) response = generateresponse('Success', 'questions', serializer.data) else: raise InvalidInformation('Filter/is_play is not present') return Response(response)
def post(self, request, format=json): print '----------------------------------------' data=JSONParser.parse(request) serializer=QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=200)
def post_question(request): if request.method == 'POST': print("data", request.data) serializer = QuestionSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): data = request.data serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() response = generateresponse('Success', 'question', serializer.data) return Response(response) else: raise InvalidInformation(serializer.errors)
def setUp(self): self.user = User.objects.create_user('Emma') self.client.force_authenticate(user=self.user) self.question = Question.objects\ .create(author=self.user, title="Who was first?", question_text="The chicken or the egg?") self.data = QuestionSerializer(self.question).data self.data.update({'likes': 1, 'closed': 'true'})
def addQuestion(request, format=json): #Post an order if request.method=='POST': data = JSONParser().parse(request) serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=200) else: return JSONRenderer().render(serializer.errors)
def addQuestion(request, format=json): #Post an order if request.method == 'POST': data = JSONParser().parse(request) serializer = QuestionSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=200) else: return JSONRenderer().render(serializer.errors)
def question_list(request, format=json): if request.method == 'GET': questions = Question.objects.all() serialized_questions = QuestionSerializer(questions, many=True) print serialized_questions.data print serialized_questions return Response(serialized_questions.data)
class UpdateQuestionTest(APITestCase): def setUp(self): self.user = User.objects.create_user('Emma') self.client.force_authenticate(user=self.user) self.question = Question.objects\ .create(author=self.user, title="Who was first?", question_text="The chicken or the egg?") self.data = QuestionSerializer(self.question).data self.data.update({'likes': 1, 'closed': 'true'}) def test_can_update_question(self): response = self.client\ .put(reverse('qapp:api:update-question-api', args=[self.question.id]), self.data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK)
def questions(request): """ /question Gets all the questions. """ if request.method == 'GET': questions = Question.objects.all() serializer = QuestionSerializer(questions, many=True) return Response(serializer.data)
def question(request, id): """ /question/id Gets a specific question. """ if request.method == 'GET': question = Question.objects.get(id=id) serializer = QuestionSerializer(question, many=False) return Response(serializer.data)
def next_question(request, id, ans_id): """ /question/id/ans_id Gets the next question based on the ans_id """ if request.method == 'GET': question = Question.objects.get(id=id) answer = Answer.objects.get(question=question, answer_id=ans_id) next_question = answer.next_question serializer = QuestionSerializer(next_question, many=False) return Response(serializer.data)
def publish_question(event, question_id, room_id, is_last_question=False): data = dict() data['event'] = event question = QuestionSerializer( Question.objects.filter(pk=question_id).first()).data choices = [ ChoiceSerializer(c).data for c in Choice.objects.filter(question=question_id).all() ] question['is_last_question'] = is_last_question question['choices'] = choices data['data'] = question data['room'] = room_id push_event(data)
def create_question(request, format=None): resp_dict = dict(message='', error=0, result='') if request.method == 'POST': serializer = QuestionSerializer(data=request.data) if serializer.is_valid(): if not request.user.is_anonymous: serializer.save(asked_by=request.user) serializer.save() resp_dict.update(result=serializer.data, message='Success') return Response(resp_dict, status=status.HTTP_201_CREATED) else: resp_dict.update(message='Not a valid user', error=1) return Response(resp_dict, status=status.HTTP_403_FORBIDDEN) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def index(request, pk): """ Retrieve, update or delete a code snippet. """ try: poll = Poll.objects.get(pk=pk) except Poll.DoesNotExist: try: loadpoll.loadpoll(pk) except: return HttpResponse(status=404) try: CountMethods.approvecount(pk) questions = Question.objects.filter(poll=pk) except Question.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = QuestionSerializer(questions, many=True) return JsonResponse(serializer.data, safe=False) else: return HttpResponse(status=400)
def current_exam(request): room_id = request.data.get('room') or request.query_params.get('room') exam = Exam.objects.filter(room_id=room_id).first() if not exam: return Response(dict(msg="room_id not found"), status=status.HTTP_400_BAD_REQUEST) exam_data = ExamSerializer(exam).data question = QuestionSerializer( Question.objects.filter( pk=exam_data['current_question_id']).first()).data choices = [ ChoiceSerializer(c).data for c in Choice.objects.filter( question=exam_data['current_question_id']).all() ] question['choices'] = choices exam_data['question'] = question return Response(exam_data, status=status.HTTP_200_OK)
def get(self, request, question_id): question = self.get_object(question_id) serializer = QuestionSerializer(question) response = generateresponse('Success', 'question', serializer.data) return Response(response)
def get(self, request, format=None): questions = Question.objects.all() serializer = QuestionSerializer(questions, many=True) return Response(serializer.data, status=status.HTTP_200_OK)