Exemplo n.º 1
0
    def delete(self, question_id):
        app.logger.debug('deleting question {}...'.format(question_id))
        client_rpc = RPCClient('mango')
        res = client_rpc.call(method='DeleteQuestion',
                              question_id=question_id)

        return Response.success(
            result={'is_deleted': res.is_deleted}
        )
Exemplo n.º 2
0
    def post(self):
        app.logger.debug('creating a new survey...')
        survey = request.json
        app.logger.debug('survey payload: {}'.format(survey))
        validate(instance=survey, schema=add_survey)
        app.logger.debug('payload is valid, sending request to Mango...')
        client_rpc = RPCClient('mango')
        res = client_rpc.call(method='AddSurvey', **survey)
        app.logger.info('survey has been created: {}'.format(res.survey_id))

        return Response.success(result={'survey_id': res.survey_id},
                                status_code=201)
Exemplo n.º 3
0
    def patch(self, question_id):
        app.logger.debug('patch question {}'.format(question_id))
        question = request.json
        validate(instance=question, schema=update_question)
        app.logger.debug('payload valid, sending request to Mango...')
        client_rpc = RPCClient('mango')
        res = client_rpc.call(method='UpdateQuestion',
                              question_id=question_id,
                              **question)

        return Response.success(
            result={'is_updated': res.is_updated}
        )
Exemplo n.º 4
0
 def post(self):
     app.logger.debug('creating a new question...')
     question = request.json
     app.logger.debug('question payload: {}'.format(question))
     validate(instance=question, schema=add_question)
     app.logger.debug('payload valid, sending request to Mango...')
     client_rpc = RPCClient('mango')
     res = client_rpc.call(method='AddQuestion',
                           **question)
     app.logger.info('question has been created: {}'.format(res.question_id))
     return Response.success(
         result={'question_id': res.question_id},
         status_code=201
     )
Exemplo n.º 5
0
def handle_error(e):
    app.logger.debug('Exception: {}'.format(type(e)))
    app.logger.error(traceback.format_exc())
    err = app.config['ERRORS']
    code = err['server_error']['code']
    reason = err['server_error']['reason']
    details = []
    status_code = 500
    if isinstance(e, HTTPException):
        status_code = e.code
        if str(status_code) in err:
            code = err[str(status_code)]['code']
            reason = err[str(status_code)]['reason']
        else:
            app.logger.error(
                '@TODO status code {} needs to be added in error config'.
                format(status_code))
    elif isinstance(e, GRPCError):
        details = [] if not e.errors.details else list(e.errors.details)
        code = e.errors.code
        reason = err['service_error']['reason']
        if code in err:
            code = err[code]['code']
            reason = err[code]['reason']
            status_code = err[code]['status_code']
        else:
            app.logger.error(
                '@TODO code {} needs to be added in error config'.format(code))
    elif isinstance(e, FutureTimeoutError):
        code = err['future_timeout']['code']
        reason = err['future_timeout']['reason']
        status_code = err['future_timeout']['status_code']
    elif isinstance(e, ValidationError):
        code = err['invalid_schema']['code']
        reason = e.message
        status_code = err['invalid_schema']['status_code']
        details = [e.instance]
    else:
        pass

    return Response.error(error={
        'code': code,
        'reason': reason,
        'details': details
    },
                          status_code=status_code)
Exemplo n.º 6
0
    def get(self, question_id):
        client_rpc = RPCClient('mango')
        res = client_rpc.call(method='GetQuestionById',
                              question_id=question_id)

        return Response.success(
            result={
                '_id': res._id,
                'title': {
                    'on_rate': res.title.on_rate,
                    'on_display': res.title.on_display
                },
                'order': res.order,
                'status': res.status,
                'include_in': list(res.include_in),
                'weight': res.weight
            }
        )
Exemplo n.º 7
0
    def get(self):
        client_rpc = RPCClient('mango')
        res = client_rpc.call(method='GetQuestions')
        questions = []
        for question in res.questions:
            questions.append({
                '_id': question._id,
                'title': {
                    'on_rate': question.title.on_rate,
                    'on_display': question.title.on_display
                },
                'order': question.order,
                'status': question.status,
                'include_in': list(question.include_in),
                'weight': question.weight
            })

        return Response.success(
            result=questions,
        )
Exemplo n.º 8
0
    def get(self):
        filters = parse_get_args(request,
                                 ['skip', 'page_size', 'city', 'complex'])

        app.logger.debug('getting list of surveys {}'.format(filters))
        client_rpc = RPCClient('mango')
        res = client_rpc.call(method='GetSurveys', **filters)

        surveys = []
        for survey in res.surveys:
            surveys.append({
                '_id':
                survey._id,
                'reservation_id':
                survey.reservation_id,
                'total_rating':
                survey.total_rating,
                'questions': [{
                    'question_id': q.question_id,
                    'rating': q.rating
                } for q in survey.questions],
                'staff_id':
                survey.staff_id,
                'user_id':
                survey.user_id,
                'status':
                survey.status,
                'content':
                survey.content,
                'platform':
                survey.platform
            })

        return Response.success(result=surveys,
                                pagination={
                                    'skip': filters.get('skip', 0),
                                    'page_size': filters.get('page_size', 50),
                                    'total_count': res.total_count
                                })