Coverage for apps/communications/management/commands/preview_emails.py: 0%

22 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-04 17:24 +0000

1"""Write every system email as an HTML file to look at in a browser (brand-email-template). 

2 

3 python manage.py preview_emails # → <temp folder>/lumen-email-previews/ 

4 python manage.py preview_emails --out C:\\tmp\\mail 

5 

6Uses sample data and the real Company record for the header and footer. Nothing is sent. 

7""" 

8 

9import tempfile 

10from pathlib import Path 

11 

12from django.conf import settings 

13from django.core.management.base import BaseCommand 

14 

15from apps.communications import emails 

16from apps.communications.email_content import EmailContent, company_brand, render_html 

17 

18 

19class Command(BaseCommand): 

20 help = "Render every system email (sample data) to HTML files for a visual check. Sends nothing." 

21 

22 def add_arguments(self, parser): 

23 parser.add_argument( 

24 "--out", 

25 default=str(Path(tempfile.gettempdir()) / "lumen-email-previews"), 

26 help="Folder to write the files to (default: the system temp folder).", 

27 ) 

28 

29 def handle(self, *args, **options): 

30 brand = company_brand() 

31 company = brand.name 

32 portal = settings.BORROWER_POS_URL 

33 staff = settings.STAFF_LOS_URL 

34 samples: dict[str, tuple[str, EmailContent]] = { 

35 "1-borrower-reset": emails.password_reset( 

36 company, f"{portal}/reset-password?token=sample-token" 

37 ), 

38 "2-staff-reset": emails.password_reset( 

39 company, f"{staff}/reset-password?token=sample-token" 

40 ), 

41 "3-sign-in-code": emails.sign_in_code(company, "482913"), 

42 "4-borrower-invite": emails.borrower_invite( 

43 company=company, name="Jane Doe", invited_by="Timothy Thomas", 

44 loan_number="LN-100245", login_url=f"{portal}/login", 

45 email="jane@example.com", temp_password="Kx7pQ2mZ", 

46 ), 

47 "5-upload-request": emails.upload_request( 

48 company=company, greeting="Hi Jane,", officer="Timothy Thomas", 

49 loan_number="LN-100245", message="Please send your last two W-2s.", 

50 link=f"{portal}/upload/sample-token", 

51 ), 

52 "6-staff-invite": emails.staff_invite( 

53 company=company, first_name="Sam", role="Loan Officer", 

54 login_url=f"{staff}/login", email="sam@example.com", temp_password="lumen-a1b2c3", 

55 ), 

56 "7-compose": ( 

57 "Your rate is locked", 

58 EmailContent.from_text( 

59 "Hi Jane,\n\nGood news — your rate is locked at 6.5% on loan LN-100245.\n\n" 

60 f"Track your loan any time: {portal}/login\n\nThanks,\nTimothy Thomas" 

61 ), 

62 ), 

63 } 

64 out = Path(options["out"]) 

65 out.mkdir(parents=True, exist_ok=True) 

66 for name, (subject, content) in samples.items(): 

67 (out / f"{name}.html").write_text(render_html(content, subject, brand), encoding="utf-8") 

68 (out / f"{name}.txt").write_text(f"Subject: {subject}\n\n{content.to_text()}\n", 

69 encoding="utf-8") 

70 self.stdout.write(self.style.SUCCESS(f"Wrote {len(samples)} emails to {out.resolve()}"))