show announcements on main page

This commit is contained in:
Laura Klünder 2017-12-10 15:40:28 +01:00
parent 6adaf4ab40
commit ad8300665b
2 changed files with 25 additions and 3 deletions

View file

@ -1,4 +1,5 @@
from django.conf import settings
from django.core.cache import cache
from django.db import models
from django.db.models import Q
from django.utils import timezone
@ -22,8 +23,23 @@ class Announcement(models.Model):
@classmethod
def get_current(cls):
result = cache.get('site:announcement', None)
if result is not None:
return result
try:
return cls.objects.filter(Q(active=True) & (Q(active_until__isnull=True) |
Q(active_until__gt=timezone.now()))).latest()
result = cls.objects.filter(Q(active=True) & (Q(active_until__isnull=True) |
Q(active_until__gt=timezone.now()))).latest()
except cls.DoesNotExist:
return None
result = None
timeout = 300
if result.active_until:
timeout = max(0, min(timeout, (result.active_until-timezone.now()).total_seconds()))
cache.set('site:announcement', result, timeout)
return result
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
cache.delete('site:announcement')

View file

@ -25,6 +25,7 @@ from c3nav.mapdata.models.locations import LocationRedirect, SpecificLocation
from c3nav.mapdata.utils.locations import get_location_by_slug_for_request, levels_by_short_label_for_request
from c3nav.mapdata.utils.user import get_user_data
from c3nav.mapdata.views import set_tile_access_cookie
from c3nav.site.models import Announcement
def check_location(location: Optional[str], request) -> Optional[SpecificLocation]:
@ -89,6 +90,11 @@ def map_index(request, mode=None, slug=None, slug2=None, details=None,
'tile_cache_server': settings.TILE_CACHE_SERVER,
'embed': bool(embed),
}
announcement = Announcement.get_current()
if announcement:
messages.info(request, announcement.text)
response = render(request, 'site/map.html', ctx)
set_tile_access_cookie(request, response)
if embed: