Skip to content

Commit

Permalink
Record and display To, CC, and BCC in email logs #4089
Browse files Browse the repository at this point in the history
  • Loading branch information
joemull authored and ajrbyers committed Jun 4, 2024
1 parent db1c014 commit 9d729b4
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/templates/admin/elements/core/addressee_display.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% regroup entry.addressees by get_field_display as email_fields %}
{% for field, addressees in email_fields %}
{% if field %}<strong>{{ field }}: </strong>{% endif %}
{% for addressee in addressees %}
{{ addressee.email }}{% if not forloop.last %}, {% endif %}
{% endfor %}
{% if not forloop.last %}<br>{% endif %}
{% endfor %}
6 changes: 4 additions & 2 deletions src/templates/admin/elements/journal/log_description.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ <h2>Headers</h2>
{% endif %}
</div>
<div class="content">
<p><strong>To: </strong>{% for email in entry.to %}{{ email }}; {% endfor %}</p>
<p>
{% include 'admin/elements/core/addressee_display.html' with entry=entry %}
</p>
{% if settings.ENABLE_ENHANCED_MAILGUN_FEATURES %}
<p><strong>Status:</strong> <i class="fa fa-circle {{ entry.message_status_class }}"
aria-hidden="true"></i> {{ entry.get_message_status_display }}
Expand All @@ -40,4 +42,4 @@ <h2>Content</h2>
</div>
</div>
</div>
</div>
</div>
10 changes: 8 additions & 2 deletions src/templates/admin/journal/article_log.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h2>Log Entries</h2>
<thead>
<tr>
<th>Entry Type</th>
<th>To (if email)</th>
<th>Addressees (if email)</th>
<th>Subject (if email)</th>
<th>Date</th>
<th>Actor</th>
Expand All @@ -36,7 +36,13 @@ <h2>Log Entries</h2>
{% for entry in log_entries %}
<tr>
<td>{{ entry.types }}</td>
<td>{% if entry.is_email %}{% for email in entry.to %}{{ email }}{% if not forloop.last %}; {% endif %}{% endfor %}{% else %}---{% endif %}</td>
<td>
{% if entry.is_email %}
{% include 'admin/elements/core/addressee_display.html' with entry=entry %}
{% else %}
---
{% endif %}
</td>
<td>{{ entry.email_subject }}</td>
<td>{{ entry.date|date:"Y-m-d H:i:s" }}</td>
<td>{% if entry.actor %}{{ entry.actor.full_name }}{% else %}Janeway System{% endif %}</td>
Expand Down
8 changes: 6 additions & 2 deletions src/templates/admin/repository/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h2>Log Entries</h2>
<thead>
<tr>
<th>Entry Type</th>
<td>To</td>
<td>Addressees</td>
<th>Information</th>
<th>Date</th>
<th>Actor</th>
Expand All @@ -40,7 +40,11 @@ <h2>Log Entries</h2>
<tr>
<td>{{ entry.types }}</td>
<td>
{% if entry.is_email %}{{ entry.to }}{% else %}N/a{% endif %}
{% if entry.is_email %}
{% include 'admin/elements/core/addressee_display.html' with entry=entry %}
{% else %}
N/a
{% endif %}
</td>
<td>
{{ entry.description|safe }}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LogAdmin(admin.ModelAdmin):
raw_id_fields = ('actor',)

inlines = [
admin_utils.ToAddressInline,
admin_utils.AddresseeInline,
]

def _to(self, obj):
Expand Down
4 changes: 2 additions & 2 deletions src/utils/admin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class RevisionActionInline(admin.TabularInline):
raw_id_fields = ('revisionaction',)


class ToAddressInline(admin.TabularInline):
model = utils_models.ToAddress
class AddresseeInline(admin.TabularInline):
model = utils_models.Addressee
extra = 0


Expand Down
22 changes: 22 additions & 0 deletions src/utils/migrations/0034_rename_toaddress_addressee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.20 on 2024-05-28 13:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('utils', '0033_upgrade_1_6_0'),
]

operations = [
migrations.RenameModel(
old_name='ToAddress',
new_name='Addressee',
),
migrations.AddField(
model_name='addressee',
name='field',
field=models.CharField(blank=True, choices=[('to', 'To'), ('cc', 'CC'), ('bcc', 'BCC')], max_length=3),
),
]
39 changes: 31 additions & 8 deletions src/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
('failed', 'Failed'),
]

EMAIL_RECIPIENT_FIELDS = [
('to', 'To'),
('cc', 'CC'),
('bcc', 'BCC'),
]

class LogEntry(models.Model):
types = models.CharField(max_length=255, null=True, blank=True)
Expand Down Expand Up @@ -83,7 +88,18 @@ def message_status_class(self):

@property
def to(self):
return [to.email for to in self.toaddress_set.all()]
""" Deprecated in 1.6 because of ambiguity with cc and bcc fields.
Use addressee_emails instead.
"""
return self.addressee_emails

@property
def addressees(self):
return self.addressee_set.all()

@property
def addressee_emails(self):
return set(addressee.email for addressee in self.addressee_set.all())

@staticmethod
def add_entry(
Expand Down Expand Up @@ -122,12 +138,14 @@ def add_entry(
}
new_entry = LogEntry.objects.create(**kwargs)

if to or cc or bcc:
for email in join_lists(to, cc, bcc):
ToAddress.objects.create(
log_entry=new_entry,
email=email,
)
for emails, field in [(to, 'to'), (cc, 'cc'), (bcc, 'bcc')]:
if emails:
for email in emails:
Addressee.objects.create(
log_entry=new_entry,
email=email,
field=field
)

return new_entry

Expand All @@ -150,12 +168,17 @@ def bulk_add_simple_entry(
return LogEntry.objects.bulk_create(new_entries, batch_size)


class ToAddress(models.Model):
class Addressee(models.Model):
log_entry = models.ForeignKey(
LogEntry,
on_delete=models.CASCADE,
)
email = models.EmailField(max_length=300)
field = models.CharField(
max_length=3,
blank=True,
choices=EMAIL_RECIPIENT_FIELDS,
)

def __str__(self):
return self.email
Expand Down

0 comments on commit 9d729b4

Please sign in to comment.