Ejemplo n.º 1
0
 def post(self, question_id):
     """
     Refresh KG for question
     ---
     tags: [cache]
     parameters:
       - in: path
         name: question_id
         description: "question id"
         type: string
         required: true
     responses:
         202:
             description: "refreshing in progress"
             type: string
         404:
             description: "invalid question key"
             type: string
     """
     auth = request.authorization
     if auth:
         user_email = auth.username
         user = get_user_by_email(user_email)
         user_id = user.id
     else:
         user_id = current_user.id
         user_email = current_user.email
     try:
         question = get_question_by_id(question_id)
     except Exception as err:
         return "Invalid question key.", 404
     # Update the knowledge graph for a question
     task = update_kg.apply_async(args=[question_id], kwargs={'user_email':user_email})
     return {'task_id':task.id}, 202
Ejemplo n.º 2
0
 def delete(self, question_id):
     """
     Delete question
     ---
     tags: [question]
     parameters:
       - in: path
         name: question_id
         description: "question id"
         type: string
         required: true
     responses:
         200:
             description: "question deleted"
         401:
             description: "unauthorized"
         404:
             description: "invalid question key"
     """
     auth = request.authorization
     if auth:
         user_email = auth.username
         user = get_user_by_email(user_email)
     else:
         user = current_user
     logger.info('Deleting question %s', question_id)
     try:
         question = get_question_by_id(question_id)
     except Exception as err:
         return "Invalid question key.", 404
     if not (user == question.user or user.has_role('admin')):
         return "UNAUTHORIZED", 401 # not authorized
     db.session.delete(question)
     db.session.commit()
     return "SUCCESS", 200
Ejemplo n.º 3
0
    def delete(self, question_id):
        """
        Delete question
        ---
        tags: [questions]
        parameters:
          - in: path
            name: question_id
            description: "question id"
            schema:
                type: string
            required: true
        responses:
            200:
                description: "question deleted"
                content:
                    text/plain:
                        schema:
                            type: string
            401:
                description: "unauthorized"
                content:
                    text/plain:
                        schema:
                            type: string
            404:
                description: "invalid question key"
                content:
                    text/plain:
                        schema:
                            type: string
        """
        auth = request.authorization
        if auth:
            user_email = auth.username
            user = get_user_by_email(user_email)
            user_id = user['id']
        else:
            user = getAuthData()
            user_id = user['id']
            user_email = user['email']

        try:
            question = get_question_by_id(question_id)
        except Exception as err:
            return "Invalid question id.", 404

        if not (user_email == question['owner_email'] or user['is_admin']):
            return "UNAUTHORIZED", 401  # not authorized

        # User is authoriced
        logger.info('Deleting question %s at the request of %s', question_id,
                    user_email)
        delete_question_by_id(question_id)

        return "SUCCESS", 200
Ejemplo n.º 4
0
 def post(self, question_id):
     """
     Edit question metadata
     ---
     tags: [question]
     parameters:
       - in: path
         name: question_id
         description: "question id"
         type: string
         required: true
       - in: body
         name: name
         description: "name of question"
         required: true
       - in: body
         name: natural_question
         description: "natural-language question"
         required: true
       - in: body
         name: notes
         description: "notes"
         required: true
     responses:
         200:
             description: "question edited"
         401:
             description: "unauthorized"
         404:
             description: "invalid question key"
     """
     auth = request.authorization
     if auth:
         user_email = auth.username
         user = get_user_by_email(user_email)
     else:
         user = current_user
     logger.info('Editing question %s', question_id)
     try:
         question = get_question_by_id(question_id)
     except Exception as err:
         return "Invalid question key.", 404
     if not (user == question.user or user.has_role('admin')):
         return "UNAUTHORIZED", 401 # not authorized
     question.name = request.json['name']
     question.notes = request.json['notes']
     question.natural_question = request.json['natural_question']
     db.session.commit()
     return "SUCCESS", 200
Ejemplo n.º 5
0
    def post(self, question_id):
        """
        Refresh KG for question
        ---
        tags: [questions]
        parameters:
          - in: path
            name: question_id
            description: "question id"
            schema:
                type: string
            required: true
        responses:
            202:
                description: "refreshing in progress"
                content:
                    text/plain:
                        schema:
                            type: string
            404:
                description: "invalid question key"
                content:
                    text/plain:
                        schema:
                            type: string
        """
        auth = request.authorization
        if auth:
            user_email = auth.username
            user = get_user_by_email(user_email)
            user_id = user['id']
        else:
            user = getAuthData()
            user_id = user['id']
            user_email = user['email']

        # Update the knowledge graph for a question
        logger.info(
            f'Adding update task for question {question_id} for user {user_email} to the queue'
        )
        task = update_kg.apply_async(args=[question_id],
                                     kwargs={'user_email': user_email})
        logger.info(
            f'Update task for question {question_id} for user {user_email} to the queue has recieved task_id {task.id}'
        )
        return {'task_id': task.id}, 202
Ejemplo n.º 6
0
    def post(self, question_id):
        """
        Edit question metadata
        ---
        tags: [questions]
        parameters:
          - in: path
            name: question_id
            description: "question id"
            schema:
                type: string
            required: true
        requestBody:
            description: request body
            content:
                application/json:
                    schema:
                        type: object
                        required:
                          - name
                          - natural_question
                          - notes
                        properties:
                            name:
                                description: "name of question"
                                schema:
                                    type: string
                            natural_question:
                                description: "natural-language question"
                                schema:
                                    type: string
                            notes:
                                description: "notes"
                                schema:
                                    type: string
        responses:
            200:
                description: "question edited"
                content:
                    text/plain:
                        schema:
                            type: string
            401:
                description: "unauthorized"
                content:
                    text/plain:
                        schema:
                            type: string
            404:
                description: "invalid question key"
                content:
                    text/plain:
                        schema:
                            type: string
        """
        auth = request.authorization
        if auth:
            user_email = auth.username
            user = get_user_by_email(user_email)
            user_id = user['id']
        else:
            user = getAuthData()
            user_id = user['id']
            user_email = user['email']
        
        try:
            question = get_question_by_id(question_id)
        except Exception as err:
            return "Invalid question id.", 404
        
        if not (user_email == question['owner_email'] or user['is_admin']):
            return "UNAUTHORIZED", 401 # not authorized

        # User is authorized
        logger.info('Editing question %s at the request of %s', question_id, user_email)
        mods = {}
        if 'notes' in request.json:
            mods['notes'] = request.json['notes']
        if 'natural_question' in request.json:
            mods['natural_question'] = request.json['natural_question']
        
        modify_question_by_id(question_id, mods)

        return "SUCCESS", 200
Ejemplo n.º 7
0
    def post(self, question_id):
        """
        Set Question Visiblity.
        ---
        tags: [questions]
        parameters:
          - in: path
            name: question_id
            description: "question id"
            schema:
                type: string
            required: true
          - in: query
            name: visibility
            description: "question visibility"
            required: true
            schema:
                type: string
                enum:
                  - private
                  - public
                  - promoted
                default: private
                example: private
        responses:
            200:
                description: "question visibility edited"
                content:
                    text/plain:
                        schema:
                            type: string
            401:
                description: "unauthorized"
                content:
                    text/plain:
                        schema:
                            type: string
            404:
                description: "invalid question key"
                content:
                    text/plain:
                        schema:
                            type: string
        """

        auth = request.authorization
        if auth:
            user_email = auth.username
            user = get_user_by_email(user_email)
            user_id = user['id']
        else:
            user = getAuthData()
            user_id = user['id']
            user_email = user['email']

        try:
            question = get_question_by_id(question_id)
        except Exception as err:
            return "Invalid question id.", 404

        visibility = request.args.get('visibility', default='private')

        if 'roles' in user:
            user_is_admin = any(role['name'] == 'admin' for role in user['roles'])
        else:
            user_is_admin = user['is_admin']

        if not (user_email == question['owner_email'] or user_is_admin):
            return "UNAUTHORIZED", 401  # not authorized

        if visibility == 'promoted' and not user_is_admin:
            return "UNAUTHORIZED", 401

        # User is authorized
        mods = {
            'visibility': visibility
        }

        modify_question_by_id(question_id, mods)

        return "SUCCESS", 200
Ejemplo n.º 8
0
    def post(self):
        """
        Create a question.
        ---
        tags: [questions]
        requestBody:
            name: question
            content:
                application/json:
                    schema:
                        $ref: '#/components/schemas/Question'
            required: true
        responses:
            201:
                description: "question id"
                content:
                    text/plain:
                        schema:
                            type: string
        """
        auth = request.authorization
        if auth:
            user_email = auth.username
            user = get_user_by_email(user_email)
            user_id = user['id']
        else:
            user = getAuthData()
            user_id = user['id']
            user_email = user['email']

        logger.debug(f"Creating new question for user {user_email}.")
        request_json = request.json
        if 'question_graph' not in request_json:
            request_json['question_graph'] = request_json.pop(
                'machine_question', None)

        question_id = add_question(request.json, owner_id=user_id)

        if request.headers.get('RebuildCache', default='true') == 'true':
            # To speed things along we start an answerset generation task for this question
            # This isn't the standard answerset generation task because we might also trigger a KG Update

            logger.info(
                f'Adding update and answer tasks for question {question_id} for user {user_email} to the queue'
            )
            ug_task = update_kg.signature(args=[question_id],
                                          kwargs={'user_email': user_email},
                                          immutable=True)
            as_task = answer_question.signature(
                args=[question_id],
                kwargs={'user_email': user_email},
                immutable=True)
            task = answer_question.apply_async(
                args=[question_id],
                kwargs={'user_email': user_email},
                link_error=ug_task | as_task)
            logger.info(
                f'Update task for question {question_id} for user {user_email} to the queue has recieved task_id {task.id}'
            )

        elif request.headers.get('AnswerNow', default='true') == 'true':
            logger.info(
                f'Adding answer task for question {question_id} for user {user_email} to the queue'
            )
            task = answer_question.apply_async(
                args=[question_id], kwargs={'user_email': user_email})
            logger.info(
                f'Answer task for question {question_id} for user {user_email} to the queue has recieved task_id {task.id}'
            )

        return question_id, 201
Ejemplo n.º 9
0
    def post(self):
        """
        Create new question
        ---
        tags: [question]
        parameters:
          - in: body
            name: question
            schema:
                $ref: '#/definitions/Question'
            required: true
          - name: RebuildCache
            in: header
            description: flag indicating whether to update the cached knowledge graph
            required: false
            default: true
            type: string
          - name: AnswerNow
            in: header
            description: flag indicating whether to find answers for the question
            required: false
            default: true
            type: string
        responses:
            201:
                description: "question id"
                type: string
        """
        auth = request.authorization
        if auth:
            user_email = auth.username
            user = get_user_by_email(user_email)
            user_id = user.id
        else:
            user_id = current_user.id
            user_email = current_user.email
        logger.debug(f"Creating new question for user {user_email}.")
        logger.debug(request.json)
        qid = ''.join(
            random.choices(string.ascii_uppercase + string.ascii_lowercase +
                           string.digits,
                           k=12))
        if not request.json['name']:
            return abort(400, "Question needs a name.")
        q = Question(request.json, id=qid, user_id=user_id)

        if not 'RebuildCache' in request.headers or request.headers[
                'RebuildCache'] == 'true':
            # To speed things along we start a answerset generation task for this question
            # This isn't the standard answerset generation task because we might also trigger a KG Update
            ug_task = update_kg.signature(args=[qid],
                                          kwargs={'user_email': user_email},
                                          immutable=True)
            as_task = answer_question.signature(
                args=[qid], kwargs={'user_email': user_email}, immutable=True)
            task = answer_question.apply_async(
                args=[qid],
                kwargs={'user_email': user_email},
                link_error=ug_task | as_task)
        elif not 'AnswerNow' in request.headers or request.headers[
                'AnswerNow'] == 'true':
            task = answer_question.apply_async(
                args=[qid], kwargs={'user_email': user_email})

        return qid, 201