def _update_userbadges(self, user): mappings = config.get_json("adhocracy.shibboleth.userbadge_mapping") is_modified = False for m in mappings: badge_title = m["title"] function_name = m["function"] kwargs = m["args"] badge = UserBadge.find(badge_title) if badge is None: raise Exception('configuration expects badge "%s"' % badge_title) want_badge = USERBADGE_MAPPERS[function_name](request, **kwargs) has_badge = badge in user.badges if want_badge and not has_badge: # assign badge badge.assign(user=user, creator=user) is_modified = True elif has_badge and not want_badge: # unassign badge user.badges.remove(badge) is_modified = True if is_modified: meta.Session.commit()
def _get_display_name(self): display_name_function = config.get_json( "adhocracy.shibboleth.display_name.function") if display_name_function is not None: function = display_name_function["function"] kwargs = display_name_function["args"] display_name = DISPLAY_NAME_FUNCTIONS[function](request, **kwargs) else: display_name = None return display_name
def __init__(self, app, domain, config): self.app = app self.domain = domain self.config = config log.debug("Host name: %s." % domain) if aconfig.get_bool('adhocracy.instance_domains.enabled', config=config): instances_domains = aconfig.get_json('adhocracy.instance_domains', {}, config=config) self.domains_instances = dict((v, k) for k, v in instances_domains.items()) else: self.domains_instances = {}
def base_url(path='', instance=CURRENT_INSTANCE, absolute=False, append_slash=False, config=config, query_params=None, query_string=None, member=None): """ Constructs an URL. Path is expected to start with '/'. If not, a relative path to the current object will be created. If instance isn't defined, the current instance is assumed. Otherwise, either an instance, an instance key, or None has to be passed. If absolute is True, an absolute URL including the protocol part is returned. Otherwise this is avoided, if relative_urls is set to True and instance_domains isn't enabled. query_params is a dictionary of parameters for the query string of the URL. Alternatively to query_params, query_string can be specified which is directly used as the query string of the resulting URL. """ if instance == CURRENT_INSTANCE: instance = ifilter.get_instance() if instance is None: instance_key = None else: if isinstance(instance, (str, unicode)): instance_key = instance else: instance_key = instance.key domain = None instance_domains = aconfig.get_bool('adhocracy.instance_domains.enabled') if instance and instance_domains: domain = aconfig.get_json('adhocracy.instance_domains', {})\ .get(instance_key) if domain is not None: protocol = config.get('adhocracy.protocol', 'http').strip() result = '%s://%s%s' % (protocol, domain, path) elif relative_urls(config): if instance is None: prefix = '' else: prefix = '/i/' + instance_key if absolute or instance_domains: protocol = config.get('adhocracy.protocol', 'http').strip() domain = config.get('adhocracy.domain').strip() result = '%s://%s%s%s' % (protocol, domain, prefix, path) else: result = '%s%s' % (prefix, path) else: protocol = config.get('adhocracy.protocol', 'http').strip() domain = config.get('adhocracy.domain').strip() if instance is None or g.single_instance: subdomain = '' else: subdomain = '%s.' % instance_key result = '%s://%s%s%s' % (protocol, subdomain, domain, path) if member is not None: result += '/' + member if result == '': result = '/' if append_slash and not result.endswith('/'): result += '/' if query_params: result += '&' if '?' in result else '?' u = lambda s: unicode(s).encode('utf-8') query_params = [(u(k), u(v)) for (k, v) in query_params.iteritems()] result += urllib.urlencode(query_params) elif query_string: result += '&' if '?' in result else '?' result += query_string return result