diff --git a/home/context_processors.py b/home/context_processors.py index 238742c..a31cf12 100644 --- a/home/context_processors.py +++ b/home/context_processors.py @@ -1,4 +1,6 @@ -from .models import Caterer, GlobalConstants +from django.core.cache import cache + +from .models import Caterer, Contact, GlobalConstants """ File-name: context_processors.py @@ -6,6 +8,35 @@ Used to send All Caterers Information as context in the base template """ +FOOTER_CONTACT_CACHE_KEY = "footer_contact" +FOOTER_CONTACT_EMAIL = "gs.dining@iiti.ac.in" +FOOTER_CONTACT_CACHE_TIMEOUT = 60 * 60 * 24 # 1 day +_MISSING = object() + + +def get_footer_contact(): + cached = cache.get(FOOTER_CONTACT_CACHE_KEY, _MISSING) + if cached is not _MISSING: + return cached + + contact = ( + Contact.objects.filter(email__iexact=FOOTER_CONTACT_EMAIL) + .only("name", "occupation", "email", "contact") + .first() + ) + data = ( + { + "name": contact.name, + "occupation": contact.occupation, + "email": contact.email, + "phone": contact.contact, + } + if contact + else {} + ) + cache.set(FOOTER_CONTACT_CACHE_KEY, data, FOOTER_CONTACT_CACHE_TIMEOUT) + return data + def base(request): caterer = Caterer.objects.filter(visible=True).all() @@ -13,4 +44,8 @@ def base(request): if not constants: # Provide defaults if no constants exist yet constants = GlobalConstants.objects.create() - return {"all_caterer": caterer, "constants": constants} + return { + "all_caterer": caterer, + "constants": constants, + "footer_contact": get_footer_contact(), + } diff --git a/home/signals.py b/home/signals.py index 7af06f9..6bbc75b 100644 --- a/home/signals.py +++ b/home/signals.py @@ -1,13 +1,16 @@ import logging from datetime import datetime -from django.db.models.signals import post_save, pre_save +from django.core.cache import cache +from django.db.models.signals import post_delete, post_save, pre_save from django.dispatch import receiver +from .context_processors import FOOTER_CONTACT_CACHE_KEY from .models import ( Allocation, Caterer, CatererBills, + Contact, LongRebate, Period, Rebate, @@ -26,6 +29,11 @@ logger = logging.getLogger(__name__) +@receiver([post_save, post_delete], sender=Contact) +def invalidate_footer_contact_cache(sender, **kwargs): + cache.delete(FOOTER_CONTACT_CACHE_KEY) + + @receiver(post_save, sender=Student) def create_bill(sender, instance, created, **kwargs): if created: diff --git a/messWebsite/settings.py b/messWebsite/settings.py index 9ec7a60..3152ade 100644 --- a/messWebsite/settings.py +++ b/messWebsite/settings.py @@ -91,6 +91,13 @@ WSGI_APPLICATION = "messWebsite.wsgi.application" DJANGO_ADMIN_LOGS_DELETABLE = True +CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.locmem.LocMemCache", + "LOCATION": "mess-website", + } +} + # Database # https://docs.djangoproject.com/en/4.1/ref/settings/#databases diff --git a/templates/base.html b/templates/base.html index 9190da2..0d7c8f7 100644 --- a/templates/base.html +++ b/templates/base.html @@ -127,13 +127,21 @@
Location :