Ejemplo n.º 1
0
    def question_respond(self):
        _id = bottle.request.POST.get('_id')
        option_id = bottle.request.POST.get('option')

        if _id and option_id:
            q = Question(_DBCON, _id=_id)
            r = Response(_DBCON)
            r.option = option_id
            r.userId = bottle.request.session.userid
            
            q.responses.append(r)
            q.save()
        
        return bottle.redirect(settings.BASEURL +'/')
Ejemplo n.º 2
0
    def post(self):
        j = request.get_json()

        # need to ensure the required fields are in the json

        if "question_title" not in j:
            abort(422, message="question_title is not in json body")
        else:
            question_title = j["question_title"]

        if "author" not in j:
            abort(422, message="author not in json body")
        else:
            author = j["author"]

        if "description" not in j:
            abort(422, message="description not in json body")
        else:
            description = j["description"]

        question_obj = Question(
            question_title=question_title,
            author=author,
            description=description,
        )

        d = question_obj.save()

        return json.loads(d.to_json())
Ejemplo n.º 3
0
    def question_save(self):
        _id = bottle.request.POST.get('_id')
        t = bottle.request.POST.get('text')
        options = []

        for option in bottle.request.POST.getall('option'):
            o = Option(_DBCON)
            o.text = option
            o.save()
            options.append(o)

        if t and len(t.strip())>0:
            q = Question(_DBCON, _id)
            q.text = t
            q.options = options
            q.save()
        
            return bottle.redirect(settings.BASEURL +'/')
        else:
            self.viewdata.update({'error':'Please complete the form'})
            return self.question()
Ejemplo n.º 4
0
async def questions(req, resp):
    if 'quizz-token' in req.headers:
        # Check the HTTP request method
        if req.method == 'get':
            # Check if user is authenticated
            if check_token(req.headers['quizz-token'], False, True):
                # Get the user
                user = User.objects.get(token=req.headers['quizz-token'])
                resp.status_code = api.status_codes.HTTP_200
                if user.admin == True:
                    resp.media = {'questions': json.loads(Question.objects.all().to_json())}
                else:
                    resp.media = {'questions': json.loads(Question.objects(created_by=user).to_json())}
            # If not, a message will notify the user
            else:
                resp.status_code = api.status_codes.HTTP_403
                resp.media = {'message': 'Not authenticated'}
        elif req.method == 'post':
            # Check if the user is authenticated and is admin
            if check_token(req.headers['quizz-token'], False, True):
                try:
                    data = await req.media()
                    # Check if the question has at least 2 answers and 4 or less answers
                    if len(data['answers']) >= 2 and len(data['answers']) <= 4:
                        if 'question' in data and 'image' in data:
                            answers_not_valid = False
                            for answer in data['answers']:
                                # Check if the answers have the right attributes
                                if 'name' not in answer or 'value' not in answer:
                                    answers_not_valid = True
                                else:
                                    if isinstance(answer['value'], bool) and isinstance(answer['name'], str):
                                        answers_not_valid = False
                                    else:
                                        answers_not_valid = True
                            if not answers_not_valid:
                                # Get the user to put in created_by
                                user = User.objects.get(token=req.headers['quizz-token'])
                                # Create the question
                                new_question = Question(question=data['question'], image=data['image'], answers=data['answers'], created_by=user)
                                new_question.save(validate=True)
                                # Return a successful message to the frontend
                                resp.status_code = api.status_codes.HTTP_200
                                resp.media = {'question': json.loads(new_question.to_json())}
                            else:
                                resp.status_code = api.status_codes.HTTP_401
                                resp.media = {'message': 'data sent is not valid'}
                    else:
                        resp.status_code = api.status_codes.HTTP_401
                        resp.media = {'message': 'A question must have between 2 and 4 answers'}
                # If data sent is not valid
                except ValidationError as validation_error:
                    # Return an error message
                    resp.status_code = api.status_codes.HTTP_401
                    resp.media = {'message': validation_error}
            else:
                # Return an error message if user is not authenticated or admin
                resp.status_code = api.status_codes.HTTP_403
                resp.media = {'message': 'Not authenticated or not admin'}
    else:
        resp.status_code = api.status_codes.HTTP_403
        resp.media = {'message': 'auth token not sent in request'}