def login(self, request, *args, **kwargs): body = json.loads(request.body) # username = body.get('username') api_key = body.get('api_key') if not valid_user(request, api_key): return self.create_response(request, { 'status': False, 'Message': "User Unauthorised", }) else: user_id = fetch_id(api_key) question_set = {} for q in Question.objects.all(): if q.user_id == user_id: question_set[q.question_body] = [ q.option1, q.option2, q.option3, q.option4, q.correct ] resp = { 'status': True, 'Questions': question_set, 'Message': 'Welcome ' + user_id.name } return self.create_response(request, resp)
def add_question(self, request, *args, **kwargs): body = json.loads(request.body) api_key = body.get('api_key') question_body = body.get('question_body') options = body.get('options') correct = body.get('correct') # if the user is invalid if not valid_user(request, api_key): return self.create_response(request, { 'status': False, 'Message': "User Unauthorised", }) else: user_id = fetch_id(api_key) question = Question(user_id=user_id, question_body=question_body, option1=options[0], option2=options[1], option3=options[2], option4=options[3], correct=correct) question.save() response = { 'status': True, 'Message': 'Question Added Successfully' } return self.create_response(request, response)
def create_game(self, request, *args, **kwargs): body = json.loads(request.body) api_key = body.get('api_key') allowed_users = body.get('allowed_users') if not valid_user(request, api_key): return self.create_response(request, { 'status': False, 'Message': 'Invalid Credentials' }) else: # Fetch quiz object for creator user user_id = fetch_id(api_key) quiz = Quiz.objects.get(created_by=user_id) game = Game(quiz=quiz) game.save() for name in allowed_users: user = Users.objects.get(name=name) game.allowed_users.add(user) # Returns the link to created Quiz return self.create_response( request, { 'status': True, 'Message': 'Game created at - https://127.0.0.1:8000/api/v2/play/game/' })
def submit_ans(self, request, *args, **kwargs): body = json.loads(request.body) api_key = body.get('api_key') created_by = body.get('created_by') responses = body.get('responses') # fetch player current score player_user_id = fetch_id(api_key) player_score = player_user_id.score # fetch creator Users Object creator_user_id = Users.objects.get(name=created_by) creator_quiz = Quiz.objects.get(created_by=creator_user_id) if not valid_user(request, api_key): return self.create_response(request, { 'status': True, 'Message': 'Invalid Credentials' }) else: if not is_allowed(creator_user_id, api_key): return self.create_response( request, { 'status': False, 'Message': 'User not allowed to view/play the game' }) elif player_user_id.is_submitted: return self.create_response( request, { 'status': True, 'Message': 'Responses submitted already. Go to leader-board to view scores.', }) else: question_set = creator_quiz.question.all().order_by( 'id').reverse() for q in range(question_set.count()): if responses[q] == question_set[q].correct: player_score += 1 player_user_id.score = player_score player_user_id.is_submitted = True player_user_id.save() return self.create_response( request, { 'status': True, 'Message': 'Responses submitted for :' + player_user_id.name, })
def play_game(self, request, *args, **kwargs): body = json.loads(request.body) api_key = body.get('api_key') created_by = body.get('created_by') # fetch player Users object player_user_id = fetch_id(api_key) # fetch creator Users Object creator_user_id = Users.objects.get(name=created_by) if not valid_user(request, api_key): return self.create_response(request, { 'status': True, 'Message': 'Invalid Credentials' }) else: if not is_allowed(creator_user_id, api_key): return self.create_response( request, { 'status': False, 'Message': 'User not allowed to view/play the game' }) else: quiz = {} for j in Quiz.objects.get( created_by=creator_user_id).question.all(): quiz[j.question_body] = [ j.option1, j.option2, j.option3, j.option4 ] return self.create_response( request, { 'status': True, 'Message': 'Welcome : ' + player_user_id.name, 'Quiz-Questions': quiz })
def add_quiz(self, request, *args, **kwargs): body = json.loads(request.body) api_key = body.get('api_key') if not valid_user(request, api_key): return self.create_response(request, { 'status': False, 'Message': 'Invalid Credentials' }) else: user_id = fetch_id(api_key=api_key) quiz = Quiz(created_by=user_id) quiz.save() for q in Question.objects.all(): if q.user_id == user_id: quiz.question.add(q) return self.create_response( request, { 'status': True, 'Message': 'Quiz created by:' + user_id.name })