Home Information - Your home, your data, organized.
Python
Added test/ui module. Notification email UI test. Refactoring much.
Anthony Cassandraedcdaadde05a
diff --git a/src/hi/apps/common/module_utils.py b/src/hi/apps/common/module_utils.pynew file mode 100644index 00000000..f5c4da4c--- /dev/null+++ b/src/hi/apps/common/module_utils.py@@ -0,0 +1,17 @@+import importlib+from types import ModuleType+from typing import Optional+++def import_module_safe( module_name : str ) -> Optional[ModuleType]:+ """+ Imports a module if it exists, else returns None. Will raise an+ exception if the module exists, but there is a problem loading it.+ """+ try:+ return importlib.import_module(module_name)+ except ModuleNotFoundError as e:+ if module_name.startswith( e.name ):+ return None+ else:+ raisediff --git a/src/hi/apps/notify/notification_queue.py b/src/hi/apps/notify/notification_queue.pyindex 234e8db6..cb3fa88c 100644--- a/src/hi/apps/notify/notification_queue.py+++ b/src/hi/apps/notify/notification_queue.py@@ -49,6 +49,7 @@ class NotificationQueue:if len(notification_item_list) < 1:continuenotification = Notification(+ title = notification_item_list[0].title,item_list = notification_item_list,)notifications_map[signature] = notificationdiff --git a/src/hi/apps/notify/templates/notify/emails/base_email_message.html b/src/hi/apps/notify/templates/notify/emails/base_email_message.htmlindex 3b1ccb68..fab88b4d 100644--- a/src/hi/apps/notify/templates/notify/emails/base_email_message.html+++ b/src/hi/apps/notify/templates/notify/emails/base_email_message.html@@ -1,44 +1,56 @@{% load static %}<html><head>+ <style>+ body { margin: 0; padding: 0; font-family: Arial, sans-serif; }+ .email-container { border: 4px solid #040408; padding: 0; margin: 0; }+ .email-inner { border: 4px solid white; padding: 0; margin: 0; }+ .email-header { border: 4px solid #177072; padding: 0; }+ .email-header tr { background-color: #177072; }+ .email-logo { width: 128px; padding-left: 2px; padding-bottom: 4px; }+ .email-title { font-size: 18px; font-weight: 800; color: white; padding: 12px; }+ .email-content { margin: 8px; }+ .email-unsubscribe { margin: 8px; color: #888888; font-size: 0.8rem; text-decoration: none; }+ img.logo { height: 48px; width: auto; }+ </style></head><body>- <div style="border: 4px solid #040408; padding: 0; margin: 0">- <div style="border: 4px solid white; padding: 0; margin: 0;">- <div style="border: 4px solid #721917; padding: 0;">- {% block body %}- <table style="width: 100%;" cellpadding="0" cellspacing="0">- <tr style="background-color: #721917;">- <td style="width: 128px; padding-left: 2px; padding-bottom: 4px;">- {% block logo %}- {% if BASE_URL %}- <a href="{{ BASE_URL }}" alt="Home Information Home"><img src="{{ BASE_URL }}{% static 'img/hi-logo-128x64.png' %}" alt="Home Information Logo"/></a>- {% endif %}- {% endblock %}- </td>- <td>- <p style="font-size: 18px; font-weight: 800; color: white; padding: 12px;" >- {% block title %}- {% endblock %}- </p>- </td>- </tr>- </table>-- {% endblock %}-- <div style="margin: 8px;">- {% block content %}- {% endblock %}- </div>-- {% if UNSUBSCRIBE_URL %}- <hr />- <p style="margin: 8px;">- <a style="color: #888888; font-size: 0.8rem;" href="{{ UNSUBSCRIBE_URL }}">Unsubscribe.</a>- </p>- {% endif %}- </div>+ <div class="email-container">+ <div class="email-inner">+ <div class="email-header">+ {% block body %}+ <table style="width: 100%;" cellpadding="0" cellspacing="0">+ <tr>+ <td class="email-logo">+ {% block logo %}+ {% if BASE_URL %}+ <a href="{{ BASE_URL }}" alt="Home Information Home">+ <img class="logo" src="{{ BASE_URL }}{% static 'img/hi-icon-196x196.png' %}"+ alt="Home Information Logo"/>+ </a>+ {% endif %}+ {% endblock %}+ </td>+ <td>+ <p class="email-title">+ {% block title %}+ {% endblock %}+ </p>+ </td>+ </tr>+ </table>+ {% endblock %}+ </div>+ <div class="email-content">+ {% block content %}+ {% endblock %}+ </div>+ {% if UNSUBSCRIBE_URL %}+ <hr />+ <p class="email-unsubscribe">+ <a href="{{ UNSUBSCRIBE_URL }}">Unsubscribe</a>+ </p>+ {% endif %}</div></div></body>diff --git a/src/hi/apps/notify/templates/notify/emails/notification_message.html b/src/hi/apps/notify/templates/notify/emails/notification_message.htmlindex bba57a0c..03397482 100644--- a/src/hi/apps/notify/templates/notify/emails/notification_message.html+++ b/src/hi/apps/notify/templates/notify/emails/notification_message.html@@ -2,6 +2,10 @@{% block title %}Home Information Notification{% endblock %}{% block content %}<p>- TBD : {{ notification }}+ <ul>+ {% for item in notification.item_list %}+ <li>{{ item.title }}</li>+ {% endfor %}+ </ul></p>{% endblock %}diff --git a/src/hi/apps/notify/templates/notify/emails/notification_message.txt b/src/hi/apps/notify/templates/notify/emails/notification_message.txtindex 74a54808..c98f066c 100644--- a/src/hi/apps/notify/templates/notify/emails/notification_message.txt+++ b/src/hi/apps/notify/templates/notify/emails/notification_message.txt@@ -3,4 +3,6 @@{{ notification.title }}+{% for item in notification.item_list %} - {{ item.title }}+{% endfor %}{% endblock %}diff --git a/src/hi/apps/notify/templates/notify/tests/ui/email_preview.html b/src/hi/apps/notify/templates/notify/tests/ui/email_preview.htmlnew file mode 100644index 00000000..30841491--- /dev/null+++ b/src/hi/apps/notify/templates/notify/tests/ui/email_preview.html@@ -0,0 +1,30 @@+{% extends "modals/action_ok.html" %}++{% block title_text %}+Email: {{ view_name }}+{% endblock %}++{% block body %}+<div class="container">++ <h4>Subject (text)</h4>+ <div class="border mb-2 p-2"><pre>{% include subject_text_template %}</pre></div>++ <h4>Body (text)</h4>+ <div class="border mb-2 p-2"><pre>{% include body_text_template %}</pre></div>++ <h4>Body (HTML)</h4>++ <div class="border p-2">++ {% autoescape off %}+ <iframe srcdoc="{{ body_html|safe }}"+ style="width: 100%; height: 50vh; border: 1px solid #ccc;"+ sandbox="allow-same-origin allow-scripts">+ </iframe>+ {% endautoescape %}++ </div>++</div>+{% endblock %}diff --git a/src/hi/apps/notify/templates/notify/tests/ui/home.html b/src/hi/apps/notify/templates/notify/tests/ui/home.htmlnew file mode 100644index 00000000..4fc019ad--- /dev/null+++ b/src/hi/apps/notify/templates/notify/tests/ui/home.html@@ -0,0 +1,33 @@+{% extends "pages/base.html" %}+{% block head_title %}HI: Visual Testing!{% endblock %}++{% block content %}+<div class="container-fluid m-4">++ <h2 class="text-info">Notifications: UI Tests</h2>++ <div class="row">+ <div class="col-xl-4 col-lg-4 col-md-6 col-12">+ <div class="panel panel-default">+ <div class="panel-heading">+ <h3 class="panel-title text-primary">Emails</h3>+ </div>+ <div class="panel-body">+ <ul>+ <li>+ <a href="{% url 'notify_tests_ui_view_email' name='notification' %}" data-async="modal">View Notification Email+ </a>+ </li>+ <li>+ <a href="{% url 'notify_tests_ui_send_email' name='notification' %}" data-async="modal">Send Notification Email+ </a>+ </li>+ </ul>+ </div>+ </div>++ </div>++ </div>+</div>+{% endblock %}diff --git a/src/hi/apps/notify/tests/__init__.py b/src/hi/apps/notify/tests/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/src/hi/apps/notify/tests/synthetic_data.py b/src/hi/apps/notify/tests/synthetic_data.pynew file mode 100644index 00000000..38e0e11f--- /dev/null+++ b/src/hi/apps/notify/tests/synthetic_data.py@@ -0,0 +1,22 @@+import random++from hi.apps.notify.transient_models import Notification, NotificationItem+++class NotifySyntheticData:++ def create_random_notification( self ):++ item_list = list()+ for item_idx in range( random.randint( 1, 5 )):+ item = NotificationItem(+ signature = f'signature-{item_idx}',+ title = f'This is notification item {item_idx}',+ )+ item_list.append( item )+ continue++ return Notification(+ title = 'Test Notification',+ item_list = item_list,+ )diff --git a/src/hi/apps/notify/tests/ui/__init__.py b/src/hi/apps/notify/tests/ui/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/src/hi/apps/notify/tests/ui/urls.py b/src/hi/apps/notify/tests/ui/urls.pynew file mode 100644index 00000000..2bf35f3f--- /dev/null+++ b/src/hi/apps/notify/tests/ui/urls.py@@ -0,0 +1,19 @@+from django.urls import re_path++from . import views+++urlpatterns = [++ re_path( r'^$',+ views.TestUiNotifyHomeView.as_view(),+ name='notify_tests_ui'),++ re_path( r'^email/view/(?P<name>\w+)$',+ views.TestUiViewEmailView.as_view(),+ name='notify_tests_ui_view_email'),++ re_path( r'^email/send/(?P<name>\w+)$',+ views.TestUiSendEmailView.as_view(),+ name='notify_tests_ui_send_email'),+]diff --git a/src/hi/apps/notify/tests/ui/views.py b/src/hi/apps/notify/tests/ui/views.pynew file mode 100644index 00000000..1f099767--- /dev/null+++ b/src/hi/apps/notify/tests/ui/views.py@@ -0,0 +1,61 @@+import html++from django.shortcuts import render+from django.template.loader import render_to_string++from django.views.generic import View++from hi.apps.notify.tests.synthetic_data import NotifySyntheticData+++class TestUiNotifyHomeView( View ):++ def get(self, request, *args, **kwargs):+ context = {+ }+ return render(request, "notify/tests/ui/home.html", context )+++class TestUiViewEmailView( View ):++ def get( self, request, *args, **kwargs ):+ view_name = kwargs.get('name')+ notification = NotifySyntheticData().create_random_notification()+ context = {+ 'notification': notification,+ }+ return self.email_preview_response(+ request = request,+ app_name = 'notify',+ view_name = view_name,+ context = context,+ )++ def email_preview_response( self, request, app_name : str, view_name : str, context : dict ):++ context.update({+ 'view_name': view_name,+ 'BASE_URL': ' ',+ 'UNSUBSCRIBE_URL': 'https://example.com/unsubscribe',+ 'USER_HOME_URL': 'https://example.com/home',+ })++ # Need to make sure inline CSS of HTML email does not clash with+ # site's main CSS. Using IFRAME for this, and the "srcdoc"+ # atttribute need us to pre-render.+ #+ body_html_template = f'{app_name}/emails/{view_name}_message.html'+ body_html = render_to_string( body_html_template, context )+ escaped_email_html = html.escape( body_html )++ context.update({+ 'subject_text_template': f'{app_name}/emails/{view_name}_subject.txt',+ 'body_text_template': f'{app_name}/emails/{view_name}_message.txt',+ 'body_html_template': body_html_template,+ 'body_html': escaped_email_html,+ })+ return render( request, 'notify/tests/ui/email_preview.html', context )+++class TestUiSendEmailView( View ):+ passdiff --git a/src/hi/apps/notify/transient_models.py b/src/hi/apps/notify/transient_models.pyindex f7bc4704..ed8e7ee8 100644--- a/src/hi/apps/notify/transient_models.py+++ b/src/hi/apps/notify/transient_models.py@@ -50,4 +50,5 @@ class NotificationItem:@dataclassclass Notification:+ title : stritem_list : List[ NotificationItem ]diff --git a/src/hi/hi_grid_view.py b/src/hi/hi_grid_view.pyindex f952b478..ecfca28e 100644--- a/src/hi/hi_grid_view.py+++ b/src/hi/hi_grid_view.py@@ -27,7 +27,7 @@ logger = logging.getLogger(__name__)class HiGridView(View):"""- - The Hi app 'grid' is an HTML layout that is defined in pages/home.html.+ - The Hi app 'grid' is an HTML layout that is defined in pages/hi_grid.html.- Most views participate in keeping that same four-pane layout.- There is a top and bottom control button areas with middle section of 'main' and 'side' content.- In normal (non-editing) view mode, the side, top and bottom panes stays mostly indepedenent main area.@@ -42,7 +42,7 @@ class HiGridView(View):- Subclass this and provide get_main_template_name() and get_template_context()"""- HI_GRID_TEMPLATE_NAME = 'pages/home.html'+ HI_GRID_TEMPLATE_NAME = 'pages/hi_grid.html'TOP_TEMPLATE_NAME = 'panes/top_buttons.html'BOTTOM_TEMPLATE_NAME = 'panes/bottom_buttons.html'SIDE_DEFAULT_TEMPLATE_NAME = 'panes/side.html'diff --git a/src/hi/settings/base.py b/src/hi/settings/base.pyindex 133df1ff..87880387 100644--- a/src/hi/settings/base.py+++ b/src/hi/settings/base.py@@ -286,9 +286,8 @@ PIPELINE = {),'output_filename': 'js/js_after_content.js',},- 'js_after_content_custom': {+ 'js_hi_grid_content': {'source_filenames': (- 'js/audio.js','js/svg-utils.js','js/watchdog.js','js/status.js',@@ -298,8 +297,8 @@ PIPELINE = {'js/svg-path.js','js/svg-location.js', # Should come last: edit event handlers should be registered first.),- 'output_filename': 'js/js_after_content_custom.js',- }+ 'output_filename': 'js/js_hi_grid_content.js',+ },}}diff --git a/src/hi/templates/pages/base.html b/src/hi/templates/pages/base.htmlindex 9be01b1a..e89dce9c 100644--- a/src/hi/templates/pages/base.html+++ b/src/hi/templates/pages/base.html@@ -5,7 +5,7 @@<head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">- <title>Home Information</title>+ <title>{% block head_title %}Home Information{% endblock %}</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="shortcut icon" type="image/png" href="{% static 'favicon.png' %}"/><link rel="icon" type="image/png" href="{% static 'img/hi-icon-16x16.png' %}" sizes="16x16">@@ -25,7 +25,7 @@{% javascript 'js_before_content' %}{% include "alert/panes/alert_banner.html" %}-+{% block content %}{% endblock %}{% include "config/panes/base_page_settings.html" %}@@ -37,7 +37,6 @@{% block footer_pre_javascript %}{% endblock %}{% javascript 'js_after_content' %}- {% javascript 'js_after_content_custom' %}{% block footer_post_javascript %}{% endblock %}</body>diff --git a/src/hi/templates/pages/home.html b/src/hi/templates/pages/hi_grid.htmlsimilarity index 84%rename from src/hi/templates/pages/home.htmlrename to src/hi/templates/pages/hi_grid.htmlindex fd445dfb..84fc2c35 100644--- a/src/hi/templates/pages/home.html+++ b/src/hi/templates/pages/hi_grid.html@@ -1,4 +1,5 @@{% extends "pages/base.html" %}+{% load pipeline %}{% block content %}@@ -25,3 +26,8 @@</div></div>{% endblock %}+++{% block footer_post_javascript %}+{% javascript 'js_hi_grid_content' %}+{% endblock %}diff --git a/src/hi/templates/tests/ui/home.html b/src/hi/templates/tests/ui/home.htmlnew file mode 100644index 00000000..7c514c8b--- /dev/null+++ b/src/hi/templates/tests/ui/home.html@@ -0,0 +1,17 @@+{% extends "pages/base.html" %}+{% block head_title %}HI: Visual Testing!{% endblock %}++{% block content %}+<div class="container-fluid m-4">++ <h2 class="text-info">UI Tests Home</h2>++ <ul>+ {% for app_label, url in app_url_list %}+ <li><a href='{{ url }}'>{{ app_label.capitalize }} UI Tests</a></li>+ {% empty %}+ <div class="alert alert-warning" >No modules have defined any UI tests.</div>+ {% endfor %}+ </ul>+</div>+{% endblock %}diff --git a/src/hi/tests/__init__.py b/src/hi/tests/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/src/hi/tests/ui/__init__.py b/src/hi/tests/ui/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/src/hi/tests/ui/urls.py b/src/hi/tests/ui/urls.pynew file mode 100644index 00000000..8b5be56b--- /dev/null+++ b/src/hi/tests/ui/urls.py@@ -0,0 +1,31 @@+import importlib++from django.apps import apps+from django.urls import include, re_path++from . import views+++def get_tests_ui_urls():+ urlpatterns = []+ for app_config in apps.get_app_configs():+ try:+ module = importlib.import_module( f'{app_config.name}.tests.ui.urls' )+ short_name = app_config.name.split('.')[-1]+ urlpatterns.append( re_path( f'{short_name}/', include(module) ))+ except ModuleNotFoundError:+ pass+ continue+ return urlpatterns+++urlpatterns = [+ re_path( r'^$',+ views.TestingHomeView.as_view(),+ name='testing_home'),+]++urlpatterns += get_tests_ui_urls()+++diff --git a/src/hi/tests/ui/views.py b/src/hi/tests/ui/views.pynew file mode 100644index 00000000..3b91b548--- /dev/null+++ b/src/hi/tests/ui/views.py@@ -0,0 +1,35 @@+import importlib+import logging++from django.apps import apps+from django.shortcuts import render+from django.views.generic import View++from hi.apps.common.module_utils import import_module_safe++logger = logging.getLogger(__name__)+++class TestingHomeView( View ):++ def get(self, request, *args, **kwargs):++ app_url_list = []+ for app_config in apps.get_app_configs():+ if not app_config.name.startswith( 'hi' ):+ continue+ module_name = f'{app_config.name}.tests.ui.urls'+ short_name = app_config.name.split('.')[-1]+ try:+ module = import_module_safe( module_name = module_name )+ if module:+ app_url_list.append( ( short_name, f'{short_name}/' ) )++ except Exception as e:+ logger.exception( f'Problem loading UI tests for {short_name}.', e )++ continue+ context = {+ 'app_url_list': app_url_list,+ }+ return render(request, 'tests/ui/home.html', context )diff --git a/src/hi/urls.py b/src/hi/urls.pyindex 24fa847c..d2f05942 100644--- a/src/hi/urls.py+++ b/src/hi/urls.py@@ -55,3 +55,8 @@ urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)handler404 = 'hi.views.custom_404_handler'++if settings.DEBUG:+ urlpatterns += [+ re_path( r'^tests/ui/', include('hi.tests.ui.urls' )),+ ]