From 9aba91d8045d1161e5305cdbf0a76883c1496ce4 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 24 2018 10:34:51 +0000 Subject: Move from a context_processor to jinja filters Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/flask_app.py b/pagure/flask_app.py index 8352c7c..8a053d4 100644 --- a/pagure/flask_app.py +++ b/pagure/flask_app.py @@ -107,36 +107,6 @@ def create_app(config=None): os.path.join(app.root_path, 'static'), ] - @app.context_processor - def context_processor(): - def user_can_clone_ssh(username): - ssh_keys = '' - if flask.g.authenticated: - ssh_keys = pagure.lib.search_user( - flask.g.session, - username=flask.g.fas_user.username - ).public_ssh_key or '' - if not (pagure_config.get('ALWAYS_RENDER_SSH_CLONE_URL') - or ssh_keys.strip()): - return False - return True - - def get_git_url_ssh(): - """ Return the GIT SSH URL to be displayed in the UI based on the - content of the configuration file. - """ - git_url_ssh = pagure_config.get('GIT_URL_SSH') - if flask.g.authenticated and git_url_ssh: - try: - git_url_ssh = git_url_ssh.format( - username=flask.g.fas_user.username) - except (KeyError, IndexError): - pass - return git_url_ssh - - return {'user_can_clone_ssh': user_can_clone_ssh, - 'git_url_ssh': get_git_url_ssh()} - auth = pagure_config.get('PAGURE_AUTH', None) if auth in ['fas', 'openid']: # Only import and set flask_fas_openid if it is needed diff --git a/pagure/templates/repo_master.html b/pagure/templates/repo_master.html index f71b2cc..44a4457 100644 --- a/pagure/templates/repo_master.html +++ b/pagure/templates/repo_master.html @@ -1,7 +1,7 @@ {% extends "master.html" %} -{% macro print_ssh_url(repo, git_url_ssh, current_user) %} - {% if not user_can_clone_ssh(current_user) %} +{% macro print_ssh_url(repo, end_url_ssh, current_user) %} + {% if not (current_user |user_can_clone_ssh) %} You need to upload SSH key to be able to clone over SSH @@ -13,7 +13,7 @@
SSH
+ end_url_ssh | git_url_ssh }}{{ repo.fullname }}.git" readonly>
{% endif %} @@ -171,7 +171,7 @@
Source Code
{% if g.authenticated and g.repo_committer %} - {{ print_ssh_url(repo, git_url_ssh, g.fas_user.username) }} + {{ print_ssh_url(repo, g.fas_user.username) }} {% endif %}
@@ -186,7 +186,7 @@ and repo.settings.get('project_documentation', True) %}
Documentation
{% if g.authenticated and g.repo_committer %} - {{ print_ssh_url(repo, git_url_ssh + "docs/", g.fas_user.username) }} + {{ print_ssh_url(repo, "docs/", g.fas_user.username) }} {% endif %}
@@ -200,10 +200,10 @@ {% if config.get('ENABLE_TICKETS', True) and repo.settings.get('issue_tracker', True) %}
Issues
- {{ print_ssh_url(repo, git_url_ssh + "tickets/", g.fas_user.username) }} + {{ print_ssh_url(repo, "tickets/", g.fas_user.username) }} {% endif %}
Pull Requests
- {{ print_ssh_url(repo, git_url_ssh + "requests/", g.fas_user.username) }} + {{ print_ssh_url(repo, "requests/", g.fas_user.username) }} {% endif %}
diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py index db89e8f..b9840ac 100644 --- a/pagure/ui/filters.py +++ b/pagure/ui/filters.py @@ -685,3 +685,32 @@ def join_prefix(values, num): if len(values) <= num: return ", ".join(values[:-1]) + " and " + values[-1] return "%s and %d others" % (", ".join(values[:num]), len(values) - num) + + +@UI_NS.app_template_filter('user_can_clone_ssh') +def user_can_clone_ssh(username): + ssh_keys = '' + if flask.g.authenticated: + ssh_keys = pagure.lib.search_user( + flask.g.session, + username=flask.g.fas_user.username + ).public_ssh_key or '' + if not (pagure_config.get('ALWAYS_RENDER_SSH_CLONE_URL') + or ssh_keys.strip()): + return False + return True + + +@UI_NS.app_template_filter('git_url_ssh') +def get_git_url_ssh(complement=''): + """ Return the GIT SSH URL to be displayed in the UI based on the + content of the configuration file. + """ + git_url_ssh = pagure_config.get('GIT_URL_SSH') + if flask.g.authenticated and git_url_ssh: + try: + git_url_ssh = git_url_ssh.format( + username=flask.g.fas_user.username) + except (KeyError, IndexError): + pass + return git_url_ssh + complement