from app import get_app_url from data import model from util.gravatar import compute_hash from util.names import parse_robot_username from jinja2 import Template, Environment, FileSystemLoader, contextfilter def icon_path(icon_name): return '%s/static/img/icons/%s.png' % (get_app_url(), icon_name) def icon_image(icon_name): return '%s' % (icon_path(icon_name), icon_name) def user_reference(username): user = model.get_namespace_user(username) if not user: return username is_robot = False if user.robot: parts = parse_robot_username(username) user = model.get_namespace_user(parts[0]) return """Robot %s""" % (icon_path('wrench'), username) alt = 'Organization' if user.organization else 'User' return """ %s %s """ % (compute_hash(user.email), alt, username) def repository_tag_reference(repository_path_and_tag): (repository_path, tag) = repository_path_and_tag (namespace, repository) = repository_path.split('/') owner = model.get_namespace_user(namespace) if not owner: return tag return """%s""" % (get_app_url(), namespace, repository, tag, tag) def repository_reference(pair): if isinstance(pair, tuple): (namespace, repository) = pair else: pair = pair.split('/') namespace = pair[0] repository = pair[1] owner = model.get_namespace_user(namespace) if not owner: return "%s/%s" % (namespace, repository) return """ %s/%s """ % (compute_hash(owner.email), get_app_url(), namespace, repository, namespace, repository) def admin_reference(username): user = model.get_user_or_org(username) if not user: return 'account settings' if user.organization: return """ organization's admin setting """ % (get_app_url(), username) else: return """ account settings """ % (get_app_url()) def get_template_env(searchpath): template_loader = FileSystemLoader(searchpath=searchpath) template_env = Environment(loader=template_loader) add_filters(template_env) return template_env def add_filters(template_env): template_env.filters['icon_image'] = icon_image template_env.filters['user_reference'] = user_reference template_env.filters['admin_reference'] = admin_reference template_env.filters['repository_reference'] = repository_reference template_env.filters['repository_tag_reference'] = repository_tag_reference