Пример #1
0
        def run_jobs():
            lock = threading.RLock()
            lock.acquire()
            session = meta.Session
            log.info(source + 'Started scheduled background jobs')

            # add task is for debug
            total = tasks.add(2, 3)
            print total

            try:
                log.info(source + "Checking ueb model build request status")
                from routes import request_config
                from routes.mapper import Mapper
                config = request_config()
                config.mapper = Mapper()
                config.host = '127.0.0.1:5000'
                config.protocol = 'http'
                #if hasattr(config, 'using_request_local'):
                config.request_local = tasks.check_ueb_request_process_status()
                config = request_config()
                    #tasks.check_ueb_request_process_status()
                log.info(source + "UEB model build request status check finished")
            except Exception as e:
                log.error(source + 'Failed to check ueb package build request status.\nException:%s' % e)
                pass
            try:
                log.info(source + "Retrieving ueb model package from app server")
                tasks.retrieve_ueb_packages()
                log.info(source + "Retrieving ueb model package from app server was successful")
            except Exception as e:
                log.error(source + 'Failed to retrieve ueb package from app server.\nException:%s' % e)
                pass

            try:
                log.info(source + "Checking ueb model run status")
                tasks.check_ueb_run_status()
                log.info(source + "UEB model run status check finished")
            except Exception as e:
                log.error(source + 'Failed to check ueb package run status.\nException:%s' % e)
                pass
            try:
                log.info(source + "Retrieving ueb model output package from app server")
                tasks.retrieve_ueb_run_output_packages()
                log.info(source + "Retrieving ueb model output package from app server was successful")
            except Exception as e:
                log.error(source + 'Failed to retrieve ueb model output package from app server.\nException:%s' % e)
                pass

            session.remove()
            log.info(source + 'Finished scheduled background jobs')
            time.sleep(interval * 60)
            lock.release()
Пример #2
0
 def _fix_routes(self):
     env = {}
     for k, v in request.environ.items():
         env[k] = v
     env['SCRIPT_NAME'] = ''
     config = routes.request_config()
     config.environ = env
Пример #3
0
 def __html__(self):
     '''Return a transformed URL if necessary (appending protocol and CDN)'''
     host = self.get('netloc', None)
     # Don't CDN urls with hosts we're not re-writing
     if host:
         if (project.STATIC_SOURCES is None or
                                        host not in project.STATIC_SOURCES):
             return self.raw_url
     if (project.USE_CDN and (project.CDN_HOST or project.STATIC_HOSTS)):
         # get the protocol for the current request
         # this requires the custom HTTP header X-Forwarded-Proto
         # set if running behind a proxy (or if SSL is terminated
         # upstream)
         protocol = request_config().protocol
         # use the round robin hosts to speed download when not https
         if protocol != "https" and project.STATIC_HOSTS:
             self['netloc'] = project.STATIC_HOSTS[hashfunc(self.raw_url) %
                                                   len(project.STATIC_HOSTS)]
         else:
             self['netloc'] = project.CDN_HOST
         # adjust the scheme of any link with a net location
         # to match the current request so we don't have mixed link
         # protocols
         if self['netloc']:
             self['scheme'] = protocol
     return ParseResult(**self).geturl()
Пример #4
0
 def _fix_routes(self):
     env = {}
     for k,v in request.environ.items():
         env[k]=v
     env['SCRIPT_NAME'] = ''
     config = routes.request_config()
     config.environ = env
Пример #5
0
 def __html__(self):
     '''Return a transformed URL if necessary (appending protocol and CDN)'''
     host = self.get('netloc', None)
     # Don't CDN urls with hosts we're not re-writing
     if host:
         if (context.config.STATIC_SOURCES is None
                 or host not in context.config.STATIC_SOURCES):
             return self.raw_url
     if (context.config.USE_CDN
             and (context.config.CDN_HOST or context.config.STATIC_HOSTS)):
         # get the protocol for the current request
         # this requires the custom HTTP header X-Forwarded-Proto
         # set if running behind a proxy (or if SSL is terminated
         # upstream)
         try:
             protocol = request_config().protocol
         except AttributeError:
             # are we not in a request? Use a default protocol
             protocol = context.config.DEFAULT_PROTOCOL
         # use the round robin hosts to speed download when not https
         if protocol != "https" and context.config.STATIC_HOSTS:
             self['netloc'] = context.config.STATIC_HOSTS[hashfunc(
                 self.raw_url) % len(context.config.STATIC_HOSTS)]
         else:
             self['netloc'] = context.config.CDN_HOST
         # adjust the scheme of any link with a net location
         # to match the current request so we don't have mixed link
         # protocols
         if self['netloc']:
             self['scheme'] = protocol
     return ParseResult(**self).geturl()
Пример #6
0
	def find_handler(self, path_info):
		"""Find the right page handler, and set request.config."""
		import routes

		request = cherrypy.request
		
		config = routes.request_config()
		config.mapper = self.mapper
		if hasattr(cherrypy.request, 'wsgi_environ'):
			config.environ = cherrypy.request.wsgi_environ

		config.host = request.headers.get('Host', None)
		config.protocol = request.scheme
		config.redirect = self.redirect

		result = self.mapper.match(path_info)
		_log.debug('find %s = %s' %(path_info,result))
		config.mapper_dict = result
		params = {}
		if result:
			params = result.copy()
		else:
			_log.debug("%s not in Routes" % path_info)

		if not self.full_result:
			params.pop('controller', None)
			params.pop('action', None)
			# routes add id attribute if when routes not explicit.
			# we remove id form result, to avoid attribute error
			if not params.get('id',None):
				params.pop('id', None)

		params.update(request.params)
		request.params.update(params)
Пример #7
0
    def dispatch(self, environ, start_response):
        # Request config must be initialized per request
        config = request_config()
        config.mapper = self.__mapper
        config.environ = environ
        request = self.requestFactory.getInstance(environ)
        response = self.responseFactory.getInstance()

        uri = ""
        if environ.has_key("REQUEST_URI"):
            uri = environ["REQUEST_URI"]
        else:
            uri = environ["PATH_INFO"]
        match = self.__mapper.match(uri)

        if isinstance(match, dict):
            controller = self.app_context.get_object(match["controller"])
            action = getattr(controller, match["action"])
            del (match["controller"])
            del (match["action"])

            if not hasattr(request, "form"):
                request.form = {}
            for item in match:
                request.form[item] = match[item]
            response = action(request, response, {})
        else:
            response.status = 500
            response.body_file.write("500 Internal Server Error")
        self.logger.debug(response.headerlist)
        start_response(response.status, response.headerlist)
        return [response.body]
Пример #8
0
    def options_handler(self, path, environ):
        """
        Generates a response for an OPTIONS request
        """
        mapper = request_config(original=False).mapper
        mapper.create_regs()

        full_path = '/' + path
        routes = list()
        for route in mapper.matchlist:
            match = route.match(full_path)
            if isinstance(match, dict) or match:
                routes.append(route)

        if len(routes) == 0:
            raise HTTPNotFound()

        allowed = set()
        for route in routes:
            if route.conditions and 'method' in route.conditions:
                allowed.update(set(route.conditions['method']))

        # If only this handler matches, consider this a Not Found
        if allowed == set(['OPTIONS']):
            raise HTTPNotFound()

        pylons.response.headers['Allow'] = ', '.join(allowed)
        return None
Пример #9
0
    def _getChannel(self, request):
        """Find and return an appropriate channel for the given request.

        The channel is found by consulting the Routes mapper to return a
        matching controller (target) and action (channel) along with a
        dictionary of additional parameters to be added to the request.
        """

        import routes

        path = request.path
        method = request.method.upper()
        request.index = path.endswith("/")

        # setup routes singleton config instance
        config = routes.request_config()
        config.mapper = self.mapper
        config.host = request.headers.get('Host', None)
        config.protocol = request.scheme

        # Redirect handler not supported at present
        config.redirect = None

        result = self.mapper.match(path)
        config.mapper_dict = result
        channel = None
        target = None
        vpath = []
        if result:
            target = self.controllers[result['controller']]
            channel = result["action"]
        return channel, target, vpath, result.copy()
Пример #10
0
    def _getChannel(self, request):
        """Find and return an appropriate channel for the given request.

        The channel is found by consulting the Routes mapper to return a
        matching controller (target) and action (channel) along with a
        dictionary of additional parameters to be added to the request.
        """

        import routes

        path = request.path
        method = request.method.upper()
        request.index = path.endswith("/")

        # setup routes singleton config instance
        config = routes.request_config()
        config.mapper = self.mapper
        config.host = request.headers.get('Host', None)
        config.protocol = request.scheme

        # Redirect handler not supported at present
        config.redirect = None

        result = self.mapper.match(path)
        config.mapper_dict = result
        channel = None
        target = None
        vpath = []
        if result:
            target = self.controllers[result['controller']]
            channel = result["action"]
        return channel, target, vpath, result.copy()
Пример #11
0
    def login_begin(self):
        """Step one of logging in with OpenID; we redirect to the provider"""

        cons = Consumer(session=session, store=self.openid_store)

        try:
            openid_url = request.params['openid_identifier']
        except KeyError:
            return self._bail("Gotta enter an OpenID to log in.")

        try:
            auth_request = cons.begin(openid_url)
        except DiscoveryFailure:
            return self._bail(
                "Can't connect to '{0}'.  You sure it's an OpenID?".format(
                    openid_url))

        sreg_req = SRegRequest(optional=[
            'nickname', 'email', 'dob', 'gender', 'country', 'language',
            'timezone'
        ])
        auth_request.addExtension(sreg_req)

        host = request.headers['host']
        protocol = request_config().protocol
        return_url = url(host=host,
                         controller='accounts',
                         action='login_finish')
        new_url = auth_request.redirectURL(return_to=return_url,
                                           realm=protocol + '://' + host)
        redirect(new_url)
Пример #12
0
    def __call__(self, environ):
        if not self._regs_created:
            self.mapper.create_regs([])
            self._regs_created = True
        path = environ.get('PATH_INFO', '/')
        self.mapper.environ = environ
        args = self.mapper.match(path)
        if args:
            context_factory = args.get('context_factory', _marker)
            if context_factory is _marker:
                context_factory = DefaultRoutesContext
            else:
                args = args.copy()
                del args['context_factory']
            config = request_config()
            config.mapper = self.mapper
            config.mapper_dict = args
            config.host = environ.get('HTTP_HOST', environ['SERVER_NAME'])
            config.protocol = environ['wsgi.url_scheme']
            config.redirect = None
            context = context_factory(**args)
            alsoProvides(context, IRoutesContext)
            return context

        # fall back to original get_root
        return self.get_root(environ)
Пример #13
0
def load_config(config: Any, load_site_user: bool = True):
    conf = _get_config(config)
    assert 'ckan' not in dir()  # otherwise loggers would be disabled
    # We have now loaded the config. Now we can import ckan for the
    # first time.
    from ckan.config.environment import load_environment
    load_environment(conf)

    # Set this internal test request context with the configured environment so
    # it can be used when calling url_for from the CLI.
    global _cli_test_request_context

    app = make_app(conf)
    flask_app = app.apps['flask_app']._wsgi_app
    _cli_test_request_context = flask_app.test_request_context()

    registry = Registry()
    registry.prepare()

    site_user = None
    if model.user_table.exists() and load_site_user:
        site_user = logic.get_action('get_site_user')({
            'ignore_auth': True
        }, {})

    ## give routes enough information to run url_for
    parsed = urlparse(cast(str, conf.get('ckan.site_url', 'http://0.0.0.0')))
    request_config = routes.request_config()
    request_config.host = parsed.netloc + parsed.path
    request_config.protocol = parsed.scheme

    return site_user
Пример #14
0
def get_session():
    """Return the current session from the environ provided by routes. A Pylons
    supported session is assumed. A KeyError is raised if one doesn't exist."""
    environ = request_config().environ
    session_key = environ['pylons.environ_config']['session']
    session = environ[session_key]
    return session
Пример #15
0
class AccountController(BaseController):
    geoip = GeoIP(os.path.join(config['pylons.paths']['data'], 'geoip.dat'))
    openid_store = FileOpenIDStore('/var/tmp')

    @require('guest')
    def login(self):
        login = render('account/login.tpl', slacks=True)

        if request.environ['REQUEST_METHOD'] != 'POST':
            return login

        try:
            form = LoginForm().to_python(request.POST)
        except validators.Invalid, e:
            return h.htmlfill(e, form=login)

        try:
            cons = Consumer(session=session, store=self.openid_store)
            auth_request = cons.begin(form['openid_identifier'])
            auth_request.addExtension(
                SRegRequest(optional=['nickname', 'email']))
        except DiscoveryFailure:
            h.flash(_('The specified URL is not a valid OpenID end-point.'),
                    'error')
            redirect(url(controller='account', action='login'))
        host = request.headers['host']
        realm = '%s://%s' % (request_config().protocol, host)
        return_url = url(host=host,
                         controller='account',
                         action='login_complete')
        new_url = auth_request.redirectURL(return_to=return_url, realm=realm)
        redirect(new_url)
Пример #16
0
def legacy_url_for(mapper, *args, **kwargs) -> str:
    """
    Re-establishes the mapper for legacy WSGI routes.
    """
    rc = request_config()
    rc.mapper = mapper
    return base.routes.url_for(*args, **kwargs)
Пример #17
0
def get_session():
    """Return the current session from the environ provided by routes. A Pylons
    supported session is assumed. A KeyError is raised if one doesn't exist."""
    environ = request_config().environ
    session_key = environ['pylons.environ_config']['session']
    session = environ[session_key]
    return session
Пример #18
0
    def login_begin(self):
        """Step one of logging in with OpenID; we redirect to the provider"""

        cons = Consumer(session=session, store=self.openid_store)

        try:
            openid_url = request.params['openid_identifier']
        except KeyError:
            return self._bail("Gotta enter an OpenID to log in.")

        try:
            auth_request = cons.begin(openid_url)
        except DiscoveryFailure:
            return self._bail(
                "Can't connect to '{0}'.  You sure it's an OpenID?"
                .format(openid_url)
            )

        sreg_req = SRegRequest(optional=['nickname', 'email', 'dob', 'gender',
                                         'country', 'language', 'timezone'])
        auth_request.addExtension(sreg_req)

        host = request.headers['host']
        protocol = request_config().protocol
        return_url = url(host=host, controller='accounts', action='login_finish')
        new_url = auth_request.redirectURL(return_to=return_url,
                                           realm=protocol + '://' + host)
        redirect(new_url)
Пример #19
0
 def handle_request(self, environ, start_response):
     # Grab the request_id (should have been set by middleware)
     request_id = environ.get('request_id', 'unknown')
     # Map url using routes
     path_info = environ.get('PATH_INFO', '')
     map = self.mapper.match(path_info, environ)
     if path_info.startswith('/api'):
         environ['is_api_request'] = True
         controllers = self.api_controllers
     else:
         environ['is_api_request'] = False
         controllers = self.controllers
     if map == None:
         raise httpexceptions.HTTPNotFound("No route for " + path_info)
     self.trace(path_info=path_info, map=map)
     # Setup routes
     rc = routes.request_config()
     rc.mapper = self.mapper
     rc.mapper_dict = map
     rc.environ = environ
     # Setup the transaction
     trans = self.transaction_factory(environ)
     trans.request_id = request_id
     rc.redirect = trans.response.send_redirect
     # Get the controller class
     controller_name = map.pop('controller', None)
     controller = controllers.get(controller_name, None)
     if controller_name is None:
         raise httpexceptions.HTTPNotFound("No controller for " + path_info)
     # Resolve action method on controller
     action = map.pop('action', 'index')
     # This is the easiest way to make the controller/action accessible for
     # url_for invocations.  Specifically, grids.
     trans.controller = controller_name
     trans.action = action
     method = getattr(controller, action, None)
     if method is None:
         method = getattr(controller, 'default', None)
     if method is None:
         raise httpexceptions.HTTPNotFound("No action for " + path_info)
     # Is the method exposed
     if not getattr(method, 'exposed', False):
         raise httpexceptions.HTTPNotFound("Action not exposed for " +
                                           path_info)
     # Is the method callable
     if not callable(method):
         raise httpexceptions.HTTPNotFound("Action not callable for " +
                                           path_info)
     # Combine mapper args and query string / form args and call
     kwargs = trans.request.params.mixed()
     kwargs.update(map)
     # Special key for AJAX debugging, remove to avoid confusing methods
     kwargs.pop('_', None)
     try:
         body = method(trans, **kwargs)
     except Exception, e:
         body = self.handle_controller_exception(e, trans, **kwargs)
         if not body:
             raise
Пример #20
0
def reset_cycle(name='default'):
    """
    Resets a cycle
    
    Resets the cycle so that it starts from the first element in the array
    the next time it is used.
    """
    del request_config().environ['railshelpers.cycles'][name]
Пример #21
0
 def test_dont_cdn_url_rewrite_for_unowned_link(self):
     '''Don't rewrite links on hosts we don't own'''
     request_config().protocol = "https"
     a_test_config = test_config.copy()
     a_test_config["USE_CDN"] = True
     pybald.configure(a_test_config)
     asset_url = AssetUrl(not_owned_static_resource)
     self.assertEqual(str(asset_url), str(not_owned_static_resource))
Пример #22
0
def current_url(*args, **kwargs):
    """Return the current page's url."""
    config = request_config()
    environ = config.environ
    qs = environ.get('QUERY_STRING', '')
    if qs:
        qs = '?' + qs
    return url_for(*args, **kwargs) + qs
Пример #23
0
 def test_dont_cdn_url_rewrite_for_unowned_link(self):
     '''Don't rewrite links on hosts we don't own'''
     request_config().protocol = "https"
     a_test_config = test_config.copy()
     a_test_config["USE_CDN"] = True
     pybald.configure(a_test_config)
     asset_url = AssetUrl(not_owned_static_resource)
     self.assertEqual(str(asset_url), str(not_owned_static_resource))
Пример #24
0
def reset_cycle(name='default'):
    """
    Resets a cycle
    
    Resets the cycle so that it starts from the first element in the array
    the next time it is used.
    """
    del request_config().environ['railshelpers.cycles'][name]
Пример #25
0
 def test_cdn_absolute_url_rewrite_http(self):
     '''Test that an absolute link gets changed to a CDN link'''
     request_config().protocol = "http"
     a_test_config = test_config.copy()
     a_test_config["USE_CDN"] = True
     pybald.configure(config_object=a_test_config)
     asset_url = AssetUrl(my_static_resource_explicit)
     self.assertEqual(str(asset_url), 'http://s0.sample.com/static_stuff.js')
Пример #26
0
def make_template():
    ''' In the following call, namespace is a dictionary of stuff for the templating
    engine ... which is why c is a (nearly) empty class, and h is the normal helper '''
    request_config().environ = request.environ
    return buffet.render('jinja',
        template_name="signin",
        namespace=dict(h=h, g=g, c=State())
    ).replace("%", "%%").replace("FORM_ACTION", "%s")
Пример #27
0
def current_url(*args, **kwargs):
    """Return the current page's url."""
    config = request_config()
    environ = config.environ
    qs = environ.get('QUERY_STRING', '')
    if qs:
        qs = '?' + qs
    return url_for(*args, **kwargs) + qs
Пример #28
0
 def __call__(self, environ, start_response):
     """
     Call interface as specified by WSGI. Wraps the environment in user
     friendly objects, finds the appropriate method to handle the request
     and calls it.
     """
     # Map url using routes
     path_info = environ.get('PATH_INFO', '')
     map = self.mapper.match(path_info, environ)
     if map is None:
         environ['is_api_request'] = True
         map = self.api_mapper.match(path_info, environ)
         mapper = self.api_mapper
         controllers = self.api_controllers
     else:
         mapper = self.mapper
         controllers = self.controllers
     if map == None:
         raise httpexceptions.HTTPNotFound("No route for " + path_info)
     # Setup routes
     rc = routes.request_config()
     rc.mapper = mapper
     rc.mapper_dict = map
     rc.environ = environ
     # Setup the transaction
     trans = self.transaction_factory(environ)
     rc.redirect = trans.response.send_redirect
     # Get the controller class
     controller_name = map.pop('controller', None)
     controller = controllers.get(controller_name, None)
     if controller_name is None:
         raise httpexceptions.HTTPNotFound("No controller for " + path_info)
     # Resolve action method on controller
     action = map.pop('action', 'index')
     method = getattr(controller, action, None)
     if method is None:
         method = getattr(controller, 'default', None)
     if method is None:
         raise httpexceptions.HTTPNotFound("No action for " + path_info)
     # Is the method exposed
     if not getattr(method, 'exposed', False):
         raise httpexceptions.HTTPNotFound("Action not exposed for " +
                                           path_info)
     # Is the method callable
     if not callable(method):
         raise httpexceptions.HTTPNotFound("Action not callable for " +
                                           path_info)
     # Combine mapper args and query string / form args and call
     kwargs = trans.request.params.mixed()
     kwargs.update(map)
     # Special key for AJAX debugging, remove to avoid confusing methods
     kwargs.pop('_', None)
     try:
         body = method(trans, **kwargs)
     except Exception, e:
         body = self.handle_controller_exception(e, trans, **kwargs)
         if not body:
             raise
Пример #29
0
    def __before__(self, action, **params):
        # This is done as a closure so it can just be called from the footer
        # in the template, putting off the final time() as late as possible
        start_time = time()

        def time_elapsed():
            return time() - start_time

        c.time_elapsed = time_elapsed
        c.query_log = QueryLog()

        c.config = Config().readdb(model.Config)
        c.empty_form = FormGenerator()
        c.error_msgs = []
        c.route = request_config().mapper_dict
        c.javascripts = ['jquery-1.2.6.pack', 'common']
        c.site_name = config.get('site_name', 'Ferrox')

        if 'user_id' in session:
            try:
                user_id = session['user_id']
                c.auth_user = model.Session.query(model.User) \
                    .options(eagerload('active_bans')) \
                    .get(user_id)
            except InvalidRequestError:
                # User may have been deleted in the interim
                del session['user_id']
                session.save()

        if c.auth_user:
            ip = request.environ['REMOTE_ADDR']
            ip = inet.pton(ip)
            if c.auth_user.can('admin.auth'):
                session['admin_ip'] = ip

            cip = inet.ntop(ip)

            # Log IPs
            ip_log_q = model.Session.query(model.IPLogEntry)
            last_ip_record = ip_log_q.filter_by(user_id = user_id) \
                .order_by(model.IPLogEntry.end_time.desc()).first()
            if last_ip_record and last_ip_record.ip == cip:
                last_ip_record.end_time = datetime.now()
            else:
                model.Session.add(model.IPLogEntry(user_id, ip))

            # Check to see if there are any active bans to expire
            if c.auth_user.active_bans:
                for ban in c.auth_user.active_bans:
                    if ban.expires <= datetime.now():
                        ban.expired = True
                        c.auth_user.role = ban.revert_to

            # Magical commit.
            model.Session.commit()

        else:
            c.auth_user = model.GuestUser()
Пример #30
0
    def _pagerlink(self, page, text):
        # Let the url_for() from webhelpers create a new link and set
        # the variable called 'page_param'. Example:
        # You are in '/foo/bar' (controller='foo', action='bar')
        # and you want to add a parameter 'page'. Then you
        # call the navigator method with page_param='page' and
        # the url_for() call will create a link '/foo/bar?page=...'
        # with the respective page number added.
        link_params = {}
        # Use the instance kwargs from Page.__init__ as URL parameters
        link_params.update(self.kwargs)
        # Add keyword arguments from pager() to the link as parameters
        link_params.update(self.pager_kwargs)
        link_params[self.page_param] = page

        # Get the URL generator
        if self._url_generator is not None:
            url_generator = self._url_generator
        else:
            try:
                import pylons
                url_generator = pylons.url.current
            except (ImportError, AttributeError):
                try:
                    import routes
                    url_generator = routes.url_for
                    config = routes.request_config()
                except (ImportError, AttributeError):
                    raise NotImplementedError("no URL generator available")
                else:
                    # if the Mapper is configured with explicit=True we have to fetch
                    # the controller and action manually
                    if config.mapper.explicit:
                        if hasattr(config, 'mapper_dict'):
                            for k, v in config.mapper_dict.items():
                                if k != self.page_param:
                                    link_params[k] = v

        # Create the URL to load a certain page
        link_url = url_generator(**link_params)

        if self.onclick:  # create link with onclick action for AJAX
            # Create the URL to load the page area part of a certain page (AJAX
            # updates)
            link_params[self.partial_param] = 1
            partial_url = url_generator(**link_params)
            try:  # if '%s' is used in the 'onclick' parameter (backwards compatibility)
                onclick_action = self.onclick % (partial_url,)
            except TypeError:
                onclick_action = Template(self.onclick).safe_substitute({
                    "partial_url": partial_url,
                    "page": page
                    })
            a_tag = HTML.a(text, href=link_url, onclick=onclick_action, **self.link_attr)
        else:  # return static link
            a_tag = HTML.a(text, href=link_url, **self.link_attr)
        li_tag = HTML.li(a_tag)
        return li_tag
Пример #31
0
 def handle_request( self, environ, start_response ):
     # Grab the request_id (should have been set by middleware)
     request_id = environ.get( 'request_id', 'unknown' )
     # Map url using routes
     path_info = environ.get( 'PATH_INFO', '' )
     map = self.mapper.match( path_info, environ )
     if path_info.startswith('/api'):
         environ[ 'is_api_request' ] = True
         controllers = self.api_controllers
     else:
         environ[ 'is_api_request' ] = False
         controllers = self.controllers
     if map is None:
         raise httpexceptions.HTTPNotFound( "No route for " + path_info )
     self.trace( path_info=path_info, map=map )
     # Setup routes
     rc = routes.request_config()
     rc.mapper = self.mapper
     rc.mapper_dict = map
     rc.environ = environ
     # Setup the transaction
     trans = self.transaction_factory( environ )
     trans.request_id = request_id
     rc.redirect = trans.response.send_redirect
     # Get the controller class
     controller_name = map.pop( 'controller', None )
     controller = controllers.get( controller_name, None )
     if controller_name is None:
         raise httpexceptions.HTTPNotFound( "No controller for " + path_info )
     # Resolve action method on controller
     action = map.pop( 'action', 'index' )
     # This is the easiest way to make the controller/action accessible for
     # url_for invocations.  Specifically, grids.
     trans.controller = controller_name
     trans.action = action
     method = getattr( controller, action, None )
     if method is None:
         method = getattr( controller, 'default', None )
     if method is None:
         raise httpexceptions.HTTPNotFound( "No action for " + path_info )
     # Is the method exposed
     if not getattr( method, 'exposed', False ):
         raise httpexceptions.HTTPNotFound( "Action not exposed for " + path_info )
     # Is the method callable
     if not callable( method ):
         raise httpexceptions.HTTPNotFound( "Action not callable for " + path_info )
     environ['controller_action_key'] = "%s.%s.%s" % ('api' if environ['is_api_request'] else 'web', controller_name, action or 'default')
     # Combine mapper args and query string / form args and call
     kwargs = trans.request.params.mixed()
     kwargs.update( map )
     # Special key for AJAX debugging, remove to avoid confusing methods
     kwargs.pop( '_', None )
     try:
         body = method( trans, **kwargs )
     except Exception, e:
         body = self.handle_controller_exception( e, trans, **kwargs )
         if not body:
             raise
Пример #32
0
def get_script_name():
    """
    Determine the current web application's ``SCRIPT_NAME``.
    """
    script_name = ''
    config = request_config()
    if hasattr(config, 'environ'):
        script_name = config.environ.get('SCRIPT_NAME', '')
    return script_name
Пример #33
0
 def test_cdn_url_rewrite_https(self):
     '''Test that the CDN rewrite is smart enough to use the raw CDN if
     https and we don't have https certs for static hosts.'''
     request_config().protocol = "https"
     a_test_config = test_config.copy()
     a_test_config["USE_CDN"] = True
     pybald.configure(config_object=a_test_config)
     asset_url = AssetUrl(my_static_resource)
     self.assertEqual(str(asset_url), 'https://awesomecdn.com/static_stuff.js')
Пример #34
0
 def test_cdn_absolute_url_rewrite_http(self):
     '''Test that an absolute link gets changed to a CDN link'''
     request_config().protocol = "http"
     a_test_config = test_config.copy()
     a_test_config["USE_CDN"] = True
     pybald.configure(config_object=a_test_config)
     asset_url = AssetUrl(my_static_resource_explicit)
     self.assertEqual(str(asset_url),
                      'http://s0.sample.com/static_stuff.js')
Пример #35
0
    def rss(self, posts):
        if not posts:
            h.flash('There are no posts to show an RSS Feed for.', 'warning')
            redirect(url(controller='blog', action='index'))

        author = model.User.first()
        host = request_config().host
        feed_kwargs = {}
        if author.email:
            feed_kwargs['author_email'] = author.email

        feed = Atom1Feed(
            title=config['rss.title'],
            description=config['rss.description'],
            link=url(host=host, controller='blog', action='index'),
            author_name=author.name,
            author_link=url(
                host=host,
                controller='account',
                action='profile',
                id=author.id
            ),
            **feed_kwargs
        )
        for post in c.posts:
            item_kwargs = {}
            if post.user.email:
                item_kwargs['author_email'] = post.user.email

            if post.summary:
                description = post.summary
            else:
                description = _('No Summary')

            feed.add_item(
                title=post.title,
                link=url(
                    host=host,
                    controller='blog',
                    action='view',
                    category=post.category.slug,
                    slug=post.slug
                ),
                description=description,
                pubdate=post.posted,
                author_name=post.user.name,
                author_link=url(
                    host=host,
                    controller='account',
                    action='profile',
                    id=post.user.id
                ),
                **item_kwargs
            )
        response.content_type = 'application/atom+xml'
        return feed.writeString('utf-8')
Пример #36
0
 def handle_request(self, environ, start_response, body_renderer=None):
     # Grab the request_id (should have been set by middleware)
     request_id = environ.get('request_id', 'unknown')
     # Map url using routes
     path_info = environ.get('PATH_INFO', '')
     client_match = self.clientside_routes.match(path_info, environ)
     map_match = self.mapper.match(path_info, environ) or client_match
     if path_info.startswith('/api'):
         environ['is_api_request'] = True
         controllers = self.api_controllers
     else:
         environ['is_api_request'] = False
         controllers = self.controllers
     if map_match is None:
         raise webob.exc.HTTPNotFound("No route for " + path_info)
     self.trace(path_info=path_info, map_match=map_match)
     # Setup routes
     rc = routes.request_config()
     rc.mapper = self.mapper
     rc.mapper_dict = map_match
     rc.environ = environ
     # Setup the transaction
     trans = self.transaction_factory(environ)
     trans.request_id = request_id
     rc.redirect = trans.response.send_redirect
     # Resolve mapping to controller/method
     try:
         # We don't use default methods if there's a clientside match for this route.
         use_default = client_match is None
         controller_name, controller, action, method = self._resolve_map_match(
             map_match, path_info, controllers, use_default=use_default)
     except webob.exc.HTTPNotFound:
         # Failed, let's check client routes
         if not environ['is_api_request'] and client_match is not None:
             controller_name, controller, action, method = self._resolve_map_match(
                 client_match, path_info, controllers)
         else:
             raise
     trans.controller = controller_name
     trans.action = action
     environ['controller_action_key'] = "%s.%s.%s" % (
         'api' if environ['is_api_request'] else 'web', controller_name,
         action or 'default')
     # Combine mapper args and query string / form args and call
     kwargs = trans.request.params.mixed()
     kwargs.update(map_match)
     # Special key for AJAX debugging, remove to avoid confusing methods
     kwargs.pop('_', None)
     try:
         body = method(trans, **kwargs)
     except Exception as e:
         body = self.handle_controller_exception(e, trans, **kwargs)
         if not body:
             raise
     body_renderer = body_renderer or self._render_body
     return body_renderer(trans, body, environ, start_response)
Пример #37
0
    def __before__(self, action, **params):
        # This is done as a closure so it can just be called from the footer
        # in the template, putting off the final time() as late as possible
        start_time = time()
        def time_elapsed():
            return time() - start_time
        c.time_elapsed = time_elapsed
        c.query_log = QueryLog()

        c.config = Config().readdb(model.Config)
        c.empty_form = FormGenerator()
        c.error_msgs = []
        c.route = request_config().mapper_dict
        c.javascripts = ['jquery-1.2.6.pack', 'common']
        c.site_name = config.get('site_name', 'Ferrox')

        if 'user_id' in session:
            try:
                user_id = session['user_id']
                c.auth_user = model.Session.query(model.User) \
                    .options(eagerload('active_bans')) \
                    .get(user_id)
            except InvalidRequestError:
                # User may have been deleted in the interim
                del session['user_id']
                session.save()

        if c.auth_user:
            ip = request.environ['REMOTE_ADDR']
            ip = inet.pton(ip)
            if c.auth_user.can('admin.auth'):
                session['admin_ip'] = ip

            cip = inet.ntop(ip)

            # Log IPs
            ip_log_q = model.Session.query(model.IPLogEntry)
            last_ip_record = ip_log_q.filter_by(user_id = user_id) \
                .order_by(model.IPLogEntry.end_time.desc()).first()
            if last_ip_record and last_ip_record.ip == cip:
                last_ip_record.end_time = datetime.now()
            else:
                model.Session.add(model.IPLogEntry(user_id, ip))

            # Check to see if there are any active bans to expire
            if c.auth_user.active_bans:
                for ban in c.auth_user.active_bans:
                    if ban.expires <= datetime.now():
                        ban.expired = True
                        c.auth_user.role = ban.revert_to

            # Magical commit.
            model.Session.commit()

        else:
            c.auth_user = model.GuestUser()
Пример #38
0
 def __call__( self, environ, start_response ):
     """
     Call interface as specified by WSGI. Wraps the environment in user
     friendly objects, finds the appropriate method to handle the request
     and calls it.
     """
     # Map url using routes
     path_info = environ.get( 'PATH_INFO', '' )
     map = self.mapper.match( path_info, environ )
     if map is None:
         environ[ 'is_api_request' ] = True
         map = self.api_mapper.match( path_info, environ )
         mapper = self.api_mapper
         controllers = self.api_controllers
     else:
         mapper = self.mapper
         controllers = self.controllers
     if map == None:
         raise httpexceptions.HTTPNotFound( "No route for " + path_info )
     # Setup routes
     rc = routes.request_config()
     rc.mapper = mapper
     rc.mapper_dict = map
     rc.environ = environ
     # Setup the transaction
     trans = self.transaction_factory( environ )
     rc.redirect = trans.response.send_redirect
     # Get the controller class
     controller_name = map.pop( 'controller', None )
     controller = controllers.get( controller_name, None )
     if controller_name is None:
         raise httpexceptions.HTTPNotFound( "No controller for " + path_info )
     # Resolve action method on controller
     action = map.pop( 'action', 'index' )
     method = getattr( controller, action, None )
     if method is None:
         method = getattr( controller, 'default', None )
     if method is None:
         raise httpexceptions.HTTPNotFound( "No action for " + path_info )
     # Is the method exposed
     if not getattr( method, 'exposed', False ):
         raise httpexceptions.HTTPNotFound( "Action not exposed for " + path_info )
     # Is the method callable
     if not callable( method ):
         raise httpexceptions.HTTPNotFound( "Action not callable for " + path_info ) 
     # Combine mapper args and query string / form args and call
     kwargs = trans.request.params.mixed()
     kwargs.update( map )
     # Special key for AJAX debugging, remove to avoid confusing methods
     kwargs.pop( '_', None )
     try:
         body = method( trans, **kwargs )
     except Exception, e:
         body = self.handle_controller_exception( e, trans, **kwargs )
         if not body:
             raise
Пример #39
0
def reset_counter(name='default'):
    """Resets a counter.

    Resets the counter so that it starts from the ``start`` cardinal in
    the sequence next time it is used.
    """
    try:
        del request_config().environ['railshelpers.counters'][name]
    except KeyError:
        pass
Пример #40
0
 def test_cdn_url_rewrite_https(self):
     '''Test that the CDN rewrite is smart enough to use the raw CDN if
     https and we don't have https certs for static hosts.'''
     request_config().protocol = "https"
     a_test_config = test_config.copy()
     a_test_config["USE_CDN"] = True
     pybald.configure(config_object=a_test_config)
     asset_url = AssetUrl(my_static_resource)
     self.assertEqual(str(asset_url),
                      'https://awesomecdn.com/static_stuff.js')
Пример #41
0
def reset_counter(name='default'):
    """Resets a counter.

    Resets the counter so that it starts from the ``start`` cardinal in
    the sequence next time it is used.
    """
    try:
        del request_config().environ['railshelpers.counters'][name]
    except KeyError:
        pass
Пример #42
0
    def route(self):
        """Return the parsed route for the current request.

        Most route information is available in the request parameters, but the
        controller and action components get stripped out early.
        """
        if not self._route:
            mapper = routes.request_config().mapper
            self._route = mapper.match(self.request.path_info)
        return self._route
Пример #43
0
def current_url():
    """
    Returns the current page's url.
    """
    config = request_config()
    environ = config.environ
    curopts = config.mapper_dict.copy()
    if environ.get('REQUEST_METHOD', 'GET') == 'GET':
        if environ.has_key('QUERY_STRING'):
            curopts.update(dict(parse_querystring(environ)))
    return url_for(**curopts)
Пример #44
0
 def setUp(self):
     m = routes.Mapper(explicit=False)
     m.minimization = True
     m.connect('archive/:year/:month/:day', controller='blog', action='view', month=None, day=None,
               requirements={'month':'\d{1,2}','day':'\d{1,2}'})
     m.connect('viewpost/:id', controller='post', action='view')
     m.connect(':controller/:action/:id')
     m.create_regs(['content', 'blog'])
     con = routes.request_config()
     con.mapper = m
     self.con = con
Пример #45
0
def current_url():
    """
    Returns the current page's url.
    """
    config = request_config()
    environ = config.environ
    curopts = config.mapper_dict.copy()
    if environ.get('REQUEST_METHOD', 'GET') == 'GET':
        if environ.has_key('QUERY_STRING'):
            curopts.update(dict(parse_querystring(environ)))
    return url_for(**curopts)
Пример #46
0
 def setUp(self):
     m = routes.Mapper(explicit=False)
     m.minimization = True
     m.connect('archive/:year/:month/:day', controller='blog', action='view', month=None, day=None,
               requirements={'month':'\d{1,2}','day':'\d{1,2}'})
     m.connect('viewpost/:id', controller='post', action='view')
     m.connect(':controller/:action/:id')
     m.create_regs(['content', 'blog'])
     con = routes.request_config()
     con.mapper = m
     self.con = con
Пример #47
0
 def test_request_config(self):
     orig_config = request_config()
     class Obby(object): pass
     myobj = Obby()
     class MyCallable(object):
         def __init__(self):
             class Obby(object): pass
             self.obj = myobj
         
         def __call__(self):
             return self.obj
     
     mycall = MyCallable()
     if hasattr(orig_config, 'using_request_local'):
         orig_config.request_local = mycall 
         config = request_config()
     assert id(myobj) == id(config)
     old_config = request_config(original=True)
     assert issubclass(old_config.__class__, _RequestConfig) is True
     del orig_config.request_local
Пример #48
0
 def test_request_config(self):
     orig_config = request_config()
     class Obby(object): pass
     myobj = Obby()
     class MyCallable(object):
         def __init__(self):
             class Obby(object): pass
             self.obj = myobj
         
         def __call__(self):
             return self.obj
     
     mycall = MyCallable()
     if hasattr(orig_config, 'using_request_local'):
         orig_config.request_local = mycall 
         config = request_config()
     assert id(myobj) == id(config)
     old_config = request_config(original=True)
     assert issubclass(old_config.__class__, _RequestConfig) is True
     del orig_config.request_local
Пример #49
0
def redirect_to(*args, **kargs):
    """Issues a redirect based on the arguments. 
    
    Redirect's *should* occur as a "302 Moved" header, however the web 
    framework may utilize a different method.
    
    All arguments are passed to url_for to retrieve the appropriate URL, then
    the resulting URL it sent to the redirect function as the URL.
    """
    target = url_for(*args, **kargs)
    config = request_config()
    return config.redirect(target)
Пример #50
0
def redirect_to(*args, **kargs):
    """Issues a redirect based on the arguments. 
    
    Redirect's *should* occur as a "302 Moved" header, however the web 
    framework may utilize a different method.
    
    All arguments are passed to url_for to retrieve the appropriate URL, then
    the resulting URL it sent to the redirect function as the URL.
    """
    target = url_for(*args, **kargs)
    config = request_config()
    return config.redirect(target)
Пример #51
0
def load_config(ckan_ini_filepath):
    import paste.deploy
    config_abs_path = os.path.abspath(ckan_ini_filepath)
    conf = paste.deploy.appconfig('config:' + config_abs_path)
    import ckan
    ckan.config.environment.load_environment(conf.global_conf, conf.local_conf)

    ## give routes enough information to run url_for
    parsed = urlparse(conf.get('ckan.site_url', 'http://0.0.0.0'))
    request_config = routes.request_config()
    request_config.host = parsed.netloc + parsed.path
    request_config.protocol = parsed.scheme
Пример #52
0
 def handle_request( self, environ, start_response, body_renderer=None ):
     # Grab the request_id (should have been set by middleware)
     request_id = environ.get( 'request_id', 'unknown' )
     # Map url using routes
     path_info = environ.get( 'PATH_INFO', '' )
     client_match = self.clientside_routes.match( path_info, environ )
     map_match = self.mapper.match( path_info, environ ) or client_match
     if path_info.startswith('/api'):
         environ[ 'is_api_request' ] = True
         controllers = self.api_controllers
     else:
         environ[ 'is_api_request' ] = False
         controllers = self.controllers
     if map_match is None:
         raise httpexceptions.HTTPNotFound( "No route for " + path_info )
     self.trace( path_info=path_info, map_match=map_match )
     # Setup routes
     rc = routes.request_config()
     rc.mapper = self.mapper
     rc.mapper_dict = map_match
     rc.environ = environ
     # Setup the transaction
     trans = self.transaction_factory( environ )
     trans.request_id = request_id
     rc.redirect = trans.response.send_redirect
     # Resolve mapping to controller/method
     try:
         # We don't use default methods if there's a clientside match for this route.
         use_default = client_match is None
         controller_name, controller, action, method = self._resolve_map_match( map_match, path_info, controllers, use_default=use_default)
     except httpexceptions.HTTPNotFound:
         # Failed, let's check client routes
         if not environ[ 'is_api_request' ] and client_match is not None:
             controller_name, controller, action, method = self._resolve_map_match( client_match, path_info, controllers )
         else:
             raise
     trans.controller = controller_name
     trans.action = action
     environ['controller_action_key'] = "%s.%s.%s" % ('api' if environ['is_api_request'] else 'web', controller_name, action or 'default')
     # Combine mapper args and query string / form args and call
     kwargs = trans.request.params.mixed()
     kwargs.update( map_match )
     # Special key for AJAX debugging, remove to avoid confusing methods
     kwargs.pop( '_', None )
     try:
         body = method( trans, **kwargs )
     except Exception as e:
         body = self.handle_controller_exception( e, trans, **kwargs )
         if not body:
             raise
     body_renderer = body_renderer or self._render_body
     return body_renderer( trans, body, environ, start_response )
Пример #53
0
def load_config(ckan_ini_filepath):
    import paste.deploy
    config_abs_path = os.path.abspath(ckan_ini_filepath)
    conf = paste.deploy.appconfig('config:' + config_abs_path)
    import ckan
    ckan.config.environment.load_environment(conf.global_conf,
                                             conf.local_conf)

    # give routes enough information to run url_for
    parsed = urlparse.urlparse(conf.get('ckan.site_url', 'http://0.0.0.0'))
    request_config = routes.request_config()
    request_config.host = parsed.netloc + parsed.path
    request_config.protocol = parsed.scheme
Пример #54
0
def routing_hook(connection, sessionDict):
    if not Cfg.MvcOn:
        return
    debug("in routing hook")
    # initialize routes request config
    rcfg = request_config()
    rcfg.redirect = _do_redirect
    rcfg.mapper = map = Mapper()
    rcfg.host = connection.host
    if connection.env.get('HTTPS', False):
        rcfg.protocol = 'HTTPS'
    else:
        rcfg.protocol = 'HTTP'
    env = connection.env.copy()
    env['wsgi.version'] = (1, 0)
    env['wsgi.multithread'] = False
    env['wsgi.multiprocess'] = True
    env['wsgi.run_once'] = False
    env['wsgi.url_scheme'] = 'https' if env.get(
        'HTTPS', 'off') in ('on', '1') else 'http'
    env['SCRIPT_NAME'] = ''
    rcfg.environ = env

    # initialize the map
    for r in Cfg.routes:
        if isinstance(r, dict):
            map.connect(**r)
        elif isinstance(r, (list, tuple)):
            if (len(r) == 2 and isinstance(r[0], (list, tuple))
                    and isinstance(r[1], dict)):
                map.connect(*r[0], **r[1])
            else:
                map.connect(*r)
        else:
            raise ValueError, "wrong arguments for connect()"

    map.create_regs(Cfg.controllers.keys())

    # test the url with the map
    res = rcfg.mapper_dict = map.match(connection.uri) or {}
    debug("performed routing match")
    # put the result if the session dict
    if res:
        debug("have routing match!")
        res = res.copy()
        debug("result dict: %s", res)
        controller = res.pop('controller', None)
        action = res.pop('action', None)
        sessionDict['CONTROLLER'] = controller
        sessionDict['ACTION'] = action
        sessionDict['CONTROLLER_ARGS'] = res
Пример #55
0
def routing_hook(connection, sessionDict):
    if not Cfg.MvcOn:
        return
    debug("in routing hook")
    # initialize routes request config
    rcfg=request_config()
    rcfg.redirect=_do_redirect
    rcfg.mapper=map=Mapper()
    rcfg.host=connection.host
    if connection.env.get('HTTPS', False):
        rcfg.protocol='HTTPS'
    else:
        rcfg.protocol='HTTP'
    env=connection.env.copy()
    env['wsgi.version']=(1,0)
    env['wsgi.multithread']=False
    env['wsgi.multiprocess']=True
    env['wsgi.run_once']=False
    env['wsgi.url_scheme']='https' if env.get('HTTPS', 'off') in ('on', '1') else 'http'
    env['SCRIPT_NAME']=''
    rcfg.environ=env
        
    # initialize the map
    for r in Cfg.routes:
        if isinstance(r, dict):
            map.connect(**r)
        elif isinstance(r, (list, tuple)):
            if (len(r)==2 and
                isinstance(r[0], (list, tuple)) and
                isinstance(r[1], dict)):
                map.connect(*r[0], **r[1])
            else:
                map.connect(*r)
        else:
            raise ValueError, "wrong arguments for connect()"

    map.create_regs(Cfg.controllers.keys())

    # test the url with the map
    res=rcfg.mapper_dict=map.match(connection.uri) or {}
    debug("performed routing match")
    # put the result if the session dict
    if res:
        debug("have routing match!")
        res=res.copy()
        debug("result dict: %s", res)
        controller=res.pop('controller', None)
        action=res.pop('action', None)
        sessionDict['CONTROLLER']=controller
        sessionDict['ACTION']=action
        sessionDict['CONTROLLER_ARGS']=res
Пример #56
0
def cycle(*args, **kargs):
    """
    Return the next cycle of the given list.
    
    Everytime ``cycle`` is called, the value returned will be the next 
    item in the list passed to it. This list is reset on every request, 
    but can also be reset by calling ``reset_cycle()``.
    
    You may specify the list as either arguments, or as a single list 
    argument.
    
    This can be used to alternate classes for table rows::
    
        # In Myghty...
        % for item in items:
        <tr class="<% cycle("even", "odd") %>">
            ... use item ...
        </tr>
        % #endfor
    
    You can use named cycles to prevent clashes in nested loops. You'll
    have to reset the inner cycle, manually::
    
        % for item in items:
        <tr class="<% cycle("even", "odd", name="row_class") %>
            <td>
        %     for value in item.values:
                <span style="color:'<% cycle("red", "green", "blue",
                                             name="colors") %>'">
                            item
                </span>
        %     #endfor
            <% reset_cycle("colors") %>
            </td>
        </tr>
        % #endfor
        
    """
    if len(args) > 1:
        items = args
    else:
        items = args[0]
    name = kargs.get('name', 'default')
    cycles = request_config().environ.setdefault('railshelpers.cycles', {})

    cycle = cycles.setdefault(name, iterdict(items))

    if cycles[name].get('items') != items:
        cycle = cycles[name] = iterdict(items)
    return cycle['iter'].next()
Пример #57
0
    def FindRoute(self, request):

        config = request_config()
        config.mapper = self._routes
        # route matching
        # -- Assignment of environ to config triggers route matching
        config.environ = request.environ

        if config.mapper_dict is None:
            # log.error( u"Cannot find route for: '%s'" % ( request.environ.get('PATH_INFO', '<unset PATH_INFO>') ) );
            return None

        # request.environ['pylons.routes_dict'] = request.ROUTE
        # request.environ['wsgiorg.routing_args'] = ((), match)
        request.environ['routes.route'] = config.route
        return config