Skip to content

Commit

Permalink
Rename template functions for compatibility with tera and rust repolo…
Browse files Browse the repository at this point in the history
…gy-webapp
  • Loading branch information
AMDmi3 committed Sep 16, 2024
1 parent 4502a3c commit 7b443f3
Show file tree
Hide file tree
Showing 66 changed files with 354 additions and 328 deletions.
4 changes: 3 additions & 1 deletion repologyapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from repologyapp.config import config
from repologyapp.globals import repometadata
from repologyapp.template_filters import css_for_versionclass, extract_netloc, maintainer_to_links, maintainers_to_group_mailto
from repologyapp.template_functions import endpoint_like, url_for_self
from repologyapp.template_functions import endpoint_like, url_for, url_for_self, url_for_static
from repologyapp.template_tests import for_page, has_flag, has_flag_at, is_fallback_maintainer
from repologyapp.views import registry as view_registry

Expand Down Expand Up @@ -60,7 +60,9 @@
app.jinja_env.tests['has_flag_at'] = has_flag_at

# templates: custom global functions
app.jinja_env.globals['url_for'] = url_for
app.jinja_env.globals['url_for_self'] = url_for_self
app.jinja_env.globals['url_for_static'] = url_for_static

# templates: custom global data
app.jinja_env.globals['REPOLOGY_HOME'] = config['REPOLOGY_HOME']
Expand Down
30 changes: 27 additions & 3 deletions repologyapp/template_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,42 @@
__all__ = ['url_for_self']


PYTHON_TO_RUST_ENDPOINT_NAMES = {
'api_v1_projects': 'ApiV1Project',
'badge_tiny_repos': 'BadgeTinyRepos',
'badge_version_for_repo': 'BadgeVersionForRepo',
'badge_vertical_allrepos': 'BadgeVerticalAllRepos',
'badge_latest_versions': 'BadgeLatestVersions',
}

RUST_TO_PYTHON_ENDPOINT_NAMES = { rust: python for python, rust in PYTHON_TO_RUST_ENDPOINT_NAMES.items() }


def url_for(**args: Any) -> Any:
if endpoint := RUST_TO_PYTHON_ENDPOINT_NAMES.get(args['endpoint']):
args['endpoint'] = endpoint
return flask.url_for(**args)


def url_for_self(**args: Any) -> Any:
assert flask.request.endpoint is not None
return flask.url_for(flask.request.endpoint, **dict(flask.request.view_args, **args))


def url_for_static(**args: Any) -> Any:
return url_for(endpoint='static', filename=args['file'])


def endpoint_like(*variants: str) -> bool:
endpoint = flask.request.endpoint
python_endpoint = flask.request.endpoint
rust_endpoint = PYTHON_TO_RUST_ENDPOINT_NAMES.get(python_endpoint)

for variant in variants:
if endpoint == variant:
if python_endpoint == variant or rust_endpoint == variant:
return True
elif variant.endswith('*') and python_endpoint and python_endpoint.startswith(variant[:-1]):
return True
elif variant.endswith('*') and endpoint and endpoint.startswith(variant[:-1]):
elif variant.endswith('*') and rust_endpoint and rust_endpoint.startswith(variant[:-1]):
return True

return False
42 changes: 21 additions & 21 deletions repologyapp/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
{% block extrameta %}
{% endblock %}
<title>{% block title %}Repology{% endblock %}</title>
<link rel="stylesheet" href="{{ url_for("static", filename="bootstrap.min.v3.3.7.css") }}">
<link rel="stylesheet" href="{{ url_for("static", filename="repology.v21.css") }}">
<link rel="icon" href="{{ url_for("static", filename="repology.v1.ico") }}" sizes="16x16 32x32 64x64" type="image/x-icon">
<link rel="search" type="application/opensearchdescription+xml" title="Repology packages" href="{{ url_for("opensearch_project") }}">
<link rel="search" type="application/opensearchdescription+xml" title="Repology maintainers" href="{{ url_for("opensearch_maintainer") }}">
<link rel="stylesheet" href="{{ url_for_static(file="bootstrap.min.v3.3.7.css") }}">
<link rel="stylesheet" href="{{ url_for_static(file="repology.v21.css") }}">
<link rel="icon" href="{{ url_for_static(file="repology.v1.ico") }}" sizes="16x16 32x32 64x64" type="image/x-icon">
<link rel="search" type="application/opensearchdescription+xml" title="Repology packages" href="{{ url_for(endpoint="opensearch_project") }}">
<link rel="search" type="application/opensearchdescription+xml" title="Repology maintainers" href="{{ url_for(endpoint="opensearch_maintainer") }}">
</head>
<body>

{% if now().month == 6 and now().day == 6 and ':' not in request.environ.REMOTE_ADDR and request.endpoint != 'index' %}
<div class="site-alert-outer">
<div class="container site-alert-inner">
<img src="{{ url_for("static", filename="ipv6.v1.svg") }}" alt="IPv6 logo" height="64">
<img src="{{ url_for_static(file="ipv6.v1.svg") }}" alt="IPv6 logo" height="64">
<p>You are connected to Repology using legacy IPv4 protocol.
Due to <a href="https://en.wikipedia.org/wiki/IPv6#IPv4_address_exhaustion">IPv4 address exhaustion</a>
it may become too expensive or impossible for Repology to support IPv4 in the nearest future.
Expand All @@ -39,26 +39,26 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{{ url_for("index") }}">
<img alt="Repology" src="{{ url_for("static", filename="repology40x40.v1.png") }}" width="40" height="40">
<a class="navbar-brand" href="{{ url_for(endpoint="index") }}">
<img alt="Repology" src="{{ url_for_static(file="repology40x40.v1.png") }}" width="40" height="40">
</a>
</div>

<div class="collapse navbar-collapse" id="repology-navbar-collapse">
<ul class="nav navbar-nav">
<li{% if endpoint_like('projects', 'project_*') %} class="active"{% endif %}><a href="{{ url_for('projects') }}">Projects</a></li>
<li{% if endpoint_like('maintainers', 'maintainer') %} class="active"{% endif %}><a href="{{ url_for('maintainers') }}">Maintainers</a></li>
<li{% if endpoint_like('repositories_*', 'repository') %} class="active"{% endif %}><a href="{{ url_for('repositories_statistics') }}">Repositories</a></li>
<li{% if endpoint_like('tools', 'tool_*', 'trending') %} class="active"{% endif %}><a href="{{ url_for('tools') }}">Tools</a></li>
<li{% if endpoint_like('security_*') %} class="active"{% endif %}><a href="{{ url_for('security_recent_cves') }}">Security</a></li>
{% if session.admin %}<li{% if endpoint_like('admin', 'admin_*') %} class="active"{% endif %}><a href="{{ url_for('admin') }}">Admin</a></li>{% endif %}
<li{% if endpoint_like('projects', 'project_*') %} class="active"{% endif %}><a href="{{ url_for(endpoint='projects') }}">Projects</a></li>
<li{% if endpoint_like('maintainers', 'maintainer') %} class="active"{% endif %}><a href="{{ url_for(endpoint='maintainers') }}">Maintainers</a></li>
<li{% if endpoint_like('repositories_*', 'repository') %} class="active"{% endif %}><a href="{{ url_for(endpoint='repositories_statistics') }}">Repositories</a></li>
<li{% if endpoint_like('tools', 'tool_*', 'trending') %} class="active"{% endif %}><a href="{{ url_for(endpoint='tools') }}">Tools</a></li>
<li{% if endpoint_like('security_*') %} class="active"{% endif %}><a href="{{ url_for(endpoint='security_recent_cves') }}">Security</a></li>
{% if session.admin %}<li{% if endpoint_like('admin', 'admin_*') %} class="active"{% endif %}><a href="{{ url_for(endpoint='admin') }}">Admin</a></li>{% endif %}
</ul>
<ul class="nav navbar-nav navbar-right">
{% if session.experimental %}
<li{% if endpoint_like('experimental') %} class="active"{% endif %}><a href="{{ url_for('experimental') }}">Experimental</a></li>
<li{% if endpoint_like('experimental') %} class="active"{% endif %}><a href="{{ url_for(endpoint='experimental') }}">Experimental</a></li>
{% endif %}
<li{% if endpoint_like('news') %} class="active"{% endif %}><a href="{{ url_for('news') }}">News</a></li>
<li{% if endpoint_like('docs', 'docs_*', 'api_v1') %} class="active"{% endif %}><a href="{{ url_for('docs') }}">Docs</a></li>
<li{% if endpoint_like('news') %} class="active"{% endif %}><a href="{{ url_for(endpoint='news') }}">News</a></li>
<li{% if endpoint_like('docs', 'docs_*', 'api_v1') %} class="active"{% endif %}><a href="{{ url_for(endpoint='docs') }}">Docs</a></li>
</ul>
</div>
</div>
Expand Down Expand Up @@ -102,10 +102,10 @@ <h1 class="page-header">{% block header %}{% endblock %}</h1>
</div>
</footer>

<script src="{{ url_for("static", filename="jquery-3.7.1.min.js") }}"></script>
<script src="{{ url_for("static", filename="bootstrap.min.v3.3.7.js") }}"></script>
<script src="{{ url_for("static", filename="moment.min.v2.29.2.js") }}"></script>
<script src="{{ url_for("static", filename="repology.v2.js") }}"></script>
<script src="{{ url_for_static(file="jquery-3.7.1.min.js") }}"></script>
<script src="{{ url_for_static(file="bootstrap.min.v3.3.7.js") }}"></script>
<script src="{{ url_for_static(file="moment.min.v2.29.2.js") }}"></script>
<script src="{{ url_for_static(file="repology.v2.js") }}"></script>

</body>
</html>
2 changes: 1 addition & 1 deletion repologyapp/templates/_macros/link_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

{%- macro link_status(link, use_archive=False) -%}
{%- if link.ipv4_success is not none or link.ipv6_success is not none -%}
<sup class="link-status non-selectable"><a href="{{ url_for('link', url=link.url) }}" rel="nofollow">
<sup class="link-status non-selectable"><a href="{{ url_for(endpoint='link', url=link.url) }}" rel="nofollow">
{%- if link.ipv6_success and link.ipv6_permanent_redirect_target is not none -%}
<span class="link-redirect">redir</span>
{%- elif link.ipv6_success -%}
Expand Down
2 changes: 1 addition & 1 deletion repologyapp/templates/_macros/links.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@

{%- macro repository_link(repo) -%}
{%- if repo in repometadata.active_names() -%}
<a href="{{ url_for('repository', repo=repo) }}">{{ repometadata[repo].desc }}</a>
<a href="{{ url_for(endpoint='repository', repo=repo) }}">{{ repometadata[repo].desc }}</a>
{%- else -%}
<span class="legacy-repository">{{ repometadata[repo].desc }}</span>
{%- endif -%}
Expand Down
4 changes: 2 additions & 2 deletions repologyapp/templates/_macros/projects_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
{% endif %}
<tr>
<td class="text-nowrap">
<a href="{{ url_for(target_project_endpoint, name=metapackagename) }}">{{ metapackagename }}</a>
<a href="{{ url_for(endpoint=target_project_endpoint, name=metapackagename) }}">{{ metapackagename }}</a>
{%- if metapackage.has_related -%}
<a class="non-selectable" href="{{ url_for("project_related", name=metapackagename) }}"></a>
<a class="non-selectable" href="{{ url_for(endpoint="project_related", name=metapackagename) }}"></a>
{% endif %}
</td>

Expand Down
18 changes: 9 additions & 9 deletions repologyapp/templates/admin/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

{% block preheader %}
{% if session.admin %}
<form class="form-inline pageheader-right" action="{{ url_for("admin") }}" method="POST">
<form class="form-inline pageheader-right" action="{{ url_for(endpoint="admin") }}" method="POST">
<button type="submit" class="btn btn-default">Logout</button>
</form>
<div class="btn-group pageheader-right">
<a href="{{ url_for("admin_reports_unprocessed") }}" class="btn btn-default{% if request.endpoint == "admin_reports_unprocessed" %} active{% endif %}">Unprocessed reports</a>
<a href="{{ url_for("admin_reports_recent") }}" class="btn btn-default{% if request.endpoint == "admin_reports_recent" %} active{% endif %}">Recent reports</a>
<a href="{{ url_for("admin_updates") }}" class="btn btn-default{% if request.endpoint == "admin_updates" %} active{% endif %}">Updates</a>
<a href="{{ url_for("admin_redirects") }}" class="btn btn-default{% if request.endpoint == "admin_redirects" %} active{% endif %}">Redirects</a>
<a href="{{ url_for("admin_name_samples") }}" class="btn btn-default{% if request.endpoint == "admin_name_samples" %} active{% endif %}">Name samples</a>
<a href="{{ url_for("admin_cpes") }}" class="btn btn-default{% if request.endpoint == "admin_cpes" %} active{% endif %}">CPEs</a>
<a href="{{ url_for("admin_cve_misses") }}" class="btn btn-default{% if request.endpoint == "admin_cve_misses" %} active{% endif %}">CVE misses</a>
<a href="{{ url_for("admin_omni_cves") }}" class="btn btn-default{% if request.endpoint == "admin_omni_cves" %} active{% endif %}">Omni CVEs</a>
<a href="{{ url_for(endpoint="admin_reports_unprocessed") }}" class="btn btn-default{% if request.endpoint == "admin_reports_unprocessed" %} active{% endif %}">Unprocessed reports</a>
<a href="{{ url_for(endpoint="admin_reports_recent") }}" class="btn btn-default{% if request.endpoint == "admin_reports_recent" %} active{% endif %}">Recent reports</a>
<a href="{{ url_for(endpoint="admin_updates") }}" class="btn btn-default{% if request.endpoint == "admin_updates" %} active{% endif %}">Updates</a>
<a href="{{ url_for(endpoint="admin_redirects") }}" class="btn btn-default{% if request.endpoint == "admin_redirects" %} active{% endif %}">Redirects</a>
<a href="{{ url_for(endpoint="admin_name_samples") }}" class="btn btn-default{% if request.endpoint == "admin_name_samples" %} active{% endif %}">Name samples</a>
<a href="{{ url_for(endpoint="admin_cpes") }}" class="btn btn-default{% if request.endpoint == "admin_cpes" %} active{% endif %}">CPEs</a>
<a href="{{ url_for(endpoint="admin_cve_misses") }}" class="btn btn-default{% if request.endpoint == "admin_cve_misses" %} active{% endif %}">CVE misses</a>
<a href="{{ url_for(endpoint="admin_omni_cves") }}" class="btn btn-default{% if request.endpoint == "admin_omni_cves" %} active{% endif %}">Omni CVEs</a>
</div>
{% endif %}
{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion repologyapp/templates/admin/cpes.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<tbody>
{% for cpe in cpes %}
<tr>
<td class="minimal-column"><a href="{{ url_for('project_versions', name=cpe.effname) }}">{{ cpe.effname }}</a></td>
<td class="minimal-column"><a href="{{ url_for(endpoint='project_versions', name=cpe.effname) }}">{{ cpe.effname }}</a></td>
<td class="minimal-column">{{ cpe_components(cpe) }}</td>
<td class="text-center minimal-column">
{%- if cpe.has_alive_project -%}
Expand Down
2 changes: 1 addition & 1 deletion repologyapp/templates/admin/cve-misses.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
{% if item.project_candidates %}
{% for effname in item.project_candidates %}
{% if not loop.first %}<br>{% endif %}
<a href="{{ url_for('project_versions', name=effname ) }}">{{ effname }}</a>
<a href="{{ url_for(endpoint='project_versions', name=effname ) }}">{{ effname }}</a>
<form class="form-inlinest" action="{{ url_for_self() }}" method="POST">
<input type="hidden" name="effname" value="{{ effname }}">
<input type="hidden" name="cpe_vendor" value="{{ item.cpe_vendor }}">
Expand Down
4 changes: 2 additions & 2 deletions repologyapp/templates/admin/name-samples.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="container">

{% for reponame in repometadata.active_names() %}
<h3><a href="{{ url_for('repository', repo=reponame) }}">{{ repometadata[reponame].desc }}</a></h3>
<h3><a href="{{ url_for(endpoint='repository', repo=reponame) }}">{{ repometadata[reponame].desc }}</a></h3>

<table class="table table-striped">
<thead>
Expand All @@ -22,7 +22,7 @@ <h3><a href="{{ url_for('repository', repo=reponame) }}">{{ repometadata[reponam
<tbody>
{% for sample in samples_by_repo[reponame] %}
<tr>
<td class="text-center break-word"><a href="{{ url_for('project', name=sample.effname) }}">{{ sample.effname }}</td>
<td class="text-center break-word"><a href="{{ url_for(endpoint='project', name=sample.effname) }}">{{ sample.effname }}</td>
<td class="text-center break-word">{{ sample.projectname_seed }}</td>
<td class="text-center break-word info">{{ sample.trackname }}</td>
<td class="text-center break-word">{% if sample.srcname %}{{ sample.srcname }}{% else %}-{% endif %}</td>
Expand Down
2 changes: 1 addition & 1 deletion repologyapp/templates/admin/omni-cves.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<tbody>
{% for item in items %}
<tr>
<td class="minimal-column"><a href="{{ url_for('project_versions', name=item.effname) }}">{{ item.effname }}</a></td>
<td class="minimal-column"><a href="{{ url_for(endpoint='project_versions', name=item.effname) }}">{{ item.effname }}</a></td>
<td class="minimal-column"><a href="https://nvd.nist.gov/vuln/detail/{{ item.cve_id }}">{{ item.cve_id }}</a></td>
<td class="minimal-column">
<span class="version version-big version-rolling }}">
Expand Down
4 changes: 2 additions & 2 deletions repologyapp/templates/admin/redirects.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

{% if project %}

<h3>Redirects for <a href="{{ url_for('project', name=project) }}">{{ project }}</a></h3>
<h3>Redirects for <a href="{{ url_for(endpoint='project', name=project) }}">{{ project }}</a></h3>

<table class="table table-striped">
{% if redirects %}
Expand Down Expand Up @@ -54,7 +54,7 @@ <h3>Redirects for <a href="{{ url_for('project', name=project) }}">{{ project }}
<b class="text-danger">unknown</b>
{%- endif -%}
<td>
<a class="btn btn-default btn-xs" href={{ url_for('project', name=redirect.oldname if redirect.newname == project else redirect.oldname) }}>Go to</a>
<a class="btn btn-default btn-xs" href={{ url_for(endpoint='project', name=redirect.oldname if redirect.newname == project else redirect.oldname) }}>Go to</a>
{%- if redirect.is_manual %}
<form class="form-inlinest" action="{{ url_for_self(project=project) }}" method="POST">
<input type="hidden" name="oldname" value="{{ redirect.oldname }}">
Expand Down
2 changes: 1 addition & 1 deletion repologyapp/templates/admin/reports.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% for report in reports %}
<div class="panel {% if report.accepted is none %}panel-default{% elif report.accepted %}panel-success{% else %}panel-danger{% endif %}">
<div class="panel-heading">
<b><a href="{{ url_for('project_versions', name=report.effname) }}">{{ report.effname }}</a></b> - {{ format_time_interval(report.created_ago) }} ago</b>,
<b><a href="{{ url_for(endpoint='project_versions', name=report.effname) }}">{{ report.effname }}</a></b> - {{ format_time_interval(report.created_ago) }} ago</b>,
{% if report.accepted is none %}not processed yet{% elif report.accepted %}accepted{% else %}rejected{% endif %}
</div>
<div class="panel-body"><b>Flags:</b>
Expand Down
6 changes: 3 additions & 3 deletions repologyapp/templates/admin/updates.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
<tbody>
{% for repo in repos %}
<tr id="{{ repo.name }}">
<td class="text-right"><a href="{{ url_for('repository', repo=repo.name) }}">{{ repometadata[repo.name].desc }}</a></td>
<td class="text-right"><a href="{{ url_for(endpoint='repository', repo=repo.name) }}">{{ repometadata[repo.name].desc }}</a></td>

{% if repo.fetch_duration and repo.fetch_utime %}
<td class="text-center"><b><a href="{{ url_for('log', run_id=repo.fetch_run_id) }}">{{ format_time_interval_short(repo.fetch_duration) }}</a></b></td>
<td class="text-center"><b><a href="{{ url_for(endpoint='log', run_id=repo.fetch_run_id) }}">{{ format_time_interval_short(repo.fetch_duration) }}</a></b></td>
<td class="text-center"><b>{{ format_time_interval_short(repo.fetch_utime + repo.fetch_stime) }}</b> (<b>{{ (100.0 * (repo.fetch_utime + repo.fetch_stime) / repo.fetch_duration) | int }}</b>%)</td>
<td class="text-center">{% if not repo.fetch_maxrss_reliable %}<{{ (repo.fetch_maxrss / 1024.0) | round(2) }}{% else %}<b>{{ (repo.fetch_maxrss_reliable / 1024.0) | round(2) }}</b>{% endif %}MB</td>
{% else %}
Expand All @@ -39,7 +39,7 @@
{% endif %}

{% if repo.parse_duration and repo.parse_utime %}
<td class="text-center"><b><a href="{{ url_for('log', run_id=repo.parse_run_id) }}">{{ format_time_interval_short(repo.parse_duration) }}</a></b></td>
<td class="text-center"><b><a href="{{ url_for(endpoint='log', run_id=repo.parse_run_id) }}">{{ format_time_interval_short(repo.parse_duration) }}</a></b></td>
<td class="text-center"><b>{{ format_time_interval_short(repo.parse_utime + repo.parse_stime) }}</b> (<b>{{ (100.0 * (repo.parse_utime + repo.parse_stime) / repo.parse_duration) | int }}</b>%)</td>
<td class="text-center">{% if not repo.parse_maxrss_reliable %}<{{ (repo.parse_maxrss / 1024.0) | round(2) }}{% else %}<b>{{ (repo.parse_maxrss_reliable / 1024.0) | round(2) }}</b>{% endif %}MB</td>
{% else %}
Expand Down
Loading

0 comments on commit 7b443f3

Please sign in to comment.