def help_url(page, anchor=None): if adhocracy_service.instance_staticpages_api_address(): url = base_url('/static/%s.%s') else: url = base_url('/static/%s.%s', None) if anchor is not None: url += "#" + anchor return url % (page, 'html')
def icon_url(proposal, size=16): if proposal.adopted: return (base_url(None, path='') + u"/img/icons/proposal_adopted_" + str(size) + u".png") if proposal.is_adopt_polling(): return (base_url(None, path='') + u"/img/icons/vote_" + str(size) + u".png") else: return (base_url(None, path='') + u"/img/icons/proposal_" + str(size) + u".png")
def root(): if c.instance: from instance_helper import url return link(c.instance.label, url(c.instance)) + BREAD_SEP else: return link(site.name(), site.base_url(None)) + BREAD_SEP
def help_link(text, page, anchor=None): url = base_url(None, path="/static/%s.%s") if anchor is not None: url += "#" + anchor full_url = url % (page, 'html') return (u"<a target='_new' class='staticlink_%s' href='%s' " u">%s</a>") % (page, full_url, text)
def build(instance, base, id, query=None, anchor=None, member=None, format=None, absolute=False): ''' Build an url which will be placed in the subdomain of the *instance*'. The url will be composed out of *base* and 'id', point to the html id *anchor*. ''' if base: base = '/' + base + '/' else: base = u'/' id = id.decode('utf-8') if isinstance(id, str) else unicode(id) _path = base + id url = site.base_url(_path, instance, absolute=absolute) url = append_member_and_format(url, member, format) if query is not None: url += '&' if '?' in url else '?' u = lambda s: unicode(s).encode('utf-8') query = [(u(k), u(v)) for (k, v) in query.iteritems()] url += urllib.urlencode(query) if anchor is not None: url += "#" + anchor return url
def build(instance, base, id, query=None, anchor=None, member=None, format=None): ''' Build an url which will be placed in the subdomain of the *instance*'. The url will be composed out of *base* and 'id', point to the html id *anchor*. ''' if base: base = '/' + base + '/' else: base = u'/' id = id.decode('utf-8') if isinstance(id, str) else unicode(id) _path = base + id url = site.base_url(instance, path=_path) url = append_member_and_format(url, member, format) if anchor is not None: url += "#" + anchor if query is not None: for k, v in query.items(): key = unicode(k).encode('ascii', 'ignore') query[key] = unicode(v).encode('utf-8') url = url + u'?' + unicode(urllib.urlencode(query)) return url
def __call__(self, environ, start_response): relative_urls = asbool(self.config.get('adhocracy.relative_urls', 'false')) environ['adhocracy.domain'] = self.domain instance_key = self.config.get('adhocracy.instance') if instance_key is None: instance_key = self.domains_instances.get(environ.get('HTTP_HOST')) if instance_key is None: if relative_urls: path = environ.get('PATH_INFO', '') if path.startswith('/i/'): instance_key = path.split('/')[2] environ['PATH_INFO'] = path[len('/i/' + instance_key):] if environ['PATH_INFO'] == '': response = Response() if instance_key == '': response.status_int = 404 # Double slashes are stripped, so we can't redirect # to /i// return response(environ, start_response) response.status_int = 302 response.headers['location'] = path + '/' return response(environ, start_response) else: host = environ.get('HTTP_HOST', "") host = host.replace(self.domain, "") host = host.replace('localhost', "") host = host.split(':', 1)[0] host = host.strip('.').strip() instance_key = host if instance_key: # instance key is set (neither None nor "") instance = model.Instance.find(instance_key) if instance is None: response = Response() if not relative_urls: # HTTP_HOST needs to be set to an existing domain, # otherwise we end up here again after being internally # redirected from StatusCodeRedirect and produce a white # page. environ['HTTP_HOST'] = self.domain # Fair handling of users prefixing everything with www. if instance_key == 'www': response.status_int = 301 response.headers['location'] = \ base_url(environ.get('PATH_INFO', '/'), absolute=True, config=self.config) return response(environ, start_response) response.status_int = 404 return response(environ, start_response) else: model.instance_filter.setup_thread(instance) try: return self.app(environ, start_response) finally: model.instance_filter.setup_thread(None)
def link_with_untag(tag, delegateable, simple=True): tag_link = link(tag, simple=simple) if can.instance.edit(c.instance): return '%s (%s)' % (tag_link, '<a href="%s?tag=%d&delegateable=%d&%s">%s</a>' % (base_url('/untag_all'), tag.id, delegateable.id, url_token(), _('delete'))) else: return tag_link
def build(instance, base, id, query=None, anchor=None, **kwargs): base = '/' + base + '/' id = id.decode('utf-8') if isinstance(id, str) else unicode(id) _path = base + id url = site.base_url(instance, path=_path) url = append_member_and_format(url, **kwargs) if anchor is not None: url += "#" + anchor if query is not None: for k, v in query.items(): key = unicode(k).encode('ascii', 'ignore') query[key] = unicode(v).encode('utf-8') url = url + u'?' + unicode(urllib.urlencode(query)) return url
def build(instance, base, id, query=None, anchor=None, **kwargs): base = "/" + base + "/" id = id.decode("utf-8") if isinstance(id, str) else unicode(id) _path = base + id url = site.base_url(instance, path=_path) url = append_member_and_format(url, **kwargs) if anchor is not None: url += "#" + anchor if query is not None: for k, v in query.items(): key = unicode(k).encode("ascii", "ignore") query[key] = unicode(v).encode("utf-8") url = url + u"?" + unicode(urllib.urlencode(query)) return url
def link_with_untag(tag, delegateable, simple=True): tag_link = link(tag, simple=simple) if can.instance.edit(c.instance): return '%s (%s)' % ( tag_link, '<a href="%s?tag=%d&delegateable=%d&%s">%s</a>' % ( base_url('/untag_all'), tag.id, delegateable.id, url_token(), _('delete') ) ) else: return tag_link
def register_redirect_url(entity=None, **kwargs): ''' Builds an ".../login?came_from=http...." pointing to the /login form in the current instance domain. If ``entity`` is set, this will redirect to the given entity after successful login. If ``entity`` is None, it will redirect to the current URL. ''' if entity is None: came_from_url = base_url(request.path) else: came_from_url = entity_url(entity, **kwargs) login_url = build(c.instance, '', 'register', query={'came_from': came_from_url}) return login_url
def get_redirect_url(target=u'login', entity=None, **kwargs): ''' Builds an URL similar to ".../login?came_from=http...." pointing to the target path in the current instance domain. If we already have a ``came_from`` parameter in the path, this is going to be used as the new ``came_from`` target. Otherwise, if ``entity`` is set, this will redirect to the given entity after successful login. If ``entity`` is None, it will redirect to the current URL. ''' if c.came_from == u'': if entity is None: c.came_from = base_url(request.path, query_string=request.query_string) else: c.came_from = entity_url(entity, **kwargs) return build(c.instance, '', target, query={'came_from': c.came_from})
def build(instance, base, id, query=None, anchor=None, member=None, format=None, absolute=False): ''' Build an url which will be placed in the subdomain of the *instance*'. The url will be composed out of *base* and 'id', point to the html id *anchor*. ''' if base: base = '/' + base + '/' else: base = u'/' id = id.decode('utf-8') if isinstance(id, str) else unicode(id) _path = base + id url = site.base_url(_path, instance, absolute=absolute) url = append_member_and_format(url, member, format) if query is not None: url += '&' if '?' in url else '?' url += urllib.urlencode(query) if anchor is not None: url += "#" + anchor return url
def build(instance, base, id, query=None, anchor=None, member=None, format=None): """ Build an url which will be placed in the subdomain of the *instance*'. The url will be composed out of *base* and 'id', point to the html id *anchor*. """ if base: base = "/" + base + "/" else: base = u"/" id = id.decode("utf-8") if isinstance(id, str) else unicode(id) _path = base + id url = site.base_url(_path, instance) url = append_member_and_format(url, member, format) if anchor is not None: url += "#" + anchor if query is not None: for k, v in query.items(): key = unicode(k).encode("ascii", "ignore") query[key] = unicode(v).encode("utf-8") url = url + u"?" + unicode(urllib.urlencode(query)) return url
def icon_url(size=16): return (base_url(None, path='') + u"/img/icons/milestone_" + str(size) + u".png")
def make_app(global_conf, full_stack=True, static_files=True, **app_conf): """Create a Pylons WSGI application and return it ``global_conf`` The inherited configuration for this application. Normally from the [DEFAULT] section of the Paste ini file. ``full_stack`` Whether this application provides a full WSGI stack (by default, meaning it handles its own exceptions and errors). Disable full_stack when this application is "managed" by another WSGI middleware. ``static_files`` Whether this application serves its own static files; disable when another web server is responsible for serving them. ``app_conf`` The application's local configuration. Normally specified in the [app:<name>] section of the Paste ini file (where <name> defaults to main). """ debug = asbool(global_conf.get("debug", False)) or asbool(app_conf.get("debug", False)) # Configure the Pylons environment config = load_environment(global_conf, app_conf) # The Pylons WSGI app app = PylonsApp(config=config) if debug and asbool(app_conf.get("adhocracy.enable_profiling", False)): from werkzeug.contrib.profiler import ProfilerMiddleware app = ProfilerMiddleware(app, sort_by=("ncalls",), restrictions=("src/adhocracy/.*", 25)) # Routing/Session/Cache Middleware app = RoutesMiddleware(app, config["routes.map"]) if config.get("adhocracy.session.implementation", "beaker") == "cookie": app = CookieSessionMiddleware(app, config) else: app = beaker.middleware.SessionMiddleware(app, config) app = beaker.middleware.CacheMiddleware(app, config) # app = make_profile_middleware(app, config, log_filename='profile.log.tmp') # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares) app = setup_auth(app, config) app = make_prefix_middleware(app, config, scheme=config.get("adhocracy.protocol", "http")) app = setup_discriminator(app, config) if asbool(config.get("adhocracy.requestlog_active", "False")): app = RequestLogger(app, config) if asbool(full_stack): # Handle Python exceptions app = ErrorHandler(app, global_conf, **config["pylons.errorware"]) # Display error documents for 401, 403, 404 status codes app = StatusCodeRedirect(app, [400, 401, 403, 404, 500]) # Establish the Registry for this application app = RegistryManager(app) if asbool(static_files): cache_age = int(config.get("adhocracy.static.age", 7200)) # Serve static files overlay_app = StaticURLParser( get_site_path("static", app_conf=config), cache_max_age=None if debug else cache_age ) static_app = StaticURLParser(config["pylons.paths"]["static_files"], cache_max_age=None if debug else cache_age) app = Cascade([overlay_app, static_app, app]) # Fanstatic inserts links for javascript and css ressources. # The required resources can be specified at runtime with <resource>.need() # and can will be delivered with version specifiers in the url and # minified when not in debug mode. fanstatic_base_url = base_url("", instance=None, config=config).rstrip("/") app = Fanstatic( app, minified=not (debug), versioning=True, recompute_hashes=debug, bundle=not (debug), base_url=fanstatic_base_url, bottom=True, ) if asbool(config.get("adhocracy.include_machine_name_in_header", "false")): app = IncludeMachineName(app, config) app = CorsMiddleware(app, config) app.config = config return app
def make_app(global_conf, full_stack=True, static_files=True, **app_conf): """Create a Pylons WSGI application and return it ``global_conf`` The inherited configuration for this application. Normally from the [DEFAULT] section of the Paste ini file. ``full_stack`` Whether this application provides a full WSGI stack (by default, meaning it handles its own exceptions and errors). Disable full_stack when this application is "managed" by another WSGI middleware. ``static_files`` Whether this application serves its own static files; disable when another web server is responsible for serving them. ``app_conf`` The application's local configuration. Normally specified in the [app:<name>] section of the Paste ini file (where <name> defaults to main). """ debug = (asbool(global_conf.get('debug', False)) or asbool(app_conf.get('debug', False))) # Configure the Pylons environment load_environment(global_conf, app_conf) # The Pylons WSGI app app = PylonsApp() # Routing/Session/Cache Middleware app = RoutesMiddleware(app, config['routes.map']) app = SessionMiddleware(app, config) app = CacheMiddleware(app, config) #app = make_profile_middleware(app, config, log_filename='profile.log.tmp') # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares) app = setup_auth(app, config) app = setup_discriminator(app, config) if asbool(full_stack): # Handle Python exceptions app = ErrorHandler(app, global_conf, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (and # 500 when debug is disabled) if debug: app = StatusCodeRedirect(app) else: app = StatusCodeRedirect(app, [400, 401, 403, 404, 500]) # Establish the Registry for this application app = RegistryManager(app) if asbool(static_files): cache_age = int(config.get('adhocracy.static.age', 7200)) # Serve static files overlay_app = StaticURLParser(get_site_path('static'), cache_max_age=None if debug else cache_age) static_app = StaticURLParser(config['pylons.paths']['static_files'], cache_max_age=None if debug else cache_age) app = Cascade([overlay_app, static_app, app]) # Fanstatic inserts links for javascript and css ressources. # The required resources can be specified at runtime with <resource>.need() # and can will be delivered with version specifiers in the url and # minified when not in debug mode. app = Fanstatic(app, minified=not(debug), versioning=True, recompute_hashes=debug, bundle=not(debug), base_url=base_url(instance=None).rstrip('/'), # fanstatic's URL path already starts with / bottom=True ) if asbool(config.get('adhocracy.include_machine_name_in_header', 'false')): app = IncludeMachineName(app, config) return app
def get_proposal_url(): return _site.base_url(u'/proposal', get_feedback_instance())
def make_app(global_conf, full_stack=True, static_files=True, **app_conf): """Create a Pylons WSGI application and return it ``global_conf`` The inherited configuration for this application. Normally from the [DEFAULT] section of the Paste ini file. ``full_stack`` Whether this application provides a full WSGI stack (by default, meaning it handles its own exceptions and errors). Disable full_stack when this application is "managed" by another WSGI middleware. ``static_files`` Whether this application serves its own static files; disable when another web server is responsible for serving them. ``app_conf`` The application's local configuration. Normally specified in the [app:<name>] section of the Paste ini file (where <name> defaults to main). """ debug = (asbool(global_conf.get('debug', False)) or asbool(app_conf.get('debug', False))) # Configure the Pylons environment load_environment(global_conf, app_conf) # The Pylons WSGI app app = PylonsApp() # Routing/Session/Cache Middleware app = RoutesMiddleware(app, config['routes.map']) app = SessionMiddleware(app, config) app = CacheMiddleware(app, config) #app = make_profile_middleware(app, config, log_filename='profile.log.tmp') # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares) app = setup_auth(app, config) app = setup_discriminator(app, config) if asbool(full_stack): # Handle Python exceptions app = ErrorHandler(app, global_conf, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (and # 500 when debug is disabled) if debug: app = StatusCodeRedirect(app) else: app = StatusCodeRedirect(app, [400, 401, 403, 404, 500]) # Establish the Registry for this application app = RegistryManager(app) if asbool(static_files): cache_age = int(config.get('adhocracy.static.age', 7200)) # Serve static files overlay_app = StaticURLParser( get_site_path('static'), cache_max_age=None if debug else cache_age) static_app = StaticURLParser( config['pylons.paths']['static_files'], cache_max_age=None if debug else cache_age) app = Cascade([overlay_app, static_app, app]) # Fanstatic inserts links for javascript and css ressources. # The required resources can be specified at runtime with <resource>.need() # and can will be delivered with version specifiers in the url and # minified when not in debug mode. app = Fanstatic( app, minified=not (debug), versioning=True, recompute_hashes=debug, bundle=not (debug), base_url=base_url(instance=None).rstrip( '/'), # fanstatic's URL path already starts with / bottom=True) if asbool(config.get('adhocracy.include_machine_name_in_header', 'false')): app = IncludeMachineName(app, config) return app
def get_proposal_url(): return _site.base_url(u'/proposal', config.get('adhocracy.feedback_instance_key'))