From 975e7e5d64d50817a1617b45fe1d96d997a513b3 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Wed, 22 Nov 2023 16:25:01 +0100 Subject: [PATCH] update saved data items for inbound parsing --- lemarche/api/emails/serializers.py | 3 +++ lemarche/api/emails/views.py | 12 ++++++++++-- lemarche/static/js/filter_data_message.js | 22 +++++++++++++++------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lemarche/api/emails/serializers.py b/lemarche/api/emails/serializers.py index 00e8b3923..037ed5a03 100644 --- a/lemarche/api/emails/serializers.py +++ b/lemarche/api/emails/serializers.py @@ -27,5 +27,8 @@ class EmailItemSerializer(serializers.Serializer): Headers = serializers.DictField() +ATTRIBUTES_TO_SAVE_FOR_INBOUND = ["From", "To", "CC", "ReplyTo", "SentAtDate", "Attachments"] + + class EmailsSerializer(serializers.Serializer): items = serializers.ListField(child=EmailItemSerializer()) diff --git a/lemarche/api/emails/views.py b/lemarche/api/emails/views.py index 2b6cbba84..f1a28cefa 100644 --- a/lemarche/api/emails/views.py +++ b/lemarche/api/emails/views.py @@ -4,7 +4,7 @@ from rest_framework.response import Response from rest_framework.views import APIView -from lemarche.api.emails.serializers import EmailsSerializer +from lemarche.api.emails.serializers import ATTRIBUTES_TO_SAVE_FOR_INBOUND, EmailsSerializer from lemarche.conversations.models import Conversation from lemarche.conversations.utils import get_info_from_email_prefix from lemarche.www.conversations.tasks import send_email_from_conversation @@ -13,6 +13,13 @@ logger = logging.getLogger(__name__) +def clean_saved_data_of_inbound(data_inbound: dict): + clean_saved_data = {} + for key in ATTRIBUTES_TO_SAVE_FOR_INBOUND: + clean_saved_data[key] = data_inbound.get(key) + return clean_saved_data + + class InboundParsingEmailView(APIView): def post(self, request): serializer = EmailsSerializer(data=request.data) @@ -23,7 +30,8 @@ def post(self, request): version, conv_uuid, user_kind = get_info_from_email_prefix(inbound_email_prefix) conv: Conversation = Conversation.objects.get_conv_from_uuid(conv_uuid=conv_uuid, version=version) # save the input data - conv.data.append(serializer.data) + data_inbound_clean = clean_saved_data_of_inbound(data_inbound=serializer.data.get("items")[0]) + conv.data.append(data_inbound_clean) conv.save() # find user_kind if version >= 1: diff --git a/lemarche/static/js/filter_data_message.js b/lemarche/static/js/filter_data_message.js index 4ecd6d60a..fb68e65e8 100644 --- a/lemarche/static/js/filter_data_message.js +++ b/lemarche/static/js/filter_data_message.js @@ -1,8 +1,8 @@ function filterData(data, filterText) { return data.filter(item => { - const from = `${item.items[0].From.Name} || ${item.items[0].From.Address}`; - const to = item.items[0].To[0].Address; - const RawHtmlBody = item.items[0].RawHtmlBody; + const from = `${item.From.Name} || ${item.From.Address}`; + const to = item.To[0].Address; + const RawHtmlBody = item.RawHtmlBody; // filter fields return from.includes(filterText) || to.includes(filterText) || RawHtmlBody.includes(filterText); }); @@ -11,9 +11,10 @@ function filterData(data, filterText) { // Fonction pour générer les lignes du tableau HTML function generateTableRows(filteredData) { const rows = filteredData.map(item => { - const sendAt = new Date(item.items[0].SentAtDate).strftime('%d/%m/%Y %Hh%M'); - const from = `${item.items[0].From.Name} - ${item.items[0].From.Address}`; - const RawHtmlBody = item.items[0].RawHtmlBody ? item.items[0].RawHtmlBody : item.items[0].RawTextBody; + debugger + const sendAt = new Date(item.SentAtDate).strftime('%d/%m/%Y %Hh%M'); + const from = `${item.From.Name} - ${item.From.Address}`; + const RawHtmlBody = item.Attachments ? item.Attachments.length : 0; return ` ${sendAt} @@ -35,7 +36,14 @@ function init() { // const filteredData = filterData(rawData, filterText); // generate rows - const tableRows = generateTableRows(rawData); + const headTable = ` + + Date d'envoi + Expéditeur + Nombre de PJ + + `; + const tableRows = headTable + generateTableRows(rawData); // insert rows dataTableBody.innerHTML = tableRows;