Skip to content

Commit

Permalink
Provide admin for email view (let's use see email HTML formatting)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMadBug committed Oct 2, 2023
1 parent 8c1a89d commit 5cb899d
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions email_manager/models/email_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.conf import settings
from django.core.mail import send_mail, get_connection, EmailMultiAlternatives
from django.db import models
from django.urls import reverse
from model_utils.models import TimeStampedModel


Expand All @@ -15,6 +16,9 @@ class EmailLog(TimeStampedModel):
probably_sent = models.BooleanField()
single_email = models.BooleanField()

def get_absolute_url(self):
return reverse('email_detail', kwargs={"email_id": self.pk})

@staticmethod
def send_mail(subject: str,
html: str,
Expand Down
22 changes: 22 additions & 0 deletions email_manager/templates/email_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% load user_tags %}
{% load ui_utils %}
{% load js_tags %}
{% if render_mode == "embedded" %}
{% labelled label="Created" %}{% timestamp email.created %}{% endlabelled %}
{% labelled label="Subject" %}{{ email.subject }}{% endlabelled %}
{% else %}
{% admin_link email %}<br/>
{% endif %}
<label class="mb-4 mt-2">Recipients</label>:
<div>
{% for email_address in unrecognised %}
<div class="text-dark" style="font-size:9pt"><i class="fa-regular fa-envelope mx-1"></i> {{ email_address }}</div>
{% endfor %}
{% for email_user in users %}
<div class="mt-1">{% user email_user show_avatar=True show_email=True show_group=True size='tiny' %}</div>
{% endfor %}
</div>
<label class="my-4">Content</label>:
<div class="border rounded p-4" style="background-color:#fdfdfd">
{{ email.html | safe }}
</div>
9 changes: 9 additions & 0 deletions email_manager/templates/email_log.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "uicore/page/base.html" %}
{% load ui_menu_bars %}
{% block title %}Email{% endblock title %}
{% block submenu %}{% menu_bar_settings %}{% endblock submenu %}
{% block content %}
<div class="container">
<table id="email-datatable" class="event-table sticky-header" data-datatable-responsive="true" data-datatable-url="{% url 'email_datatable' %}"></table>
</div>
{% endblock %}
9 changes: 9 additions & 0 deletions email_manager/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from email_manager.views.email_manager_views import email_manager_view, EmailColumns, email_detail
from snpdb.views.datatable_view import DatabaseTableView
from variantgrid.perm_path import perm_path

urlpatterns = [
perm_path('', email_manager_view, name='email_log'),
perm_path('detail/<int:email_id>', email_detail, name='email_detail'),
perm_path('datatable', DatabaseTableView.as_view(column_class=EmailColumns), name='email_datatable')
]
69 changes: 69 additions & 0 deletions email_manager/views/email_manager_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from typing import List
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.db.models import QuerySet, Q
from django.http import HttpRequest
from django.shortcuts import render
from email_manager.models import EmailLog
from library.django_utils import require_superuser
from library.utils.django_utils import render_ajax_view
from snpdb.views.datatable_view import DatatableConfig, RichColumn, SortOrder, CellData


@require_superuser
def email_manager_view(request):
return render(request, 'email_log.html', context={})


@require_superuser
def email_detail(request, email_id: int):
email_log = EmailLog.objects.get(pk=email_id)
recipients = [address.strip() for address in email_log.recipient_list.split(";")]
recipients = [address for address in recipients if address]
users: List[User] = list(User.objects.filter(email__in=recipients).order_by('email').all())
unrecognised_emails = set(recipients)
check_user: User
for check_user in users:
unrecognised_emails.discard(check_user.email)
unrecognised_email_list = list(sorted(unrecognised_emails))
unrecognised_email_list += ["foo@bar", "cat@dog", "what#ddfd"]

return render_ajax_view(
request,
'email_detail.html',
context={"email": email_log, "users": users, "unrecognised": unrecognised_email_list},
menubar='settings'
)


class EmailColumns(DatatableConfig[EmailLog]):

def power_search(self, qs: QuerySet[EmailLog], search_string: str) -> QuerySet[EmailLog]:
if search_string:
qs = qs.filter(Q(recipient_list__icontains=search_string) | Q(subject__icontains=search_string))
return qs

def recipient_renderer(self, row: CellData):
if filename := row.get('filename'):
return filename
elif detail := row.get('details'):
return detail.split('\n', 1)[0]

def __init__(self, request: HttpRequest):
super().__init__(request)
if not self.user.is_superuser:
raise PermissionDenied("Email log requires admin privileges")

self.search_box_enabled = True

self.expand_client_renderer = DatatableConfig._row_expand_ajax('email_detail', expected_height=300)
self.rich_columns = [
RichColumn('created', client_renderer='TableFormat.timestamp', orderable=True, default_sort=SortOrder.DESC),
RichColumn('subject', orderable=True),
RichColumn('recipient_list', label='Recipients', orderable=False),
RichColumn('probably_sent', client_renderer='TableFormat.boolean.bind(null, "false_is_error")'),
RichColumn('id', visible=False)
]

def get_initial_queryset(self) -> QuerySet[EmailLog]:
return EmailLog.objects.all()
4 changes: 4 additions & 0 deletions snpdb/templates/snpdb/embedded_ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
{% block submenu %}
{% if menubar == 'classification' %}
{% menu_bar_classifications %}
{% elif menubar == 'settings' %}
{% menu_bar_settings %}
{% elif menubar %}
Menubar {{ menubar }} not supported, developer to fix in embedded_ajax.html
{% endif %}
{% endblock submenu %}
{% block content %}
Expand Down
1 change: 1 addition & 0 deletions uicore/templates/uicore/menus/menu_bar_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{% menu_item 'admin' href="/admin/" admin_only=True external=True%}
{% menu_item 'eventlog' title="Event Log" admin_only=True %}
{% menu_item 'email_log' title="Email Log" admin_only=True %}
{% menu_item 'keycloak_admin' title="Keycloak" admin_only=True %}
{% menu_item 'server_status' admin_only=True %}

Expand Down
1 change: 1 addition & 0 deletions variantgrid/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"analysis",
"annotation",
"eventlog",
"email_manager",
"flags",
"genes",
"pathtests",
Expand Down

0 comments on commit 5cb899d

Please sign in to comment.