|
| 1 | +"""Custom admin views for the People app.""" |
| 2 | + |
| 3 | +from smtplib import SMTPException |
| 4 | + |
| 5 | +from django.views.generic import TemplateView |
| 6 | + |
| 7 | + |
| 8 | +class BaseSelftestAdminPageView(TemplateView): |
| 9 | + """Base class for selftest admin pages.""" |
| 10 | + |
| 11 | + is_index = False |
| 12 | + template_name = "selftest/base.html" |
| 13 | + title = "Selftest Admin Page" # should be overridden in subclasses |
| 14 | + path = None |
| 15 | + |
| 16 | + def get_context_data(self, **kwargs): |
| 17 | + """Add the title to the context.""" |
| 18 | + context = super().get_context_data(**kwargs) |
| 19 | + |
| 20 | + context["title"] = self.title |
| 21 | + return context |
| 22 | + |
| 23 | + |
| 24 | +class IndexSelftestAdminPageView(BaseSelftestAdminPageView): |
| 25 | + """View for the selftest index page.""" |
| 26 | + |
| 27 | + template_name = "selftest/index.html" |
| 28 | + is_index = True |
| 29 | + title = "Selftest Admin Index Page" |
| 30 | + path = "" |
| 31 | + |
| 32 | + def get_context_data(self, **kwargs): |
| 33 | + """Add all registered views to the context.""" |
| 34 | + from .registry import view_registry # pylint: disable=import-outside-toplevel |
| 35 | + |
| 36 | + context = super().get_context_data(**kwargs) |
| 37 | + context["pages"] = [ |
| 38 | + { |
| 39 | + "title": view["title"], |
| 40 | + "url_name": view["name"], |
| 41 | + } |
| 42 | + for view in sorted( |
| 43 | + [ |
| 44 | + view |
| 45 | + for view in view_registry.get_views() |
| 46 | + if not view["view_class"].is_index |
| 47 | + ], |
| 48 | + key=lambda x: x["name"], |
| 49 | + ) |
| 50 | + ] |
| 51 | + return context |
| 52 | + |
| 53 | + |
| 54 | +class EmailSelftestAdminPageView(BaseSelftestAdminPageView): |
| 55 | + """View for testing email functionality.""" |
| 56 | + |
| 57 | + title = "Send test email" |
| 58 | + template_name = "selftest/email.html" |
| 59 | + path = "email/" |
| 60 | + |
| 61 | + def get(self, request, *args, **kwargs): |
| 62 | + """Handle GET request.""" |
| 63 | + context = self.get_context_data(**kwargs) |
| 64 | + context["form"] = {"email": ""} |
| 65 | + return self.render_to_response(context) |
| 66 | + |
| 67 | + def post(self, request, *args, **kwargs): |
| 68 | + """Handle POST request to send test email.""" |
| 69 | + from django.contrib import messages |
| 70 | + from django.core.mail import send_mail |
| 71 | + |
| 72 | + context = self.get_context_data(**kwargs) |
| 73 | + email = request.POST.get("email", "") |
| 74 | + |
| 75 | + if email: |
| 76 | + try: |
| 77 | + send_mail( |
| 78 | + subject="Test Email from People Admin", |
| 79 | + message="This is a test email from the People Admin site.", |
| 80 | + from_email=None, # Use DEFAULT_FROM_EMAIL |
| 81 | + recipient_list=[email], |
| 82 | + fail_silently=False, |
| 83 | + ) |
| 84 | + messages.success(request, f"Test email sent successfully to {email}") |
| 85 | + except SMTPException as e: |
| 86 | + messages.error(request, f"Failed to send email: {str(e)}") |
| 87 | + else: |
| 88 | + messages.error(request, "Please provide an email address") |
| 89 | + |
| 90 | + context["form"] = {"email": email} |
| 91 | + return self.render_to_response(context) |
0 commit comments