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/5w dashboard none values #503

Merged
merged 2 commits into from
Dec 17, 2024
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
10 changes: 7 additions & 3 deletions src/core/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

# ALLOWED_HOSTS=reporthub.immap.org,www.reporthub.immap.org
# env.list() splits comma-separated string into a list
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=["localhost","dev.reporthub.immap.org", "www.dev.reporthub.immap.org"])
ALLOWED_HOSTS = env.list(
"ALLOWED_HOSTS", default=["localhost", "dev.reporthub.immap.org", "www.dev.reporthub.immap.org"]
)

# HSTS settings
SECURE_HSTS_SECONDS = env("SECURE_HSTS_SECONDS", default=31536000)
Expand All @@ -16,7 +18,9 @@
SESSION_COOKIE_SECURE = env("SESSION_COOKIE_SECURE", default=True)
CSRF_COOKIE_SECURE = env("CSRF_COOKIE_SECURE", default=True)
SECURE_SSL_REDIRECT = env("SECURE_SSL_REDIRECT", default=True)
CSRF_TRUSTED_ORIGINS = env("CSRF_TRUSTED_ORIGINS",default=["http://localhost","https://localhost","https://127.0.0.1","http://127.0.0.1"])
CSRF_TRUSTED_ORIGINS = env(
"CSRF_TRUSTED_ORIGINS", default=["http://localhost", "https://localhost", "https://127.0.0.1", "http://127.0.0.1"]
)

SENTRY_DSN = env("SENTRY_DSN", default="")

Expand Down Expand Up @@ -119,4 +123,4 @@
}
}

DJANGO_SUPERUSER_PASSWORD = env("DJANGO_SUPERUSER_PASSWORD",default="change-this")
DJANGO_SUPERUSER_PASSWORD = env("DJANGO_SUPERUSER_PASSWORD", default="change-this")
1 change: 1 addition & 0 deletions src/guides/factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import factory
from factory.django import DjangoModelFactory

from users.factory import UserFactory

from .models import Feedback, Guide, Section
Expand Down
4 changes: 2 additions & 2 deletions src/project_reports/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from openpyxl.styles import Font, NamedStyle

from project_reports.filters import MonthlyReportsFilter
from rh.models import Cluster, Organization, Project
from stock.models import StockMonthlyReport, StockReport
from stock.utils import write_csv_columns_and_rows
from users.utils import is_cluster_lead

from project_reports.filters import MonthlyReportsFilter

from .models import ActivityPlanReport, DisaggregationLocationReport, ProjectMonthlyReport, TargetLocationReport
from .utils import write_projects_reports_to_csv

Expand Down
1 change: 1 addition & 0 deletions src/project_reports/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import django_filters
from django import forms

from rh.models import (
ActivityDomain,
ActivityType,
Expand Down
1 change: 1 addition & 0 deletions src/project_reports/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.forms.models import inlineformset_factory
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy

from rh.models import ActivityPlan, Disaggregation, Indicator, TargetLocation

from .models import (
Expand Down
1 change: 1 addition & 0 deletions src/project_reports/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models

from rh.models import (
ActivityPlan,
Cluster,
Expand Down
4 changes: 2 additions & 2 deletions src/project_reports/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from openpyxl.styles import Font, NamedStyle
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.datavalidation import DataValidation

from project_reports.models import ResponseType
from rh.models import (
Currency,
Disaggregation,
Expand All @@ -16,8 +18,6 @@
UnitType,
)

from project_reports.models import ResponseType

header_style = NamedStyle(name="header")
header_style.font = Font(bold=True)

Expand Down
67 changes: 18 additions & 49 deletions src/project_reports/views/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.db.models import Count, Sum
from django.db.models import Count, Sum, Value
from django.db.models.functions import Coalesce
from django.shortcuts import get_object_or_404, render
from rh.models import Cluster, Organization
from users.utils import is_cluster_lead

from project_reports.filters import Organization5WFilter
from project_reports.models import ProjectMonthlyReport
from rh.models import Cluster, Organization
from users.utils import is_cluster_lead


@login_required
Expand Down Expand Up @@ -66,11 +67,15 @@ def cluster_5w_dashboard(request, cluster):
.order_by("from_date")
.values("from_date")
.annotate(
total_people_reached=Sum("activityplanreport__targetlocationreport__disaggregationlocationreport__reached")
total_people_reached=Coalesce(
Sum("activityplanreport__targetlocationreport__disaggregationlocationreport__reached"), Value(0)
)
)
)

counts["people_reached"] = sum(report["total_people_reached"] for report in people_reached_data)
counts["people_reached"] = 0
for report in people_reached_data:
counts["people_reached"] += report["total_people_reached"]

labels = [report["from_date"].strftime("%b") for report in people_reached_data]
data = [
Expand Down Expand Up @@ -164,48 +169,6 @@ def org_5w_dashboard(request, code):
if cluster_code:
filter_params["project__clusters__code__in"] = [cluster_code]

# counts = ProjectMonthlyReport.objects.filter(**filter_params).aggregate(
# report_indicators_count=Count("activityplanreport__activity_plan__indicator", distinct=True),
# report_implementing_partners_count=Count(
# "activityplanreport__targetlocationreport__target_location__implementing_partner", distinct=True
# ),
# report_target_location_province_count=Count(
# "activityplanreport__targetlocationreport__target_location__province", distinct=True
# ),
# )

# people_reached_data = (
# ProjectMonthlyReport.objects.filter(
# **filter_params,
# )
# .order_by("from_date")
# .values("from_date")
# .annotate(
# total_people_reached=Sum("activityplanreport__targetlocationreport__disaggregationlocationreport__reached")
# )
# )

# people reached by activities
# activity_domains = (
# ProjectMonthlyReport.objects.filter(
# **filter_params,
# )
# .values_list("activityplanreport__activity_plan__activity_domain__name", flat=True)
# .distinct()
# )

# reach_by_activity = (
# ProjectMonthlyReport.objects.filter(
# **filter_params,
# )
# .values(
# "activityplanreport__targetlocationreport__disaggregationlocationreport__disaggregation__name",
# "activityplanreport__activity_plan__activity_domain__name",
# )
# .annotate(
# total_people_reached=Sum("activityplanreport__targetlocationreport__disaggregationlocationreport__reached"),
# )
# )
counts = monthly_reports.aggregate(
report_indicators_count=Count("activityplanreport__activity_plan__indicator", distinct=True),
report_implementing_partners_count=Count(
Expand All @@ -219,19 +182,25 @@ def org_5w_dashboard(request, code):
monthly_reports.order_by("from_date")
.values("from_date")
.annotate(
total_people_reached=Sum("activityplanreport__targetlocationreport__disaggregationlocationreport__reached")
total_people_reached=Coalesce(
Sum("activityplanreport__targetlocationreport__disaggregationlocationreport__reached"), Value(0)
)
)
)
counts["people_reached"] = sum(report["total_people_reached"] for report in people_reached_data)
counts["people_reached"] = 0
for report in people_reached_data:
counts["people_reached"] += report["total_people_reached"]

labels = [report["from_date"].strftime("%b") for report in people_reached_data]
data = [
report["total_people_reached"] if report["total_people_reached"] is not None else 0
for report in people_reached_data
]

activity_domains = monthly_reports.values_list(
"activityplanreport__activity_plan__activity_domain__name", flat=True
).distinct()

reach_by_activity = monthly_reports.values(
"activityplanreport__targetlocationreport__disaggregationlocationreport__disaggregation__name",
"activityplanreport__activity_plan__activity_domain__name",
Expand Down
1 change: 1 addition & 0 deletions src/project_reports/views/monthly_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.utils import timezone
from django_htmx.http import HttpResponseClientRedirect
from extra_settings.models import Setting

from rh.models import (
ActivityDetail,
ActivityDomain,
Expand Down
1 change: 1 addition & 0 deletions src/project_reports/views/report_activity_plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.urls import reverse, reverse_lazy
from django.utils.safestring import mark_safe
from django_htmx.http import HttpResponseClientRedirect

from rh.models import ActivityPlan

from ..forms import (
Expand Down
1 change: 1 addition & 0 deletions src/project_reports/views/report_target_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.urls import reverse, reverse_lazy
from django.utils.safestring import mark_safe
from django_htmx.http import HttpResponseClientRedirect

from rh.models import (
DisaggregationLocation,
Project,
Expand Down
1 change: 1 addition & 0 deletions src/project_reports/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.urls import reverse
from django.views.decorators.http import require_http_methods
from openpyxl import Workbook

from rh.models import (
ActivityDomain,
ActivityPlan,
Expand Down
1 change: 1 addition & 0 deletions src/rh/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytz
from factory.django import DjangoModelFactory
from factory.faker import faker

from users.factory import UserFactory

from .models import (
Expand Down
2 changes: 1 addition & 1 deletion src/rh/management/commands/load_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.db import connection
from django.db.models import Q
from django.utils import timezone
from users.models import Profile

from rh.models import (
ActivityDetail,
Expand All @@ -29,6 +28,7 @@
TransferMechanismType,
UnitType,
)
from users.models import Profile

BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent

Expand Down
1 change: 1 addition & 0 deletions src/rh/signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.models import Group
from django.db.models.signals import post_save
from django.dispatch import receiver

from users.utils import assign_default_permissions_to_group

from .models import Cluster
Expand Down
2 changes: 1 addition & 1 deletion src/rh/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase
from django.urls import reverse
from users.models import Profile

from rh.models import Cluster, Location, Organization
from users.models import Profile


class TestLoggedInViews(TestCase):
Expand Down
24 changes: 17 additions & 7 deletions src/rh/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
from django.urls import path
from django.conf import settings
from django.urls import path

from .views import (
budget_progress as budget_progress,
activity_plans as activity_plans,
projects as projects,
exports as export_views,
)
from .views import (
budget_progress as budget_progress,
)
from .views import (
clusters as clusters_views,
)
from .views import (
exports as export_views,
)
from .views import (
organizations as organizations,
)
from .views import (
projects as projects,
)
from .views import (
target_locations as target_locations,
)
from .views.views import (
Expand Down Expand Up @@ -225,6 +237,4 @@
]

if settings.DEBUG:
urlpatterns.append(
path(route="test-email/<str:template_name>",view=test_email,name="test-email")
)
urlpatterns.append(path(route="test-email/<str:template_name>", view=test_email, name="test-email"))
2 changes: 1 addition & 1 deletion src/rh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from datetime import datetime

from django.contrib.auth.models import User
from users.utils import is_cluster_lead

from rh.models import Project, TargetLocation
from users.utils import is_cluster_lead


def has_permission(user: User, project: Project = None, clusters: list = [], permission: str = ""):
Expand Down
2 changes: 1 addition & 1 deletion src/rh/views/budget_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
def create_project_budget_progress_view(request, project):
project = get_object_or_404(Project, pk=project)
financial_report = project.budgetprogress_set.all()

if request.method == "POST":
form = BudgetProgressForm(request.POST, project=project)
country = request.POST.get("country")
Expand Down
1 change: 1 addition & 0 deletions src/rh/views/clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db.models import Count
from django.shortcuts import get_object_or_404, render
from extra_settings.models import Setting

from project_reports.filters import MonthlyReportsFilter
from project_reports.models import ProjectMonthlyReport

Expand Down
1 change: 1 addition & 0 deletions src/rh/views/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from openpyxl import Workbook
from openpyxl.styles import Font, NamedStyle
from openpyxl.utils import get_column_letter

from rh.utils import DateTimeEncoder

from ..filters import ProjectsFilter
Expand Down
1 change: 1 addition & 0 deletions src/rh/views/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.exceptions import PermissionDenied
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, render

from rh.models import Organization, TargetLocation
from users.utils import is_cluster_lead

Expand Down
1 change: 1 addition & 0 deletions src/rh/views/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.views.decorators.http import require_http_methods
from django_htmx.http import HttpResponseClientRedirect
from extra_settings.models import Setting

from project_reports.models import ProjectMonthlyReport

from ..filters import ProjectsFilter
Expand Down
8 changes: 5 additions & 3 deletions src/rh/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@
from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.cache import cache_page

from project_reports.models import ProjectMonthlyReport
from users.decorators import unauthenticated_user

from ..models import ActivityDomain, ActivityType, Cluster, Indicator, Location, Project


def test_email(request,template_name):
def test_email(request, template_name):
"""
Route: /test-email/{template_name}
template_name format: <app_name>-<folder_name>-<file_name>
Example: test-email/rh-emails-pending_report
"""
context = {}

template_name = template_name.replace("-","/")
template_name = template_name.replace("-", "/")

return render(request=request, template_name=f"{template_name}.html", context=context)

return render(request=request,template_name=f"{template_name}.html",context=context)

@login_required
def home(request):
Expand Down
Loading
Loading