|
6 | 6 | from django.conf import settings
|
7 | 7 | from django.contrib import messages
|
8 | 8 | from django.core.files import File
|
9 |
| -from django.db.models import Prefetch |
| 9 | +from django.db.models import Prefetch, QuerySet |
10 | 10 | from django.http import Http404, JsonResponse
|
11 | 11 | from django.shortcuts import get_object_or_404, redirect, render
|
12 | 12 | from django.urls import reverse
|
@@ -80,36 +80,25 @@ def get_context_data(self, cause_slug, **kwargs):
|
80 | 80 |
|
81 | 81 | request = self.request
|
82 | 82 |
|
83 |
| - main_cause_qs = Cause.objects.filter(is_main=True).only("id", "slug", "name", "description") |
| 83 | + main_cause_qs: QuerySet[Cause] = Cause.objects.filter(is_main=True).only("id", "slug", "name", "description") |
| 84 | + |
| 85 | + prefetch_qs = Cause.active.select_related("ngo").prefetch_related( |
| 86 | + Prefetch( |
| 87 | + lookup="ngo__causes", |
| 88 | + queryset=main_cause_qs, |
| 89 | + to_attr="main_cause_list", |
| 90 | + ) |
| 91 | + ) |
84 | 92 |
|
85 | 93 | cause: Cause = get_object_or_404(
|
86 |
| - Cause.active.select_related("ngo").prefetch_related( |
87 |
| - Prefetch( |
88 |
| - lookup="ngo__causes", |
89 |
| - queryset=main_cause_qs, |
90 |
| - to_attr="main_cause_list", |
91 |
| - ) |
92 |
| - ), |
| 94 | + prefetch_qs, |
93 | 95 | slug=cause_slug,
|
94 | 96 | )
|
95 | 97 |
|
96 | 98 | user: User = request.user
|
97 |
| - if cause.visibility == CauseVisibilityChoices.PRIVATE: |
98 |
| - # if the cause is private, we need to check if the user is logged in |
99 |
| - # and if the user is allowed to see the cause |
100 |
| - if not user.is_authenticated: |
101 |
| - raise Http404("Cause not found") |
| 99 | + self._check_cause_visibility(cause, user) |
102 | 100 |
|
103 |
| - if not user.is_staff and cause.ngo != user.ngo: |
104 |
| - raise Http404("Cause not found") |
105 |
| - |
106 |
| - # if we didn't find it or the ngo doesn't have an active page |
107 |
| - if (ngo := cause.ngo) is None: |
108 |
| - logger.exception(f"NGO not found for cause {cause.pk}") |
109 |
| - raise Http404 |
110 |
| - |
111 |
| - if not ngo.can_create_causes: |
112 |
| - raise Http404 |
| 101 | + ngo = self._get_ngo_or_404(cause) |
113 | 102 |
|
114 | 103 | # noinspection PyUnresolvedReferences
|
115 | 104 | main_cause = cause if cause.is_main else (ngo.main_cause_list[0] if ngo.main_cause_list else None)
|
@@ -157,6 +146,27 @@ def get_context_data(self, cause_slug, **kwargs):
|
157 | 146 |
|
158 | 147 | return context
|
159 | 148 |
|
| 149 | + def _get_ngo_or_404(self, cause: Cause) -> Ngo: |
| 150 | + # if we didn't find it or the ngo doesn't have an active page |
| 151 | + if (ngo := cause.ngo) is None: |
| 152 | + logger.exception(f"NGO not found for cause {cause.pk}") |
| 153 | + raise Http404 |
| 154 | + |
| 155 | + if not ngo.can_create_causes: |
| 156 | + raise Http404 |
| 157 | + |
| 158 | + return ngo |
| 159 | + |
| 160 | + def _check_cause_visibility(self, cause: Cause, user: User): |
| 161 | + if cause.visibility == CauseVisibilityChoices.PRIVATE: |
| 162 | + # if the cause is private, we need to check if the user is logged in |
| 163 | + # and if the user is allowed to see the cause |
| 164 | + if not user.is_authenticated: |
| 165 | + raise Http404("Cause not found") |
| 166 | + |
| 167 | + if not user.is_staff and cause.ngo != user.ngo: |
| 168 | + raise Http404("Cause not found") |
| 169 | + |
160 | 170 | def _get_cause_website(self, cause: Cause) -> tuple[str, str]:
|
161 | 171 | """
|
162 | 172 | Extracts the NGO website from the cause and returns a tuple with the full URL and a description.
|
|
0 commit comments