Skip to content

Commit

Permalink
Settings Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jerinisready committed Aug 10, 2019
1 parent 0343611 commit bdf525b
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 19 deletions.
11 changes: 10 additions & 1 deletion floodrelief/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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',
}
29 changes: 29 additions & 0 deletions mainapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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'))

55 changes: 39 additions & 16 deletions mainapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -203,30 +210,46 @@ 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


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


Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<meta property="og:type" content="website">
<meta property="og:image" content="{% static 'images/keralarescue.png' %}">

<title>Kerala Rescue</title>
<title>{% block title %} {% endblock %} Kerala Rescue - ദുരിതാശ്വാസ പ്രവർത്തനങ്ങളും രക്ഷാ ദൗത്യങ്ങളും ഏകോപിപ്പിക്കാൻ</title>

{# <link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.png' %}"/> #}

Expand Down
29 changes: 28 additions & 1 deletion templates/home.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{% extends 'base.html' %}
{% load static %}

{% block title %} Home | {% endblock %}


{% block content %}

{% load bootstrap3 %}
Expand All @@ -26,16 +30,19 @@ <h1 class="main-logo">Kerala<span style="font-weight: normal;">rescue</span></h1
Request<br/>for rescue<br/>
<span class="ml small notranslate">രക്ഷാപ്രവർത്തനം അഭ്യർത്ഥിക്കാൻ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{ request_for_rescue_count}} </span>
{% endif %}
</a>
<a href="https://keralarescuemap.ushahidi.io/posts/create/18" class="home-button card" role="button">
{% bootstrap_icon "grain" %}
<span class="text">
Request<br/>for resources<br/>
<span class="ml small notranslate">സഹായം അഭ്യർത്ഥിക്കാൻ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{ request_for_resource_count}} </span>

{% endif %}
</a>
<!-- <a href="{%url 'report_find_person' %}" class="home-button card" role="button">
{% bootstrap_icon "user" %}
Expand All @@ -50,79 +57,99 @@ <h1 class="main-logo">Kerala<span style="font-weight: normal;">rescue</span></h1
Relief Camps<br/>
<span class="ml small notranslate">ദുരിതാശ്വാസ ക്യാമ്പുകൾ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{relief_camps_count}} </span>
{% endif %}
</a>
<a href="{%url 'Announcements' %}" class="home-button card" role="button">
{% bootstrap_icon "bullhorn" %}
<span class="text">
Announcements<br/>
<span class="ml small notranslate">അറിയിപ്പുകൾ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{announcement_count}} </span>
{% endif %}
</a>
<a href="{%url 'reg_contribview' %}" class="home-button card" role="button">
{% bootstrap_icon "apple" %}
<span class="text">
To Contribute<br/>
<span class="ml small notranslate">സംഭാവന നൽകാൻ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{to_contribute_count}} </span>
{% endif %}
</a>
<a href="{%url 'distneedsview' %}" class="home-button card" role="button">
{% bootstrap_icon "map-marker" %}
<span class="text">
DISTRICT NEEDS &amp; COLLECTION CENTERS<br/>
<span class="ml small notranslate">ജില്ലകളിലെ ആവശ്യങ്ങൾ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{district_needs_count}} </span>
{% endif %}
</a>
<a href="{%url 'ngovolunteerview' %}" class="home-button card" role="button">
{% bootstrap_icon "user" %}
<span class="text">
Volunteer &amp; NGO/COMPANY <br/>
<span class="ml small notranslate">വൊളന്‍റീയര &amp;<br/> എൻജിഒ്/കമ്പനി‍ </span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{volunteer_and_ngo_company_count}} </span>
{% endif %}
</a>
<!-- <a href="{%url 'mapview' %}" class="home-button card" role="button">
{% bootstrap_icon "globe" %}
<span class="text">
Maps<br/>
<span class="ml small notranslate">സഹായം ആവശ്യമായ സ്ഥലങ്ങളും<br/>ദുരിതാശ്വാസ ക്യാമ്പുകളും</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: 43 </span>
{% endif %}
</a> -->
<a href="{% url 'contactus' %}" class="home-button card" role="button">
{% bootstrap_icon "phone" %}
<span class="text">
Contact <br>INFO<br/>
<span class="ml small notranslate">ഞങ്ങളുമായി ബന്ധപ്പെടാൻ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{contact_info}} </span>
{% endif %}
</a>
<a href="https://keralarescuemap.ushahidi.io/views/map" class="home-button card" role="button">
{% bootstrap_icon "list" %}
<span class="text">
Registered Requests<br/>
<span class="ml small notranslate">ഇതു വരെ ആവശ്യപ്പെട്ടവ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{registered_requests_count}} </span>
{% endif %}
</a>
<a href="{%url 'hospitals' %}" class="home-button card" role="button">
{% bootstrap_icon "plus" %}
<span class="text">
Hospitals<br/>
<span class="ml small notranslate">ആശുപത്രികൾ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{hospital_count}} </span>
{% endif %}
</a>
<a href="{%url 'privatedetails' %}" class="home-button card" role="button">
{% bootstrap_icon "briefcase" %}
<span class="text">
Private Relief &<br> Collection Centers<br/>
<span class="ml small notranslate">സ്വകാര്യ റിലീഫ് ആൻഡ് കളക്ഷൻ സെന്ററുകൾ</span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{private_relief_and_collection_centers_count}} </span>
{% endif %}
</a>
<p class="home-info">
For emergency support, call the District control room at <b>1077</b> or the State control room at <b>1070</b>.<br>
Expand Down
8 changes: 8 additions & 0 deletions templates/ngo_volunteer.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{% extends 'base.html' %}
{% load static %}

{% block title %} Register as a Volunteer OR NGO | {% endblock %}


{% block content %}

{% load bootstrap3 %}
Expand Down Expand Up @@ -33,7 +37,9 @@ <h1 class="main-logo">kerala<span style="font-weight: normal;">rescue</span></h1
View Registered<br/> Volunteers
<span class="ml small"></span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{ registered_volunteers_count }} </span>
{% endif %}

</a>
<a href="{%url 'ngoregisterview' %}" class="home-button card" role="button">
Expand All @@ -49,7 +55,9 @@ <h1 class="main-logo">kerala<span style="font-weight: normal;">rescue</span></h1
View Registered<br/> NGO's/Company's
<span class="ml small"></span>
</span>
{% if home_page_setting.DISPLAY %}
<span class="badge badge-info"> Registered: {{ registered_ngo_count }} </span>
{% endif %}
</a>
<p class="home-info">
Contact Control Room or Disaster Management Cell or District Administration for any support.<br>
Expand Down

0 comments on commit bdf525b

Please sign in to comment.