Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom management command that sets an announcement banner on front page #1781

Merged
merged 8 commits into from
Jun 20, 2024
45 changes: 45 additions & 0 deletions general/management/commands/announcement_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Custom management command to set/clear announcement_cache for front page announcements banner.

The announcement banner is a simple HTML snippet cached in the Django cache. It is formed by a title and a text
that are passed as arguments to the command. The command can be used to set the cache, clear it, or show the current
value of the cache.

Example usage:
python manage.py announcement_cache set "New feature!" "Now you can do this and that. <a href='/forum'>Learn more</a>"
python manage.py announcement_cache clear
python manage.py announcement_cache show

"""
from django.core.cache import cache
from django.core.management.base import BaseCommand
from django.template.loader import render_to_string


class Command(BaseCommand):
help = 'Set/clear "announcement_cache" for front page announcements banner'

def add_arguments(self, parser):
parser.add_argument("action", type=str, help="Indicates whether to set or clear the cache")
parser.add_argument("title", type=str, nargs="?", help="Title of the announcement")
parser.add_argument("text", type=str, nargs="?", help="Text of the announcement")

def handle(self, *args, **kwargs):
action = kwargs["action"]
title = kwargs["title"]
text = kwargs["text"]

announcement_cache_key = "announcement_cache"
Bomme marked this conversation as resolved.
Show resolved Hide resolved
if action == "set":
one_day = 60 * 60 * 24
Bomme marked this conversation as resolved.
Show resolved Hide resolved
value = render_to_string("molecules/announcement_banner.html", {"title": title, "text": text})
cache.set(announcement_cache_key, value, one_day)
self.stdout.write(self.style.SUCCESS("Successfully set announcement_cache"))
elif action == "clear":
cache.delete(announcement_cache_key)
self.stdout.write(self.style.SUCCESS("Successfully cleared announcement_cache"))
elif action == "show":
current_value = cache.get(announcement_cache_key, "<<Cache is empty>>")
self.stdout.write(self.style.SUCCESS("Current value of announcement_cache:"))
self.stdout.write(current_value)
else:
self.stdout.write(self.style.ERROR('Invalid action. Use "set" or "clear".'))
4 changes: 4 additions & 0 deletions templates/front.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ <h3 class="v-spacing-4">Latest additions</h3>
</section>
{% endcache %}
{% endif %}

{% if announcement_cache %}
{{ announcement_cache }}
{% endif %}

{% if rss_cache %}
{{ rss_cache }}
Expand Down
8 changes: 8 additions & 0 deletions templates/molecules/announcement_banner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="v-spacing-6">
<section class="border-grey-light">
<h3 class="text-center v-spacing-2 ellipsis">{{ title }}</h3>
<div class="center">
{{ text | safe }}
</div>
</section>
</div>
Loading