Skip to content

Commit 0b560e1

Browse files
committed
fix: improve NGO website extraction and context handling in redirections
1 parent 8d496e5 commit 0b560e1

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

backend/donations/views/redirections.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,38 +128,7 @@ def get_context_data(self, cause_slug, **kwargs):
128128
is_donation_period_active = not now.date() > settings.DONATIONS_LIMIT
129129
donation_status = "open" if is_donation_period_active else "closed"
130130

131-
ngo_website_description = ""
132-
ngo_website = cause.ngo.website if cause.ngo.website else ""
133-
134-
if ngo_website:
135-
# try and parse the url to see if it's valid
136-
try:
137-
url_dict = urlparse(ngo_website)
138-
139-
if not url_dict.scheme:
140-
url_dict = url_dict._replace(scheme="http")
141-
142-
# if we have a netloc, then the URL is valid
143-
# use the netloc as the website name
144-
if url_dict.netloc:
145-
ngo_website_description = url_dict.netloc
146-
ngo_website = url_dict.geturl()
147-
148-
# of we don't have the netloc, when parsing the url
149-
# urlparse might send it to path
150-
# move that to netloc and remove the path
151-
elif url_dict.path:
152-
url_dict = url_dict._replace(netloc=url_dict.path)
153-
ngo_website_description = url_dict.path
154-
155-
url_dict = url_dict._replace(path="")
156-
157-
ngo_website = url_dict.geturl()
158-
else:
159-
raise
160-
161-
except Exception:
162-
ngo_website = None
131+
ngo_website, ngo_website_description = self._get_cause_website(cause)
163132

164133
context.update(
165134
{
@@ -188,6 +157,43 @@ def get_context_data(self, cause_slug, **kwargs):
188157

189158
return context
190159

160+
def _get_cause_website(self, cause: Cause) -> tuple[str, str]:
161+
"""
162+
Extracts the NGO website from the cause and returns a tuple with the full URL and a description.
163+
"""
164+
ngo_website_description = ""
165+
ngo_website = cause.ngo.website if cause.ngo.website else ""
166+
167+
try:
168+
url_dict = urlparse(ngo_website)
169+
170+
if not url_dict.scheme:
171+
url_dict = url_dict._replace(scheme="http")
172+
173+
# if we have a netloc, then the URL is valid
174+
# use the netloc as the website name
175+
if url_dict.netloc:
176+
ngo_website_description = url_dict.netloc
177+
ngo_website = url_dict.geturl()
178+
179+
# of we don't have the netloc, when parsing the url
180+
# urlparse might send it to path
181+
# move that to netloc and remove the path
182+
elif url_dict.path:
183+
url_dict = url_dict._replace(netloc=url_dict.path)
184+
ngo_website_description = url_dict.path
185+
186+
url_dict = url_dict._replace(path="")
187+
188+
ngo_website = url_dict.geturl()
189+
else:
190+
raise
191+
192+
except Exception:
193+
ngo_website = None
194+
195+
return ngo_website, ngo_website_description
196+
191197
def post(self, request, cause_slug):
192198
post = self.request.POST
193199

0 commit comments

Comments
 (0)