Exemplo n.º 1
0
    def process(self):
        if request.method not in HTTP_VERBS:
            # Just to be sure that we don't get some crappy http verb we don't expect
            raise BadRequest

        res = ''
        g.rh = self
        sentry_set_tags({'rh': self.__class__.__name__})

        if self.EVENT_FEATURE is not None:
            self._check_event_feature()

        logger.info('%s %s [IP=%s] [PID=%s] [UID=%r]', request.method,
                    request.relative_url, request.remote_addr, os.getpid(),
                    session.get('_user_id'))

        try:
            fossilize.clearCache()
            GenericMailer.flushQueue(False)
            self._check_csrf()
            res = self._do_process()
            signals.after_process.send()

            if self.commit:
                if GenericMailer.has_queue():
                    # ensure we fail early (before sending out e-mails)
                    # in case there are DB constraint violations, etc...
                    db.enforce_constraints()
                    GenericMailer.flushQueue(True)

                db.session.commit()
            else:
                db.session.rollback()
        except DatabaseError:
            db.session.rollback()
            handle_sqlalchemy_database_error(
            )  # this will re-raise an exception
        logger.debug('Request successful')

        if res is None:
            return self._responseUtil.make_empty()

        response = self._responseUtil.make_response(res)
        if self.DENY_FRAMES:
            response.headers['X-Frame-Options'] = 'DENY'
        return response
Exemplo n.º 2
0
    def process(self):
        if request.method not in HTTP_VERBS:
            # Just to be sure that we don't get some crappy http verb we don't expect
            raise BadRequest

        res = ''
        g.rh = self
        sentry_set_tags({'rh': self.__class__.__name__})

        if self.EVENT_FEATURE is not None:
            self._check_event_feature()

        logger.info('%s %s [IP=%s] [PID=%s] [UID=%r]',
                    request.method, request.relative_url, request.remote_addr, os.getpid(), session.get('_user_id'))

        try:
            fossilize.clearCache()
            GenericMailer.flushQueue(False)
            self._check_csrf()
            res = self._do_process()
            signals.after_process.send()

            if self.commit:
                if GenericMailer.has_queue():
                    # ensure we fail early (before sending out e-mails)
                    # in case there are DB constraint violations, etc...
                    db.enforce_constraints()
                    GenericMailer.flushQueue(True)

                db.session.commit()
            else:
                db.session.rollback()
        except DatabaseError:
            db.session.rollback()
            handle_sqlalchemy_database_error()  # this will re-raise an exception
        logger.debug('Request successful')

        if res is None:
            return self._responseUtil.make_empty()

        response = self._responseUtil.make_response(res)
        if self.DENY_FRAMES:
            response.headers['X-Frame-Options'] = 'DENY'
        return response
Exemplo n.º 3
0
    def process(self, params):
        if request.method not in HTTP_VERBS:
            # Just to be sure that we don't get some crappy http verb we don't expect
            raise BadRequest

        cfg = Config.getInstance()
        profile = cfg.getProfile()
        profile_name, res, textLog = '', '', []

        self._startTime = datetime.now()

        g.rh = self

        if self.EVENT_FEATURE is not None:
            self._check_event_feature()

        textLog.append("%s : Database request started" % (datetime.now() - self._startTime))
        logger.info(u'Request started: %s %s [IP=%s] [PID=%s]',
                    request.method, request.relative_url, request.remote_addr, os.getpid())

        is_error_response = False
        try:
            try:
                fossilize.clearCache()
                GenericMailer.flushQueue(False)
                self._check_auth(params)
                profile_name, res = self._do_process(profile)
                signals.after_process.send()

                if self.commit:
                    if GenericMailer.has_queue():
                        # ensure we fail early (before sending out e-mails)
                        # in case there are DB constraint violations, etc...
                        db.enforce_constraints()
                        GenericMailer.flushQueue(True)

                    db.session.commit()
                else:
                    db.session.rollback()
            except DatabaseError:
                handle_sqlalchemy_database_error()  # this will re-raise an exception
            logger.info('Request successful')
        except Exception as e:
            db.session.rollback()
            res = self._getMethodByExceptionName(e)(e)
            if isinstance(e, HTTPException) and e.response is not None:
                res = e.response
            is_error_response = True

        totalTime = (datetime.now() - self._startTime)
        textLog.append('{} : Request ended'.format(totalTime))

        # log request timing
        if profile and os.path.isfile(profile_name):
            rep = Config.getInstance().getTempDir()
            stats = pstats.Stats(profile_name)
            stats.sort_stats('cumulative', 'time', 'calls')
            stats.dump_stats(os.path.join(rep, 'IndicoRequestProfile.log'))
            output = StringIO.StringIO()
            sys.stdout = output
            stats.print_stats(100)
            sys.stdout = sys.__stdout__
            s = output.getvalue()
            f = file(os.path.join(rep, 'IndicoRequest.log'), 'a+')
            f.write('--------------------------------\n')
            f.write('URL     : {}\n'.format(request.url))
            f.write('{} : start request\n'.format(self._startTime))
            f.write('params:{}'.format(params))
            f.write('\n'.join(textLog))
            f.write(s)
            f.write('--------------------------------\n\n')
            f.close()
        if profile and profile_name and os.path.exists(profile_name):
            os.remove(profile_name)

        if self._responseUtil.call:
            return self._responseUtil.make_call()

        if is_error_response and isinstance(res, (current_app.response_class, Response)):
            # if we went through error handling code, responseUtil._status has been changed
            # so make_response() would fail
            return res

        # In case of no process needed, we should return empty string to avoid erroneous output
        # specially with getVars breaking the JS files.
        if not self._doProcess or res is None:
            return self._responseUtil.make_empty()

        return self._responseUtil.make_response(res)
Exemplo n.º 4
0
    def process(self):
        if request.method not in HTTP_VERBS:
            # Just to be sure that we don't get some crappy http verb we don't expect
            raise BadRequest

        profile = config.PROFILE
        profile_name, res, textLog = '', '', []

        self._startTime = datetime.now()

        g.rh = self
        sentry_set_tags({'rh': self.__class__.__name__})

        if self.EVENT_FEATURE is not None:
            self._check_event_feature()

        textLog.append("%s : Database request started" %
                       (datetime.now() - self._startTime))
        logger.info(u'Request started: %s %s [IP=%s] [PID=%s]', request.method,
                    request.relative_url, request.remote_addr, os.getpid())

        is_error_response = False
        try:
            try:
                fossilize.clearCache()
                GenericMailer.flushQueue(False)
                self._check_auth()
                profile_name, res = self._do_process(profile)
                signals.after_process.send()

                if self.commit:
                    if GenericMailer.has_queue():
                        # ensure we fail early (before sending out e-mails)
                        # in case there are DB constraint violations, etc...
                        db.enforce_constraints()
                        GenericMailer.flushQueue(True)

                    db.session.commit()
                else:
                    db.session.rollback()
            except DatabaseError:
                handle_sqlalchemy_database_error(
                )  # this will re-raise an exception
            logger.info('Request successful')
        except Exception as e:
            db.session.rollback()
            res = self._get_error_handler(e)(e)
            if isinstance(e, HTTPException) and e.response is not None:
                res = e.response
            is_error_response = True

        totalTime = (datetime.now() - self._startTime)
        textLog.append('{} : Request ended'.format(totalTime))

        # log request timing
        if profile and os.path.isfile(profile_name):
            rep = config.TEMP_DIR
            stats = pstats.Stats(profile_name)
            stats.sort_stats('cumulative', 'time', 'calls')
            stats.dump_stats(os.path.join(rep, 'IndicoRequestProfile.log'))
            os.remove(profile_name)

        if is_error_response and isinstance(
                res, (current_app.response_class, Response)):
            # if we went through error handling code, responseUtil._status has been changed
            # so make_response() would fail
            return res

        # In case of no process needed, we should return empty string to avoid erroneous output
        # specially with getVars breaking the JS files.
        if not self._doProcess or res is None:
            return self._responseUtil.make_empty()

        response = self._responseUtil.make_response(res)
        if self.DENY_FRAMES:
            response.headers['X-Frame-Options'] = 'DENY'
        return response