Пример #1
0
    def handle_exception(self, exception, debug_mode):
        # TODO: this can probably be seriously cleaned up.
        exception_name = sys.exc_info()[0].__name__
        exception_details = str(sys.exc_info()[1])
        exception_traceback = ''.join(traceback.format_exception(*sys.exc_info()))
        self.log_exception(msg=exception_name)
        ctx = {
            'usermsg': exception_name,
            'errormsg': exception_details,
            'flashes': self.session.get_flashes()}

        if users.is_current_user_admin() or is_testenv():
            ctx.update({'traceback': exception_traceback})
        if users.is_current_user_admin():
            admin_user = True
            logout_url = users.create_logout_url('/')
            ctx.update({
                'logout_url': logout_url,
                'is_admin': admin_user,
                'request': self.request,
                'current_uri': self.request.uri})

            self.response.out.write(template.render('templates/errors.html', ctx))
        else:
            self.session.add_flash('%s' % exception_name, level='alert-error')
            self.redirect(self.request.uri)
Пример #2
0
    def render_to_response(self, filename, template_args):
        """Renders a Django template and sends it to the client.

        Args:
          filename: template path (relative to this file)
          template_args: argument dict for the template

        """

        if users.is_current_user_admin():
            admin_user = True
            logout_url = users.create_logout_url('/')
        else:
            admin_user = False
            logout_url = None

        if is_testenv():
            test_ver = 'Testing...'
        else:
            test_ver = " "

        # Preset values for the template (thanks to metachris for the inspiration)
        #
        values = {
            'test_ver': test_ver,
            'version': settings.VERSION,
            'logout_url': logout_url,
            'is_admin': admin_user,
            'request': self.request,
            'current_uri': self.request.uri,
            'flashes': self.session.get_flashes()
        }

        # Add manually supplied template values
        template_args.update(values)
        self.response.out.write(
            template.render(self.template_path(filename), template_args)
        )