11from typing import TYPE_CHECKING
22
33from django .conf import settings
4+ from django .contrib .sites .models import Site
45from django .core .mail import EmailMessage
56
67from constance import config
7-
8- from hope_country_report .state import state
8+ from sentry_sdk import capture_exception
99
1010if TYPE_CHECKING :
1111 from hope_country_report .apps .core .models import User
1212 from hope_country_report .apps .power_query .models import ReportConfiguration , ReportDocument
13+ from hope_country_report .types .http import AuthHttpRequest
1314
1415
15- def send_document_password (user : "User" , document : "ReportDocument" ) -> int :
16+ def send_document_password (user : "User" , document : "ReportDocument" , request : "AuthHttpRequest | None" = None ) -> int :
1617 if config .CATCH_ALL_EMAIL :
1718 recipient_list = [config .CATCH_ALL_EMAIL ]
1819 else :
1920 recipient_list = [user .email ]
2021 if not recipient_list :
2122 return 0
2223 url = document .get_absolute_url ()
23- if state .request :
24- url = state .request .build_absolute_uri (url )
24+ if request :
25+ url = request .build_absolute_uri (url )
26+ else :
27+ try :
28+ domain = Site .objects .get_current ().domain
29+ scheme = "https" if not settings .DEBUG else "http"
30+ url = f"{ scheme } ://{ domain } { url } "
31+ except Exception as e :
32+ capture_exception (e )
2533
2634 message = EmailMessage (to = recipient_list , from_email = settings .DEFAULT_FROM_EMAIL )
2735
@@ -46,10 +54,19 @@ def send_document_password(user: "User", document: "ReportDocument") -> int:
4654 return message .send ()
4755
4856
49- def send_request_access (sender : "User" , report : "ReportConfiguration" , message : str = "" ) -> int :
57+ def send_request_access (
58+ sender : "User" , report : "ReportConfiguration" , message : str = "" , request : "AuthHttpRequest | None" = None
59+ ) -> int :
5060 url = report .get_absolute_url ()
51- if state .request :
52- url = state .request .build_absolute_uri (url )
61+ if request :
62+ url = request .build_absolute_uri (url )
63+ else :
64+ try :
65+ domain = Site .objects .get_current ().domain
66+ scheme = "https" if not settings .DEBUG else "http"
67+ url = f"{ scheme } ://{ domain } { url } "
68+ except Exception as e :
69+ capture_exception (e )
5370
5471 if config .CATCH_ALL_EMAIL :
5572 recipient_list = [config .CATCH_ALL_EMAIL ]
@@ -79,14 +96,21 @@ def send_request_access(sender: "User", report: "ReportConfiguration", message:
7996 return email .send ()
8097
8198
82- def notify_report_completion (report : "ReportConfiguration" ) -> int :
99+ def notify_report_completion (report : "ReportConfiguration" , request : "AuthHttpRequest | None" = None ) -> int :
83100 url = report .get_absolute_url ()
84- if state .request :
85- url = state .request .build_absolute_uri (url )
86-
87101 docs_url = report .get_documents_url ()
88- if state .request :
89- docs_url = state .request .build_absolute_uri (docs_url )
102+
103+ if request :
104+ url = request .build_absolute_uri (url )
105+ docs_url = request .build_absolute_uri (docs_url )
106+ else :
107+ try :
108+ domain = Site .objects .get_current ().domain
109+ scheme = "https" if not settings .DEBUG else "http"
110+ url = f"{ scheme } ://{ domain } { url } "
111+ docs_url = f"{ scheme } ://{ domain } { docs_url } "
112+ except Exception as e :
113+ capture_exception (e )
90114
91115 if config .CATCH_ALL_EMAIL :
92116 recipient_list = [config .CATCH_ALL_EMAIL ]
0 commit comments