Skip to content

Commit

Permalink
frontend: add command for managing warning banner
Browse files Browse the repository at this point in the history
  • Loading branch information
nikromen authored and xsuchy committed Dec 23, 2022
1 parent 10d16bf commit e88e3eb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
51 changes: 51 additions & 0 deletions frontend/coprs_frontend/commands/warning_banner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Command for managing warning banner on the top of frontend page.
"""


import os
import sys
from pathlib import Path

import click
from jinja2 import Template

from coprs.constants import BANNER_LOCATION


@click.command()
@click.option("--outage_time", "-o", default=None, help="start time of the outage")
@click.option("--ticket", "-t", default=None, help="fedora infra ticket ID")
@click.option("--rest", default=None, help="additional code")
@click.option("--remove", is_flag=True, show_default=True, default=False, help="removes banner")
def warning_banner(outage_time, ticket, rest, remove):
"""
Adds a banner to Copr's frontend page with outage warning. `outage_time` or `rest` is required.
In case you need to schedule regular outage, fill in only `outage_time` and `ticket`. In case
you need to add something else, please use the `rest` parameter to dump to warning banner
some additional information. This can be just some text or html code.
`rest` parameter can be used as a standalone option just for showing some warning banner
with specific information. It can be also piece of HTML code.
"""
if remove:
if outage_time or ticket or rest:
print(
"Error: can't remove banner with `outage_time` or `ticket` or `rest`",
file=sys.stderr,
)
return

if os.path.exists(BANNER_LOCATION):
os.remove(BANNER_LOCATION)
return

if outage_time is None and rest is None:
print("Error: `outage_time` or `rest` should be present.", file=sys.stderr)
return

with open(BANNER_LOCATION, "w", encoding="UTF-8") as banner:
banner_path_template = Path(__file__).parent / "../coprs/templates/banner-include.html"
with banner_path_template.open() as banner_template:
template = Template(banner_template.read())
banner.write(template.render(outage_time=outage_time, ticket=ticket, rest=rest))
6 changes: 6 additions & 0 deletions frontend/coprs_frontend/coprs/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
File which contains only constants. Nothing else.
"""


BANNER_LOCATION = "/var/lib/copr/data/banner-include.html"
4 changes: 1 addition & 3 deletions frontend/coprs_frontend/coprs/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
import flask

from coprs import app
from coprs.constants import BANNER_LOCATION
from coprs.helpers import current_url


BANNER_LOCATION = "/var/lib/copr/banner-include.html"


@app.context_processor
def include_banner():
if os.path.exists(BANNER_LOCATION):
Expand Down
16 changes: 16 additions & 0 deletions frontend/coprs_frontend/coprs/templates/banner-include.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="alert alert-danger" role="alert">
{% if outage_time %}
<b>Warning!</b> Scheduled outage of Copr servers - {{ outage_time }}.
{% endif %}
{% if ticket %}
For more information please refer to
<a href="https://pagure.io/fedora-infrastructure/issue/{{ ticket }}">this ticket</a>.
{% endif %}
<!--
Possible rest of short html code. Something like:
"Check <a href="#" class="alert-link">this document</a> for details."
-->
{% if rest %}
{{ rest }}
{% endif %}
</div>
2 changes: 2 additions & 0 deletions frontend/coprs_frontend/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import commands.delete_orphans
import commands.fixup_unnoticed_chroots
import commands.chroots_template
import commands.warning_banner

from coprs import app

Expand Down Expand Up @@ -90,6 +91,7 @@
"clean_old_builds",
"delete_orphans",
"delete_dirs",
"warning_banner",
]


Expand Down

0 comments on commit e88e3eb

Please sign in to comment.