Example #1
0
 def get(self, request):
     question_id = request.query_params['question_id']
     try:
         question = Question.objects.get(id=question_id)
     except:
         raise ObjectDoesNotExist('Question does not exist')
     try:
         options = Options.objects.get(question=question)
     except:
         raise ObjectDoesNotExist('Options does not exist')
     serializer = OptionSerializer(options)
     response = generateresponse('Success', 'Options', serializer.data)
     return Response(response)
Example #2
0
 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)
Example #3
0
 def post(self, request):
     data = request.data
     try:
         uname = data['username']
         passwd = data['password']
         first_name = data['first_name']
         last_name = data['last_name']
         email = data['email']
         new_user = User.objects.create_user(username=uname,
                                             password=passwd,
                                             email=email,
                                             first_name=first_name,
                                             last_name=last_name)
     except:
         raise InvalidInformation(
             "The information entered is invalid or incorrect")
     serializer = UserSerializer(new_user)
     try:
         token = Token.objects.get(user=new_user)
         resp_data = serializer.data
         resp_data['token'] = token.key
     except:
         raise ObjectDoesNotExist('Token for the user not created.')
     response = generateresponse('Success', 'User', resp_data)
     return Response(response)
Example #4
0
 def get(self, request, category_id):
     try:
         category = Category.objects.get(id=category_id)
     except:
         raise ObjectDoesNotExist('Categories do not exist')
     serializer = CategorySerializer(category)
     response = generateresponse('Success', 'category', serializer.data)
     return Response(response)
Example #5
0
    def get(self, request):

        try:
            categories = Category.objects.all()
        except:
            raise ObjectDoesNotExist('Categories do not exist')

        serializer = CategorySerializer(categories, many=True)
        response = generateresponse('Success', 'Categories', serializer.data)
        return Response(response)
Example #6
0
 def create(self, validated_data):
     for quest_category in validated_data['question_category']:
         try:
             category = Category.objects.get(category_name=quest_category['category_name'])
             question = Question.objects.create(question_identifier=validated_data['question_identifier'],
                                                question_text=validated_data['question_text'],
                                                question_tags=validated_data['question_tags'])
             question.question_category.add(category)
             return question
         except:
             raise ObjectDoesNotExist('Category Name does not exist')
Example #7
0
 def get(self,request,category_id):
     response_data = []
     try:
         questions = Question.objects.random(category_id)
     except Exception as e:
         raise ObjectDoesNotExist(e)
     serializer = QuestionSerializer(questions,many=True)
     for data in serializer.data:
         no_of_options = data['options']['number_of_options']
         key_list = data['options'].keys()
         for i in range(no_of_options+1,len(key_list)):
             del data['options'][key_list[i]]
     response = generateresponse('Success','Quiz',serializer.data)
     return Response(response)
Example #8
0
 def get_object(self, pk):
     try:
         return Question.objects.get(pk=pk)
     except:
         raise ObjectDoesNotExist('Question does not exist')
Example #9
0
 def get_object(self, user_id):
     try:
         user = User.objects.get(id=user_id)
         return Profile.objects.get(user=user)
     except:
         raise ObjectDoesNotExist('User/Profile does not exist')