Skip to content

Commit

Permalink
optimize the homepage stats with a for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Dec 23, 2024
1 parent cc7009e commit a0e7201
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 100 deletions.
1 change: 1 addition & 0 deletions backend/donations/models/ngos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ALL_NGOS_CACHE_KEY = "ALL_NGOS"
ALL_NGO_IDS_CACHE_KEY = "ALL_NGO_IDS"
FRONTPAGE_NGOS_KEY = "FRONTPAGE_NGOS"
FRONTPAGE_STATS_KEY = "FRONTPAGE_NGOS_STATS"

REGISTRATION_NUMBER_REGEX = r"^([A-Z]{2}|)\d{2,10}$"
REGISTRATION_NUMBER_REGEX_SANS_VAT = r"^\d{2,10}$"
Expand Down
27 changes: 19 additions & 8 deletions backend/donations/views/site.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import random
from datetime import datetime
from typing import Dict
from typing import Dict, List, Union

from django.conf import settings
from django.db.models import QuerySet
from django.http import HttpResponse
from django.shortcuts import render
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView

from partners.models import DisplayOrderingChoices
from redirectioneaza.common.cache import cache_decorator

from ..models.donors import Donor
from ..models.ngos import ALL_NGO_IDS_CACHE_KEY, ALL_NGOS_CACHE_KEY, FRONTPAGE_NGOS_KEY, Ngo
from ..models.ngos import ALL_NGO_IDS_CACHE_KEY, ALL_NGOS_CACHE_KEY, FRONTPAGE_NGOS_KEY, FRONTPAGE_STATS_KEY, Ngo


class HomePage(TemplateView):
Expand All @@ -24,19 +25,29 @@ class HomePage(TemplateView):
def _get_list_of_ngo_ids() -> list:
return list(Ngo.active.values_list("id", flat=True))

@cache_decorator(timeout=settings.TIMEOUT_CACHE_SHORT, cache_key_prefix=FRONTPAGE_NGOS_KEY)
def _get_stats(self, now: datetime = None, ngo_queryset: QuerySet = None) -> Dict[str, int]:
@cache_decorator(timeout=settings.TIMEOUT_CACHE_SHORT, cache_key_prefix=FRONTPAGE_STATS_KEY)
def _get_stats(self, now: datetime = None, ngo_queryset: QuerySet = None) -> List[Dict[str, Union[str, int]]]:
if now is None:
now = timezone.now()

if ngo_queryset is None:
ngo_queryset = Ngo.active

start_of_year = datetime(now.year, 1, 1, 0, 0, 0, tzinfo=now.tzinfo)
return {
"ngos": ngo_queryset.count(),
"forms": Donor.objects.filter(date_created__gte=start_of_year).count(),
}
return [
{
"title": _("organizations registered in the platform"),
"value": ngo_queryset.count(),
},
{
"title": _("forms filled in") + " " + str(start_of_year.year),
"value": Donor.objects.filter(date_created__gte=start_of_year).count(),
},
{
"title": _("redirected to NGOs through the platform"),
"value": _("> €2 million"),
},
]

def get(self, request, *args, **kwargs):
now = timezone.now()
Expand Down
99 changes: 69 additions & 30 deletions backend/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-12-19 17:23+0200\n"
"POT-Creation-Date: 2024-12-23 10:55+0200\n"
"PO-Revision-Date: 2024-02-28 15:45+0000\n"
"Last-Translator: Tudor Amariei <tudor.amariei@commitglobal.org>\n"
"Language-Team: English <https://translate.commitglobal.org/projects/act4good/"
Expand Down Expand Up @@ -658,6 +658,24 @@ msgstr "Formularul tău de redirecționare"
msgid "Un nou formular de redirecționare"
msgstr "Un nou formular de redirecționare"

#: donations/views/site.py:40
msgid "organizations registered in the platform"
msgstr ""

#: donations/views/site.py:44
#, fuzzy
#| msgid "form with prefilled ngo data"
msgid "forms filled in"
msgstr "form with prefilled ngo data"

#: donations/views/site.py:48
msgid "redirected to NGOs through the platform"
msgstr ""

#: donations/views/site.py:49
msgid "> €2 million"
msgstr ""

#: frequent_questions/apps.py:8
#, fuzzy
#| msgid "Frequently Asked Questions"
Expand Down Expand Up @@ -700,7 +718,7 @@ msgstr ""
msgid "Questions"
msgstr "Options"

#: frequent_questions/views.py:330
#: frequent_questions/views.py:11 frequent_questions/views.py:17
msgid "Frequently Asked Questions"
msgstr "Frequently Asked Questions"

Expand Down Expand Up @@ -917,6 +935,14 @@ msgstr "Successful tasks"
msgid "Email address"
msgstr "Email address"

#: templates/v2/account/components/password-input.html:4
#: templates/v2/account/snippets/classic-login.html:37
#: templates/v2/account/snippets/register-form.html:56
#, fuzzy
#| msgid "old password"
msgid "Password"
msgstr "old password"

#: templates/v2/account/errors/login/app_missing.html:5
msgid "Application not activated"
msgstr "Application not activated"
Expand Down Expand Up @@ -970,6 +996,31 @@ msgstr ""
msgid "Send"
msgstr ""

#: templates/v2/account/set-password.html:19
#, fuzzy
#| msgid "Reset password"
msgid "Set a new password"
msgstr "Reset password"

#: templates/v2/account/set-password.html:30
#, fuzzy
#| msgid "Reset password"
msgid "New password"
msgstr "Reset password"

#: templates/v2/account/set-password.html:31
#: templates/v2/account/snippets/register-form.html:57
#, fuzzy
#| msgid "old password"
msgid "Confirm password"
msgstr "old password"

#: templates/v2/account/set-password.html:40
#, fuzzy
#| msgid "old password"
msgid "Change password"
msgstr "old password"

#: templates/v2/account/snippets/classic-login.html:12
msgid "Login with email & password"
msgstr "Login with email & password"
Expand Down Expand Up @@ -1014,23 +1065,11 @@ msgstr "first name"
msgid "Last name"
msgstr "last name"

#: templates/v2/account/snippets/register-form.html:59
#, fuzzy
#| msgid "old password"
msgid "Password"
msgstr "old password"

#: templates/v2/account/snippets/register-form.html:72
#, fuzzy
#| msgid "old password"
msgid "Confirm password"
msgstr "old password"

#: templates/v2/account/snippets/register-form.html:101
#: templates/v2/account/snippets/register-form.html:82
msgid "I agree with"
msgstr ""

#: templates/v2/account/snippets/register-form.html:103
#: templates/v2/account/snippets/register-form.html:84
msgid "the terms of the platform"
msgstr ""

Expand All @@ -1044,76 +1083,76 @@ msgstr "Go to account"
msgid "Access your account through NGO Hub"
msgstr "To access your account, you need to authenticate through NGO Hub."

#: templates/v2/account/snippets/third-party.html:32
#: templates/v2/account/snippets/third-party.html:31
msgid "To access your account, you need to authenticate through NGO Hub."
msgstr "To access your account, you need to authenticate through NGO Hub."

#: templates/v2/account/snippets/third-party.html:37
#: templates/v2/account/snippets/third-party.html:36
msgid ""
"If you're a non-profit organization, you can benefit from all the solutions "
"in the NGO-dedicated ecosystem through NGO Hub. To access redirectioneaza.ro "
"through your NGO Hub account, make sure:"
msgstr ""

#: templates/v2/account/snippets/third-party.html:56
#: templates/v2/account/snippets/third-party.html:49
msgid "You have an active account in NGO Hub"
msgstr ""

#: templates/v2/account/snippets/third-party.html:58
#: templates/v2/account/snippets/third-party.html:51
#, fuzzy
#| msgid "Login through NGO Hub"
msgid "Register your organization in NGO Hub"
msgstr "Login through NGO Hub"

#: templates/v2/account/snippets/third-party.html:63
#: templates/v2/account/snippets/third-party.html:55
msgid ""
"If you don't have an account in NGO Hub, you can create one for free. Learn "
"more about the benefits of NGO Hub"
msgstr ""

#: templates/v2/account/snippets/third-party.html:68
#: templates/v2/account/snippets/third-party.html:59
msgid "here"
msgstr ""

#: templates/v2/account/snippets/third-party.html:87
#: templates/v2/account/snippets/third-party.html:67
msgid "You have activated the Redirecționează platform in your NGO Hub account"
msgstr ""

#: templates/v2/account/snippets/third-party.html:90
#: templates/v2/account/snippets/third-party.html:69
msgid "Activate the Redirecționează application in your NGO Hub account"
msgstr ""

#: templates/v2/account/snippets/third-party.html:97
#: templates/v2/account/snippets/third-party.html:74
msgid ""
"Check with your organization's administrator if access to this platform is "
"correctly configured."
msgstr ""

#: templates/v2/account/snippets/third-party.html:100
#: templates/v2/account/snippets/third-party.html:76
msgid ""
"Look for the Redirecționează application in the All applications section. It "
"is one of the many applications offered for free to non-profit organizations "
"in Romania."
msgstr ""

#: templates/v2/account/snippets/third-party.html:120
#: templates/v2/account/snippets/third-party.html:85
msgid ""
"You have access permission from the organization's administrator in NGO Hub"
msgstr ""

#: templates/v2/account/snippets/third-party.html:123
#: templates/v2/account/snippets/third-party.html:87
#, fuzzy
#| msgid "To access your account, you need to authenticate through NGO Hub."
msgid "Give access to your colleagues in NGO Hub"
msgstr "To access your account, you need to authenticate through NGO Hub."

#: templates/v2/account/snippets/third-party.html:130
#: templates/v2/account/snippets/third-party.html:92
msgid ""
"Check with your organization's administrator if they have given you access "
"to the Redirecționează application."
msgstr ""

#: templates/v2/account/snippets/third-party.html:133
#: templates/v2/account/snippets/third-party.html:94
msgid ""
"In NGO Hub, each of your colleagues can have their own account. Make sure "
"you give your colleagues access to the applications they work with every day."
Expand Down
Loading

0 comments on commit a0e7201

Please sign in to comment.