Skip to content

Commit

Permalink
Merge pull request rapidpro#986 from rapidpro/optimization-debug
Browse files Browse the repository at this point in the history
Update and activate django debug toolbar
  • Loading branch information
norkans7 authored Apr 5, 2022
2 parents af15398 + 04bb974 commit 1a40bfc
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 291 deletions.
489 changes: 243 additions & 246 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ readme = "README.md"
python = "^3.9"
Django = "^4.0.0"
smartmin = "^4.0.0"
rapidpro-dash = "^1.9.1"
rapidpro-dash = "^1.9.3"
colorama = "^0.4.3"
django-compressor = "^3.0"
django-debug-toolbar = "^1.9.1"
django-debug-toolbar = "3.2.4"
django-digest = "^1.13"
django-rosetta = "^0.8.1"
django-storages = "^1.6.6"
Expand Down Expand Up @@ -43,7 +43,7 @@ celery = {extras = ["redis"], version = ">4.4.6"}


[tool.poetry.dev-dependencies]
black = "^21.10b0"
black = "^22.3"
isort = "^5.9.3"
flake8 = "^3.7.9"
codecov = "^2.1.11"
Expand Down
1 change: 0 additions & 1 deletion templates/public/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
- load thumbnail humanize ureport compress i18n dashblocks

- block content
- load_qbs request.org 'photos'
// latest poll if we have it
- if latest_poll and latest_poll.get_first_question
.max-w-page.mx-auto.bg-white.mt-6
Expand Down
1 change: 0 additions & 1 deletion ureport/assets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def test_assets_image(self):

response = self.client.get(list_url, SERVER_NAME="uganda.ureport.io")
self.assertEqual(len(response.context["object_list"]), Image.objects.filter(org=self.uganda).count())
self.assertTrue(isinstance(response.context["pattern_bg"], Image))

response = self.client.get(nigeria_bg_update_url, SERVER_NAME="nigeria.ureport.io")
self.assertEqual(response.request["PATH_INFO"], nigeria_bg_update_url)
Expand Down
33 changes: 30 additions & 3 deletions ureport/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.contrib.auth.models import User
from django.core.cache import cache
from django.db import connection, models
from django.db.models import Count, F, Q, Sum
from django.db.models import Count, F, Prefetch, Q, Sum
from django.db.models.functions import Lower
from django.utils import timezone
from django.utils.html import strip_tags
Expand Down Expand Up @@ -521,7 +521,28 @@ def get_main_poll(cls, org):
cached_value = cache.get(Poll.ORG_MAIN_POLL_ID % org.id, None)
main_poll = None
if cached_value:
main_poll = Poll.objects.filter(is_active=True, id=cached_value, org=org).first()
main_poll = (
Poll.objects.filter(is_active=True, id=cached_value, org=org)
.prefetch_related(
Prefetch(
"questions",
to_attr="prefetched_questions",
queryset=PollQuestion.objects.filter(poll_id=cached_value, is_active=True)
.select_related("flow_result")
.prefetch_related(
Prefetch(
"response_categories",
queryset=PollResponseCategory.objects.filter(is_active=True).exclude(
flow_result_category__category__icontains="no response"
),
to_attr="prefetched_response_categories",
)
)
.order_by("-priority", "pk"),
)
)
.first()
)

if main_poll:
return main_poll
Expand Down Expand Up @@ -607,7 +628,10 @@ def get_featured_responses(self):
return self.featured_responses.filter(is_active=True).order_by("-created_on")

def get_first_question(self):
questions = self.get_questions()
if hasattr(self, "prefetched_questions"):
questions = self.prefetched_questions
else:
questions = self.get_questions()

for question in questions:
if not question.is_open_ended():
Expand Down Expand Up @@ -1132,6 +1156,9 @@ def get_total_summary_data(self):
return dict()

def is_open_ended(self):
if hasattr(self, "prefetched_response_categories"):
return len(self.prefetched_response_categories) == 1

return (
self.response_categories.filter(is_active=True)
.exclude(flow_result_category__category__icontains="no response")
Expand Down
44 changes: 8 additions & 36 deletions ureport/public/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ def get_context_data(self, **kwargs):

context["featured_bots"] = Bot.objects.filter(is_active=True, org=org, featured=True).order_by("-priority")

context["main_stories"] = (
Story.objects.filter(org=org, featured=True, is_active=True)
.filter(Q(attachment="") | Q(attachment=None))
.order_by("-created_on")
)
context["main_stories"] = Story.get_main_stories(org, 5)

return context

Expand Down Expand Up @@ -212,7 +208,7 @@ def get_context_data(self, **kwargs):
partners_logos = Image.objects.filter(org=org, is_active=True, image_type="A").order_by("-priority")
context["partners_logos"] = partners_logos

context["main_stories"] = Story.objects.filter(org=org, featured=True, is_active=True).order_by("-created_on")
context["main_stories"] = Story.get_main_stories(org, 5)
return context


Expand Down Expand Up @@ -291,11 +287,7 @@ def get_context_data(self, **kwargs):

context["polls"] = polls

context["main_stories"] = (
Story.objects.filter(org=org, featured=True, is_active=True)
.filter(Q(attachment="") | Q(attachment=None))
.order_by("-created_on")
)
context["main_stories"] = Story.get_main_stories(org, 5)
return context


Expand Down Expand Up @@ -353,11 +345,7 @@ def get_context_data(self, **kwargs):
.order_by("title")
)

featured_stories = (
Story.objects.filter(org=org, featured=True, is_active=True)
.filter(Q(attachment="") | Q(attachment=None))
.order_by("-created_on")
)
featured_stories = Story.get_main_stories(org, 5)
context["main_stories"] = featured_stories

return context
Expand Down Expand Up @@ -410,11 +398,7 @@ def get_context_data(self, **kwargs):
context["org"] = org
context["categories"] = Category.objects.filter(org=org, is_active=True).order_by("name")

context["main_stories"] = (
Story.objects.filter(org=org, featured=True, is_active=True)
.filter(Q(attachment="") | Q(attachment=None))
.order_by("-created_on")
)
context["main_stories"] = Story.get_main_stories(org, 5)
return context


Expand Down Expand Up @@ -499,11 +483,7 @@ def get_context_data(self, **kwargs):
context["scheme_bar_height"] = (50 * len(scheme_stats)) + 30
context["schemes_stats"] = scheme_stats
context["reporters"] = org.get_reporters_count()
context["main_stories"] = (
Story.objects.filter(org=org, featured=True, is_active=True)
.filter(Q(attachment="") | Q(attachment=None))
.order_by("-created_on")
)
context["main_stories"] = Story.get_main_stories(org, 5)

# global counter
context["global_counter"] = get_global_count()
Expand Down Expand Up @@ -540,11 +520,7 @@ def get_context_data(self, **kwargs):
context = super(JoinEngageView, self).get_context_data(**kwargs)
org = self.request.org
context["org"] = org
context["main_stories"] = (
Story.objects.filter(org=org, featured=True, is_active=True)
.filter(Q(attachment="") | Q(attachment=None))
.order_by("-created_on")
)
context["main_stories"] = Story.get_main_stories(org, 5)
return context


Expand Down Expand Up @@ -576,11 +552,7 @@ def get_context_data(self, **kwargs):
context["job_sources"] = JobSource.objects.filter(org=org, is_active=True).order_by(
"-is_featured", "-created_on"
)
context["main_stories"] = (
Story.objects.filter(org=org, featured=True, is_active=True)
.filter(Q(attachment="") | Q(attachment=None))
.order_by("-created_on")
)
context["main_stories"] = Story.get_main_stories(org, 5)
return context


Expand Down
3 changes: 2 additions & 1 deletion ureport/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
"django.middleware.locale.LocaleMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"dash.orgs.middleware.SetOrgMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
)

X_FRAME_OPTIONS = "DENY"
Expand Down Expand Up @@ -621,6 +622,7 @@
"django.contrib.humanize",
# the django admin
"django.contrib.admin",
"debug_toolbar",
# compress our CSS and js
"compressor",
# thumbnail
Expand Down Expand Up @@ -712,7 +714,6 @@
"django.template.context_processors.request",
"dash.orgs.context_processors.user_group_perms_processor",
"dash.orgs.context_processors.set_org_processor",
"ureport.assets.context_processors.set_assets_processor",
"ureport.public.context_processors.set_has_better_domain",
"ureport.public.context_processors.set_is_iorg",
"ureport.public.context_processors.set_linked_sites",
Expand Down
4 changes: 4 additions & 0 deletions ureport/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ def chunk_list(iterable, size):


def get_logo(org):
if hasattr(org, "_logo_field"):
return org._logo_field

logo_field = org.logo
logo = Image.objects.filter(org=org, is_active=True, image_type=LOGO).first()
if logo:
logo_field = logo.image
org._logo_field = logo_field
return logo_field


Expand Down

0 comments on commit 1a40bfc

Please sign in to comment.