Skip to content
Merged
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
8 changes: 6 additions & 2 deletions backend/hub/templates/hub/ngo/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ <h2 class="section-title uppercase">
<div class="container">
{% if page_obj %}

{% cache listing_cache_duration ngos_list listing_cache_key %}
<div class="is-multiline infinite-container">

{% if VOTING_DOMAIN_ENABLED and not SINGLE_DOMAIN_ROUND %}
Expand All @@ -58,8 +59,11 @@ <h2 class="section-title uppercase">
{% endif %}

</div>

{% include "hub/shared/pagination.html" with page_obj=page_obj domain=current_domain %}
{% endcache %}

{% comment %}
{% include "hub/shared/pagination.html" with page_obj=page_obj domain=current_domain %}
{% endcomment %}

{% else %}
<div class="content is-medium">
Expand Down
23 changes: 20 additions & 3 deletions backend/hub/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.contrib.messages.views import SuccessMessageMixin
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector, TrigramSimilarity
from django.contrib.sites.shortcuts import get_current_site
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.db.models import Count, Q, QuerySet
Expand Down Expand Up @@ -326,12 +327,26 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
orgs = self.search(self.get_qs())

context["current_search"] = self.request.GET.get("q", "")
context["current_search"] = self.request.GET.get("q", "").strip()[:100]
context["current_county"] = self.request.GET.get("county")
context["current_city"] = self.request.GET.get("city")
context["counties"] = orgs.order_by("county").values_list("county", flat=True).distinct("county")

# noinspection InsecureHash
param_hash = hashlib.sha256(
f"{context['current_county'] or ''}_{context['current_city'] or ''}_{context['current_search']}".encode()
).hexdigest()

context["listing_cache_duration"] = settings.TIMEOUT_CACHE_SHORT
context["listing_cache_key"] = f"orgs_listing_{param_hash}"
context_cache_key = f"orgs_listing_context_{param_hash}"

sentinel = object()
if cache.get(context_cache_key, sentinel) is not sentinel:
return context

context["counties"] = list(orgs.order_by("county").values_list("county", flat=True).distinct("county"))
context["counters"] = {
"ngos_accepted": Organization.objects.filter(status=Organization.STATUS.accepted).count(),
"ngos_accepted": int(Organization.objects.filter(status=Organization.STATUS.accepted).count()),
}

if self.request.GET.get("county"):
Expand All @@ -345,6 +360,8 @@ def get_context_data(self, **kwargs):

context["cities"] = set(orgs.values_list("city__id", "city__city"))

cache.set(context_cache_key, cache, settings.TIMEOUT_CACHE_SHORT)

return context


Expand Down