Skip to content

Commit

Permalink
update saved data items for inbound parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Nov 22, 2023
1 parent 19a49ee commit 975e7e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lemarche/api/emails/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
12 changes: 10 additions & 2 deletions lemarche/api/emails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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:
Expand Down
22 changes: 15 additions & 7 deletions lemarche/static/js/filter_data_message.js
Original file line number Diff line number Diff line change
@@ -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);
});
Expand All @@ -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 `<tr>
<td>${sendAt}</td>
Expand All @@ -35,7 +36,14 @@ function init() {
// const filteredData = filterData(rawData, filterText);

// generate rows
const tableRows = generateTableRows(rawData);
const headTable = `
<tr>
<th scope="col">Date d'envoi</th>
<th scope="col">Expéditeur</th>
<th scope="col">Nombre de PJ</th>
</tr>
`;
const tableRows = headTable + generateTableRows(rawData);

// insert rows
dataTableBody.innerHTML = tableRows;
Expand Down

0 comments on commit 975e7e5

Please sign in to comment.