From bc8c98e88fa09f53bd1cb799d55def06c2575477 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Thu, 23 Nov 2023 11:30:55 +0100 Subject: [PATCH] update saved data items for inbound parsing --- lemarche/api/emails/serializers.py | 3 +++ lemarche/api/emails/views.py | 20 ++++++++++++++++++-- lemarche/static/js/filter_data_message.js | 23 +++++++++++++++-------- 3 files changed, 36 insertions(+), 10 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..7d67a1514 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,21 @@ logger = logging.getLogger(__name__) +def clean_saved_data_of_inbound(data_inbound: dict): + """We clean saved data to respect the law of RGPD + + Args: + data_inbound (dict): all data that we receive from inbound api + + Returns: + dict with only neccessary key to save + """ + 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 +38,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..e39134b03 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,14 +11,14 @@ 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; + const sendAt = new Date(item.SentAtDate).strftime('%d/%m/%Y %Hh%M'); + const from = `${item.From.Name} - ${item.From.Address}`; + const attachementsCount = item.Attachments ? item.Attachments.length : 0; return ` ${sendAt} ${from} - ${RawHtmlBody} + ${attachementsCount} `; }); @@ -35,7 +35,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;