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

Fix #1030. Display volunteer count on home page #1036

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion floodrelief/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,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 @@ -255,3 +255,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',
}
91 changes: 87 additions & 4 deletions mainapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
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
from django.db.models import Q
from django.urls import reverse
from django.core.exceptions import ValidationError
from django.db.models.signals import post_save
from django.core.cache.utils import make_template_fragment_key
from django.core.cache import cache
from django.dispatch import receiver
from django.utils import timezone

from django.utils.functional import cached_property

districts = (
('alp','Alappuzha - ആലപ്പുഴ'),
Expand Down Expand Up @@ -189,6 +191,15 @@ def __str__(self):
def is_old(self):
return self.dateadded < (timezone.now() - timezone.timedelta(days=2))

@classmethod
def request_for_rescue(cls):
return cls._default_manager.filter(needrescue=True).count()

@classmethod
def request_for_resource(cls):
flt = Q(needwater=True) | Q(needfood=True) | Q(needcloth=True) | Q(needcloth=True) | Q(needmed=True) | Q(needtoilet=True) | Q(needkit_util=True)
return cls._default_manager.filter(flt).distinct('id').count()


class Volunteer(models.Model):
district = models.CharField(
Expand Down Expand Up @@ -220,6 +231,10 @@ class Meta:
def __str__(self):
return self.name

@classmethod
def count(cls):
return cls._default_manager.count()


class NGO(models.Model):
district = models.CharField(
Expand Down Expand Up @@ -251,6 +266,10 @@ class Meta:
def __str__(self):
return self.name

@classmethod
def count(cls):
return cls._default_manager.count()


class Contributor(models.Model):
district = models.CharField(
Expand Down Expand Up @@ -283,6 +302,10 @@ class Meta:
def __str__(self):
return self.name + ' ' + self.get_district_display()

@classmethod
def count(cls):
return cls._default_manager.count()


class DistrictManager(models.Model):
district = models.CharField(
Expand All @@ -301,14 +324,18 @@ class Meta:
def __str__(self):
return self.name + ' ' + self.get_district_display()

@classmethod
def count(cls):
return cls._default_manager.count()


class DistrictNeed(models.Model):
district = models.CharField(
max_length = 15,
choices = districts,
)
needs = models.TextField(verbose_name="Items required")
cnandpts = models.TextField(verbose_name="Contacts and collection points") #contacts and collection points
cnandpts = models.TextField(verbose_name="Contacts and collection points") #contacts and collection points

class Meta:
verbose_name = 'District: Need'
Expand All @@ -317,6 +344,10 @@ class Meta:
def __str__(self):
return self.get_district_display()

@classmethod
def count(cls):
return cls._default_manager.count()


class DistrictCollection(models.Model):
district = models.CharField(
Expand Down Expand Up @@ -394,10 +425,13 @@ def district_name(self):
'wnd':'Wayanad - വയനാട്',
}.get(self.district, 'Unknown')


def __str__(self):
return self.name

@classmethod
def count(cls):
return cls._default_manager.filter(status="active").count()


@receiver(post_save, sender=RescueCamp)
def expire_people_filter_form(sender, **kwargs):
Expand Down Expand Up @@ -445,10 +479,14 @@ class Meta:
verbose_name = 'Private Relief: Camp'
verbose_name_plural = "Private Relief: Camps"


def __str__(self):
return self.name

@classmethod
def count(cls):
return cls._default_manager.filter(status="active").count()



class Person(models.Model):
name = models.CharField(max_length=51,blank=False,null=False,verbose_name="Name - പേര്")
Expand Down Expand Up @@ -533,6 +571,10 @@ def save(self, *args, **kwargs):
if(Person.objects.filter(unique_identifier = self.unique_identifier).count() == 0 ):
super(Person, self).save(*args, **kwargs)

@classmethod
def count(cls):
return cls._default_manager.count()



def upload_to(instance, filename):
Expand Down Expand Up @@ -562,6 +604,11 @@ def __str__(self):
return self.description[:100]


@classmethod
def count(cls):
return cls._default_manager.count()


class DataCollection(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
document_name = models.CharField(
Expand Down Expand Up @@ -641,6 +688,10 @@ def __str__(self):
def get_absolute_url(self):
return reverse('collection_centers_list')

@classmethod
def count(cls):
return cls._default_manager.count()


class CsvBulkUpload(models.Model):
name = models.CharField(max_length=20)
Expand Down Expand Up @@ -675,6 +726,7 @@ def full_clean(self, *args, **kwargs):
def __str__(self):
return self.name


class Hospital(models.Model):
district = models.CharField(
max_length = 15,
Expand All @@ -693,3 +745,34 @@ class Hospital(models.Model):

def __str__(self):
return self.name + ' - ' + self.designation

@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):
jerinisready marked this conversation as resolved.
Show resolved Hide resolved
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'))

53 changes: 50 additions & 3 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 @@ -49,6 +54,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 @@ -205,10 +212,48 @@ class RegisterContributor(CreateView):
class HomePageView(TemplateView):
template_name = "home.html"

def get_context_data(self, **kwargs):

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):

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


class MapView(TemplateView):
template_name = "mapview.html"
Expand Down Expand Up @@ -262,7 +307,8 @@ def __init__(self, *args, **kwargs):


def relief_camps(request):
return render(request,"mainapp/relief_camps.html")
context = {'count': RescueCamp.count(), }
return render(request,"mainapp/relief_camps.html", context=context)


def missing_persons(request):
Expand All @@ -275,7 +321,7 @@ def relief_camps_list(request):
paginator = Paginator(relief_camps,50)
page = request.GET.get('page')
data = paginator.get_page(page)
return render(request, 'mainapp/relief_camps_list.html', {'filter': filter, 'data': data})
return render(request, 'mainapp/relief_camps_list.html', {'filter': filter, 'data': data, 'count': paginator.count})

class RequestFilter(django_filters.FilterSet):
class Meta:
Expand Down Expand Up @@ -834,13 +880,14 @@ def dispatch(self, request, *args, **kwargs):
class ConsentSuccess(TemplateView):
template_name = "mainapp/volunteer_consent_success.html"


def camp_requirements_list(request):
filter = CampRequirementsFilter(request.GET, queryset=RescueCamp.objects.all())
camp_data = filter.qs.order_by('name')
paginator = Paginator(camp_data, 50)
page = request.GET.get('page')
data = paginator.get_page(page)
return render(request, "mainapp/camp_requirements_list.html", {'filter': filter , 'data' : data})
return render(request, "mainapp/camp_requirements_list.html", {'filter': filter , 'data': data})


class RequestUpdateView(CreateView):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ six==1.11.0
whitenoise==4.0
django-redis==4.9.0
requests==2.19.1
redis==2.10.0
rq==0.12.0
pillow==5.2.0
boto3==1.7.80
Expand Down
Loading