Exemplo n.º 1
0
 def create_quiz(self):
     quiz = Quiz(name=self.name,
                 description=self.description,
                 creator_id=self.creator_id,
                 video_url=self.video_url,
                 time_to_take=self.time_to_take,
                 needs_invite=self.needs_invite,
                 section_id=self.section_id)
     db.session.add(quiz)
     quiz.save()
Exemplo n.º 2
0
 def get(self):
     page = request.args.get('page')
     quiz_id = request.args.get('id')
     if quiz_id:
         quiz = Quiz.get_by_id(quiz_id)
         quiz_dict = quiz.__dict__
         del quiz_dict['_sa_instance_state']
         return jsonify(quiz_dict)
     if request.args.get('owned') == 'true':
         return jsonify(Quiz.get_owned(page))
     return jsonify(Quiz.get_all(page))
Exemplo n.º 3
0
 def get(self):
     q = request.args.get('q')
     page = request.args.get('page')
     article_query, article_total = Quiz.search(q, 1, 5)
     account_query, account_total = AccountUser.search(q, 1, 5)
     articles = Quiz.get_all(page)
     accounts = AccountUser.get_all(page)
     if article_total:
         results = utils.MergeDictList([articles, {"type": "quiz"}])
         return jsonify(results)
     if account_total:
         results = utils.MergeDictList([accounts, {"type": "user"}])
         return jsonify(dict_ctx=results)
     results = {"count": 0, "results": []}
     return jsonify(results)
Exemplo n.º 4
0
 def delete(self):
     body = request.data
     keys = ['id']
     if not body:
         validated = validator.field_validator(keys, {})
         if not validated["success"]:
             app.logger.warning(
                 '{current_user} made request with invalid fields: \n {request_body}'
                     .format(current_user=current_user.get_full_name, request_body=body))
             return jsonify(validated['data']), 400
     if request.is_json:
         body = request.get_json()
         validated = validator.field_validator(keys, body)
         if not validated["success"]:
             app.logger.warning(
                 '{} made request with invalid fields: \n {}'.format(current_user.get_full_name, body))
             return jsonify(validated['data']), 400
         quiz = Quiz.get_by_id(body['id'])
         current_user_roles = AccountUser.get_current_user_roles()
         if 'TUTOR' in current_user_roles or 'LEARNER' in current_user_roles:
             if quiz.creator_id != current_user.id:
                 return jsonify(message='You are not allowed to delete this quiz!'), 403
         if quiz:
             try:
                 quiz.delete()
                 quiz.save()
                 return jsonify(message='Successfully deleted!'), 200
             except Exception as e:
                 app.logger.exception('Exception occurred')
                 return jsonify(message='An error occurred. {}'.format(str(e))), 400
         app.logger.warning('{} trying to update quiz details with {} which does not exist'.
                            format(current_user.name, body['id']))
         return jsonify(message='Quiz with id {} does not exist'.format(body['id'])), 404
Exemplo n.º 5
0
 def get(self, pk):
     quiz = Quiz.get_by_id(pk)
     questions = quiz.questions
     response_dict = dict(count=len(questions))
     results = []
     for v in questions:
         question_dict = v.__dict__
         del question_dict['_sa_instance_state']
         results.append(question_dict)
     response_dict.update(results=results)
     return jsonify(response_dict)
Exemplo n.º 6
0
 def post(self):
     body = request.data
     keys = ['name', 'description', 'video_url', 'time_to_take','needs_invite', 'section_id']
     if not body:
         validated = validator.field_validator(keys, {})
         if not validated["success"]:
             app.logger.warning('User made request with invalid fields: \n {}'.format(body))
             return jsonify(validated['data']), 400
     if request.is_json:
         body = request.get_json()
         validated = validator.field_validator(keys, body)
         if not validated["success"]:
             app.logger.warning('User made request with invalid fields: \n {}'.format(body))
             return jsonify(jsonify=validated['data'])
         name = body['name']
         description = body['description']
         creator_id = current_user.id
         video_url = body['video_url']
         time_to_take = body['time_to_take']
         needs_invite = body['needs_invite']
         section_id = body['section_id']
         try:
             quiz = Quiz(name=name, description=description, creator_id=creator_id, video_url=video_url,
                         time_to_take=time_to_take, needs_invite=needs_invite, section_id=section_id,
                         rating=None)
             quiz.create(quiz)
             quiz.save()
             app.logger.debug("Successfully saved quiz")
             return jsonify(message="Successfully created!"), 201
         except Exception as e:
             app.logger.exception('Exception occurred')
             return jsonify(message='An error occurred. {}'.format(str(e))), 400
     else:
         app.logger.warning('User submitted request with content type header not being application/json')
         return jsonify(message='Content-type header is not application/json'), 400