This commit is contained in:
jedi 2024-05-02 22:37:34 +02:00
parent 98a944902f
commit 9daf82c13a
7 changed files with 39 additions and 23 deletions

14
core/.coveragerc Normal file
View 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

View file

@ -1,5 +1,4 @@
from datetime import datetime
from django.utils import timezone
from django.urls import re_path
from rest_framework import routers, viewsets, serializers
from rest_framework.decorators import api_view, permission_classes, authentication_classes
@ -87,7 +86,7 @@ class ItemSerializer(serializers.ModelSerializer):
def update(self, instance, validated_data):
if 'returned' in validated_data:
if validated_data['returned']:
validated_data['returned_at'] = datetime.now()
validated_data['returned_at'] = timezone.now()
validated_data.pop('returned')
if 'dataImage' in validated_data:
file = File.objects.create(data=validated_data['dataImage'])

View file

@ -1,5 +1,4 @@
from datetime import datetime
from django.utils import timezone
from django.urls import path, re_path
from django.contrib.auth.decorators import permission_required
from rest_framework import routers, viewsets, serializers
@ -78,7 +77,7 @@ class ItemSerializer(serializers.ModelSerializer):
if container:
internal['container'] = container
if returned:
internal['returned_at'] = datetime.now()
internal['returned_at'] = timezone.now()
return internal
def validate(self, attrs):
@ -96,7 +95,7 @@ class ItemSerializer(serializers.ModelSerializer):
def update(self, instance, validated_data):
if 'returned' in validated_data:
if validated_data['returned']:
validated_data['returned_at'] = datetime.now()
validated_data['returned_at'] = timezone.now()
validated_data.pop('returned')
if 'dataImage' in validated_data:
file = File.objects.create(data=validated_data['dataImage'])

View file

@ -1,5 +1,4 @@
from datetime import datetime
from django.utils import timezone
from django.test import TestCase, Client
from django.contrib.auth.models import Permission
from knox.models import AuthToken
@ -164,7 +163,7 @@ class ItemTestCase(TestCase):
response = self.client.get(f'/api/2/{self.event.slug}/item/')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 2)
item2.returned_at = datetime.now()
item2.returned_at = timezone.now()
item2.save()
response = self.client.get(f'/api/2/{self.event.slug}/item/')
self.assertEqual(response.status_code, 200)

View file

@ -5,6 +5,7 @@ from urllib.parse import quote as urlencode
from core.settings import TELEGRAM_BOT_TOKEN, TELEGRAM_GROUP_CHAT_ID
from mail.models import UserNotificationChannel
from mail.protocol import send_smtp, make_notification
async def http_get(url):
@ -19,6 +20,11 @@ async def telegram_notify(message, chat_id):
return await http_get(url)
async def email_notify(message, email):
mail = await make_notification(message, email)
await send_smtp(mail)
class NotificationDispatcher:
channel_layer = None
room_group_name = "general"
@ -58,6 +64,6 @@ class NotificationDispatcher:
if target.channel_type == 'telegram':
await telegram_notify(message, target.channel_target)
elif target.channel_type == 'email':
print("Sending mail to:", target.channel_target)
await email_notify(message, target.channel_target)
else:
print("Unknown channel type:", target.channel_type)

View file

@ -100,8 +100,7 @@ def make_notification(message, to, event=None): # TODO where should replies to
return notification
async def send_smtp(message, log):
log.info('Sending message to %s' % message['To'])
async def send_smtp(message):
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False)
@ -198,13 +197,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 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 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 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]}")
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
@ -302,7 +301,8 @@ class LMTPHandler:
'general', {"type": "generic.event", "name": "user_notification", "event_id": systemevent.id,
"message": notification})
if new and reply:
await send_smtp(reply, log)
log.info('Sending message to %s' % reply['To'])
await send_smtp(reply)
log.info("Sent auto reply")
return '250 Message accepted for delivery'

View file

@ -47,8 +47,7 @@ def reply(request, pk):
body=request.data['message'],
in_reply_to=first_mail.reference,
)
log = logging.getLogger('mail.log')
async_to_sync(send_smtp)(make_reply(mail, references), log)
async_to_sync(send_smtp)(make_reply(mail, references))
return Response({'status': 'ok'}, status=status.HTTP_201_CREATED)