diff --git a/floodrelief/settings.py b/floodrelief/settings.py index 4695a010b..4e8d9d0d3 100644 --- a/floodrelief/settings.py +++ b/floodrelief/settings.py @@ -229,7 +229,7 @@ def get_list(text): S3_URL = "https://{}.s3.ap-south-1.amazonaws.com".format(bucket_name,) -if os.environ.get('USE_S3','').lower() == "true" : +if os.environ.get('USE_S3','').lower() == "true": AWS_STORAGE_BUCKET_NAME=bucket_name AWS_ACCESS_KEY_ID=os.environ.get("AWS_ACCESS_KEY_ID") AWS_SECRET_ACCESS_KEY=os.environ.get("AWS_SECRET_ACCESS_KEY") @@ -259,3 +259,12 @@ def get_list(text): 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', ), } + +HOME_PAGE_ANALYTICS = { + 'DISPLAY': True, + 'INVALIDATION_LOGIC': 'TIMEOUT', # options : ["TIMEOUT", "ON_CREATE"] + # ON_CREATE is accurate but expensive for drastic write COUNT. + 'TIMEOUT': 60 * 40, # DEFAULTS TO 40 MINUTE. + 'HOME_PAGE_CACHE_KEY': 'home_page_data_statics', + 'VOLUNTEER_CACHE_KEY': 'ngo_data_statics', +} diff --git a/mainapp/models.py b/mainapp/models.py index da016306f..cec8bd990 100644 --- a/mainapp/models.py +++ b/mainapp/models.py @@ -5,6 +5,7 @@ import codecs from hashlib import md5 +from django.conf import settings from django.db import models from django.core.validators import RegexValidator from django.contrib.auth.models import User @@ -725,6 +726,7 @@ def full_clean(self, *args, **kwargs): def __str__(self): return self.name + class Hospital(models.Model): name = models.CharField(max_length=200) officer = models.CharField(max_length=100) @@ -740,3 +742,30 @@ def __str__(self): @classmethod def count(cls): return cls._default_manager.count() + + +@receiver(post_save, sender=Request) +@receiver(post_save, sender=RescueCamp) +@receiver(post_save, sender=Announcements) +@receiver(post_save, sender=Contributor) +@receiver(post_save, sender=DistrictNeed) +@receiver(post_save, sender=Volunteer) +@receiver(post_save, sender=DistrictManager) +@receiver(post_save, sender=Hospital) +@receiver(post_save, sender=PrivateRescueCamp) +@receiver(post_save, sender=CollectionCenter) +def expire_home_page_count_cache(sender, created, **kwargs): + if created: + home_page_setting = getattr(settings, 'HOME_PAGE_ANALYTICS', {}) + if home_page_setting.get('INVALIDATION_LOGIC', '') == 'ON_CREATE': + cache.delete(home_page_setting.get('HOME_PAGE_CACHE_KEY', 'home_page_data_statics')) + + +@receiver(post_save, sender=Volunteer) +@receiver(post_save, sender=NGO) +def expire_home_page_count_cache(sender, created, **kwargs): + if created: + home_page_setting = getattr(settings, 'HOME_PAGE_ANALYTICS', {}) + if home_page_setting.get('INVALIDATION_LOGIC', '') == 'ON_CREATE': + cache.delete(home_page_setting.get('VOLUNTEER_CACHE_KEY', 'ngo_data_statics')) + diff --git a/mainapp/views.py b/mainapp/views.py index c52cce8f9..1a065f5f4 100644 --- a/mainapp/views.py +++ b/mainapp/views.py @@ -4,6 +4,11 @@ from django.views.generic.base import TemplateView from django.views.generic.list import ListView +from django.core.cache import cache +from django.core.cache.backends.base import DEFAULT_TIMEOUT +from django.conf import settings +CACHE_TTL = getattr(settings, 'CACHE_TTL', DEFAULT_TIMEOUT) + from mainapp.redis_queue import sms_queue from mainapp.sms_handler import send_confirmation_sms from .models import Request, Volunteer, DistrictManager, Contributor, DistrictNeed, Person, RescueCamp, NGO, \ @@ -46,6 +51,8 @@ def __init__(self, *args, **kwargs): PAGE_RIGHT = 5 PAGE_INTERMEDIATE = "50" +home_page_setting = getattr(settings, 'HOME_PAGE_ANALYTICS', {}) + class CreateRequest(CreateView): model = Request template_name='mainapp/request_form.html' @@ -203,20 +210,28 @@ class HomePageView(TemplateView): template_name = "home.html" def get_context_data(self, **kwargs): - cxt = super(HomePageView, self).get_context_data(**kwargs) - cxt['request_for_rescue_count'] = Request.request_for_rescue() - cxt['request_for_resource_count'] = Request.request_for_resource() - cxt['relief_camps_count'] = RescueCamp.count() - cxt['announcement_count'] = Announcements.count() - cxt['to_contribute_count'] = Contributor.count() - cxt['district_needs_count'] = DistrictNeed.count() - cxt['volunteer_and_ngo_company_count'] = Volunteer.count() + NGO.count() - cxt['contact_info'] = DistrictManager.count() - cxt['registered_requests_count'] = "-" # todo - - cxt['hospital_count'] = Hospital.count() - cxt['private_relief_and_collection_centers_count'] = PrivateRescueCamp.count() + CollectionCenter.count() + home_page_setting = getattr(settings, 'HOME_PAGE_ANALYTICS', None) + page_data_cache_key = home_page_setting.get('HOME_PAGE_CACHE_KEY', 'home_page_data_statics') + _data = {} + if home_page_setting.get('DISPLAY', False): + if page_data_cache_key in cache: + _data = cache.get(page_data_cache_key) + else: + _data['request_for_rescue_count'] = Request.request_for_rescue() + _data['request_for_resource_count'] = Request.request_for_resource() + _data['relief_camps_count'] = RescueCamp.count() + _data['announcement_count'] = Announcements.count() + _data['to_contribute_count'] = Contributor.count() + _data['district_needs_count'] = DistrictNeed.count() + _data['volunteer_and_ngo_company_count'] = Volunteer.count() + NGO.count() + _data['contact_info'] = DistrictManager.count() + _data['registered_requests_count'] = "-" # todo + _data['hospital_count'] = Hospital.count() + _data['private_relief_and_collection_centers_count'] = PrivateRescueCamp.count() + CollectionCenter.count() + timeout = home_page_setting.get('TIMEOUT', 60 * 40) + cache.set(page_data_cache_key, _data, timeout=timeout) + cxt = super(HomePageView, self).get_context_data(**_data, home_page_setting=home_page_setting, **kwargs) return cxt @@ -224,9 +239,17 @@ class NgoVolunteerView(TemplateView): template_name = "ngo_volunteer.html" def get_context_data(self, **kwargs): - cxt = super(NgoVolunteerView, self).get_context_data(**kwargs) - cxt['registered_volunteers_count'] = 0 - cxt['registered_ngo_count'] = 0 + + page_data_cache_key = home_page_setting.get('VOLUNTEER_CACHE_KEY', 'ngo_data_statics') + _data = {} + if home_page_setting.get('DISPLAY', False): + if page_data_cache_key in cache: + _data = cache.get(page_data_cache_key) + else: + _data['registered_volunteers_count'] = Volunteer.count() + _data['registered_ngo_count'] = NGO.count() + cache.set(page_data_cache_key, _data, timeout=CACHE_TTL) + cxt = super(NgoVolunteerView, self).get_context_data(**_data, home_page_setting=home_page_setting, **kwargs) return cxt diff --git a/templates/base.html b/templates/base.html index 01079a951..8e32d1a04 100644 --- a/templates/base.html +++ b/templates/base.html @@ -17,7 +17,7 @@ - Kerala Rescue + {% block title %} {% endblock %} Kerala Rescue - ദുരിതാശ്വാസ പ്രവർത്തനങ്ങളും രക്ഷാ ദൗത്യങ്ങളും ഏകോപിപ്പിക്കാൻ {# #} diff --git a/templates/home.html b/templates/home.html index 8740d1823..2a075b3cf 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,5 +1,9 @@ {% extends 'base.html' %} {% load static %} + +{% block title %} Home | {% endblock %} + + {% block content %} {% load bootstrap3 %} @@ -26,7 +30,9 @@

Keralarescue

for rescue
രക്ഷാപ്രവർത്തനം അഭ്യർത്ഥിക്കാൻ + {% if home_page_setting.DISPLAY %} Registered: {{ request_for_rescue_count}} + {% endif %} {% bootstrap_icon "grain" %} @@ -34,8 +40,9 @@

Keralarescue

for resources
സഹായം അഭ്യർത്ഥിക്കാൻ + {% if home_page_setting.DISPLAY %} Registered: {{ request_for_resource_count}} - + {% endif %}
{% bootstrap_icon "phone" %} @@ -98,7 +117,9 @@

Keralarescue

INFO
ഞങ്ങളുമായി ബന്ധപ്പെടാൻ + {% if home_page_setting.DISPLAY %} Registered: {{contact_info}} + {% endif %}
{% bootstrap_icon "list" %} @@ -106,7 +127,9 @@

Keralarescue

ഇതു വരെ ആവശ്യപ്പെട്ടവ + {% if home_page_setting.DISPLAY %} Registered: {{registered_requests_count}} + {% endif %}
{% bootstrap_icon "plus" %} @@ -114,7 +137,9 @@

Keralarescue

ആശുപത്രികൾ + {% if home_page_setting.DISPLAY %} Registered: {{hospital_count}} + {% endif %}
{% bootstrap_icon "briefcase" %} @@ -122,7 +147,9 @@

Keralarescue

Collection Centers
സ്വകാര്യ റിലീഫ് ആൻഡ് കളക്ഷൻ സെന്ററുകൾ + {% if home_page_setting.DISPLAY %} Registered: {{private_relief_and_collection_centers_count}} + {% endif %}

For emergency support, call the District control room at 1077 or the State control room at 1070.
diff --git a/templates/ngo_volunteer.html b/templates/ngo_volunteer.html index f760e2fee..b741c59c4 100644 --- a/templates/ngo_volunteer.html +++ b/templates/ngo_volunteer.html @@ -1,5 +1,9 @@ {% extends 'base.html' %} {% load static %} + +{% block title %} Register as a Volunteer OR NGO | {% endblock %} + + {% block content %} {% load bootstrap3 %} @@ -33,7 +37,9 @@

keralarescue

Volunteers + {% if home_page_setting.DISPLAY %} Registered: {{ registered_volunteers_count }} + {% endif %} @@ -49,7 +55,9 @@

keralarescue

NGO's/Company's + {% if home_page_setting.DISPLAY %} Registered: {{ registered_ngo_count }} + {% endif %}

Contact Control Room or Disaster Management Cell or District Administration for any support.