add event mail addresses to the /events endpoints
This commit is contained in:
parent
55cef1128e
commit
f44da341b4
10 changed files with 90 additions and 13 deletions
14
core/.coveragerc
Normal file
14
core/.coveragerc
Normal file
|
@ -0,0 +1,14 @@
|
|||
[run]
|
||||
source = .
|
||||
|
||||
[report]
|
||||
fail_under = 100
|
||||
show_missing = True
|
||||
skip_covered = True
|
||||
omit =
|
||||
*/tests/*
|
||||
*/migrations/*
|
||||
core/asgi.py
|
||||
core/wsgi.py
|
||||
core/settings.py
|
||||
manage.py
|
|
@ -3,12 +3,21 @@ from rest_framework import serializers
|
|||
|
||||
from files.models import File
|
||||
from inventory.models import Event, Container, Item
|
||||
from mail.models import EventAddress
|
||||
|
||||
|
||||
class EventAdressSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = EventAddress
|
||||
fields = ['address']
|
||||
|
||||
|
||||
class EventSerializer(serializers.ModelSerializer):
|
||||
addresses = EventAdressSerializer(many=True, required=False)
|
||||
|
||||
class Meta:
|
||||
model = Event
|
||||
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end']
|
||||
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
|
||||
read_only_fields = ['eid']
|
||||
|
||||
|
||||
|
|
|
@ -54,3 +54,15 @@ class EventTestCase(TestCase):
|
|||
response = client.delete(f'/api/2/events/{event.eid}/')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(len(Event.objects.all()), 1)
|
||||
|
||||
def test_items2(self):
|
||||
from mail.models import EventAddress
|
||||
event1 = Event.objects.create(slug='TEST1', name='Event')
|
||||
EventAddress.objects.create(event=Event.objects.get(slug='TEST1'), address='foo@bar.baz')
|
||||
response = self.client.get('/api/2/events/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(1, len(response.json()))
|
||||
self.assertEqual('TEST1', response.json()[0]['slug'])
|
||||
self.assertEqual('Event', response.json()[0]['name'])
|
||||
self.assertEqual(1, len(response.json()[0]['addresses']))
|
||||
|
||||
|
|
20
core/mail/migrations/0005_alter_eventaddress_event.py
Normal file
20
core/mail/migrations/0005_alter_eventaddress_event.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# Generated by Django 4.2.7 on 2024-11-03 18:30
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('inventory', '0004_alter_event_created_at_alter_item_created_at'),
|
||||
('mail', '0004_alter_emailattachment_file'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='eventaddress',
|
||||
name='event',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='addresses', to='inventory.event'),
|
||||
),
|
||||
]
|
|
@ -31,7 +31,7 @@ class Email(SoftDeleteModel):
|
|||
|
||||
class EventAddress(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
event = models.ForeignKey(Event, models.SET_NULL, null=True)
|
||||
event = models.ForeignKey(Event, models.SET_NULL, null=True, related_name='addresses')
|
||||
address = models.CharField(max_length=255)
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
from re import match
|
||||
|
||||
import aiosmtplib
|
||||
from channels.layers import get_channel_layer
|
||||
|
@ -10,6 +11,10 @@ from notify_sessions.models import SystemEvent
|
|||
from tickets.models import IssueThread
|
||||
|
||||
|
||||
class SpecialMailException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def find_quoted_printable(s, marker):
|
||||
positions = [i for i in range(len(s)) if s.lower().startswith('=?utf-8?' + marker + '?', i)]
|
||||
for pos in positions:
|
||||
|
@ -180,13 +185,13 @@ def receive_email(envelope, log=None):
|
|||
header_in_reply_to = parsed.get('In-Reply-To')
|
||||
header_message_id = parsed.get('Message-ID')
|
||||
|
||||
if header_from != envelope.mail_from:
|
||||
log.warning("Header from does not match envelope from")
|
||||
log.info(f"Header from: {header_from}, envelope from: {envelope.mail_from}")
|
||||
if match(r'^([a-zA-Z ]*<)?MAILER-DAEMON@', header_from) and envelope.mail_from.strip("<>") == "":
|
||||
log.warning("Ignoring mailer daemon")
|
||||
raise SpecialMailException("Ignoring mailer daemon")
|
||||
|
||||
if header_to != envelope.rcpt_tos[0]:
|
||||
log.warning("Header to does not match envelope to")
|
||||
log.info(f"Header to: {header_to}, envelope to: {envelope.rcpt_tos[0]}")
|
||||
if Email.objects.filter(reference=header_message_id).exists(): # break before issue thread is created
|
||||
log.warning("Email already exists")
|
||||
raise Exception("Email already exists")
|
||||
|
||||
recipient = envelope.rcpt_tos[0].lower() if envelope.rcpt_tos else header_to.lower()
|
||||
sender = envelope.mail_from if envelope.mail_from else header_from
|
||||
|
@ -233,7 +238,7 @@ Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid
|
|||
active_issue_thread.state = 'pending_open'
|
||||
active_issue_thread.save()
|
||||
|
||||
return email, new, reply
|
||||
return email, new, reply, active_issue_thread
|
||||
|
||||
|
||||
class LMTPHandler:
|
||||
|
@ -255,7 +260,7 @@ class LMTPHandler:
|
|||
content = None
|
||||
try:
|
||||
content = envelope.content
|
||||
email, new, reply = await receive_email(envelope, log)
|
||||
email, new, reply, thread = await receive_email(envelope, log)
|
||||
log.info(f"Created email {email.id}")
|
||||
systemevent = await database_sync_to_async(SystemEvent.objects.create)(type='email received',
|
||||
reference=email.id)
|
||||
|
@ -263,14 +268,20 @@ class LMTPHandler:
|
|||
channel_layer = get_channel_layer()
|
||||
await channel_layer.group_send(
|
||||
'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id,
|
||||
"message": "email received"}
|
||||
)
|
||||
"message": "email received"})
|
||||
log.info(f"Sent message to frontend")
|
||||
if new and reply:
|
||||
log.info('Sending message to %s' % reply['To'])
|
||||
await send_smtp(reply)
|
||||
log.info("Sent auto reply")
|
||||
|
||||
return '250 Message accepted for delivery'
|
||||
except SpecialMailException as e:
|
||||
import uuid
|
||||
random_filename = 'special-' + str(uuid.uuid4())
|
||||
with open(random_filename, 'wb') as f:
|
||||
f.write(content)
|
||||
log.warning(f"Special mail exception: {e} saved to {random_filename}")
|
||||
return '250 Message accepted for delivery'
|
||||
except Exception as e:
|
||||
from hashlib import sha256
|
||||
|
|
|
@ -760,7 +760,6 @@ dGVzdGltYWdl
|
|||
response = self.client.post(f'/api/2/tickets/{issue_thread.id}/reply/', {
|
||||
'message': 'test'
|
||||
})
|
||||
aiosmtplib.send.assert_called_once()
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(5, len(Email.objects.all()))
|
||||
self.assertEqual(5, len(Email.objects.filter(issue_thread=issue_thread)))
|
||||
|
@ -776,6 +775,7 @@ dGVzdGltYWdl
|
|||
self.assertEqual('test subject', IssueThread.objects.all()[0].name)
|
||||
self.assertEqual('pending_new', IssueThread.objects.all()[0].state)
|
||||
self.assertEqual(None, IssueThread.objects.all()[0].assigned_to)
|
||||
aiosmtplib.send.assert_called_once()
|
||||
|
||||
def test_mail_4byte_unicode_emoji(self):
|
||||
from aiosmtpd.smtp import Envelope
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
aiodns==3.2.0
|
||||
aiohttp==3.9.5
|
||||
aiosignal==1.3.1
|
||||
aiosmtpd==1.4.4.post2
|
||||
aiosmtplib==3.0.1
|
||||
anyio==4.1.0
|
||||
|
@ -28,6 +31,7 @@ django-rest-knox==4.2.0
|
|||
django-soft-delete==0.9.21
|
||||
djangorestframework==3.14.0
|
||||
drf-yasg==1.21.7
|
||||
frozenlist==1.4.1
|
||||
h11==0.14.0
|
||||
hyperlink==21.0.0
|
||||
idna==3.4
|
||||
|
@ -38,11 +42,13 @@ Jinja2==3.1.2
|
|||
MarkupSafe==2.1.3
|
||||
msgpack==1.0.7
|
||||
msgpack-python==0.5.6
|
||||
multidict==6.0.5
|
||||
openapi-codec==1.3.2
|
||||
packaging==23.2
|
||||
Pillow==10.1.0
|
||||
pyasn1==0.5.1
|
||||
pyasn1-modules==0.3.0
|
||||
pycares==4.4.0
|
||||
pycparser==2.21
|
||||
pyOpenSSL==23.3.0
|
||||
python-dotenv==1.0.0
|
||||
|
@ -65,4 +71,5 @@ urllib3==2.1.0
|
|||
uvicorn==0.24.0.post1
|
||||
watchfiles==0.21.0
|
||||
websockets==12.0
|
||||
yarl==1.9.4
|
||||
zope.interface==6.1
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
aiodns==3.2.0
|
||||
aiohttp==3.9.5
|
||||
aiosignal==1.3.1
|
||||
aiosmtpd==1.4.4.post2
|
||||
aiosmtplib==3.0.1
|
||||
asgiref==3.7.2
|
||||
|
|
|
@ -10,6 +10,7 @@ from asgiref.sync import async_to_sync
|
|||
from channels.layers import get_channel_layer
|
||||
|
||||
from core.settings import MAIL_DOMAIN
|
||||
from inventory.models import Event
|
||||
from mail.models import Email
|
||||
from mail.protocol import send_smtp, make_reply, collect_references
|
||||
from notify_sessions.models import SystemEvent
|
||||
|
|
Loading…
Reference in a new issue