Skip to content

Commit

Permalink
Improve matching. Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 15, 2023
1 parent 1a28bab commit 88496d5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
9 changes: 6 additions & 3 deletions lemarche/api/emails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from lemarche.api.emails.serializers import 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 @@ -19,13 +20,15 @@ def post(self, request):
inbound_email = serializer.validated_data.get("items")[0]
inbound_email_prefix = inbound_email["To"][0]["Address"].split("@")[0]
# get conversation object
version, conv_uuid, user_kind = Conversation.get_info_from_email_prefix(inbound_email_prefix)
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)
conv.save()
user_kind = user_kind if version == 0 else conv.get_user_kind(conv_uuid)
# make the transfert of emails
# find user_kind
if version >= 1:
user_kind = conv.get_user_kind(conv_uuid)
# make the transfer of emails
send_email_from_conversation(
conv=conv,
user_kind=user_kind,
Expand Down
33 changes: 4 additions & 29 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings
from django.db import IntegrityError, models
from django.db.models import Func, IntegerField
from django.db.models import Func, IntegerField, Q
from django.utils import timezone
from django.utils.text import slugify
from django_extensions.db.fields import ShortUUIDField
Expand All @@ -27,7 +27,7 @@ def get_conv_from_uuid(self, conv_uuid: str, version=1):
if version == 0:
return self.get(uuid=conv_uuid)
else:
return self.get(models.Q(sender_encoded=conv_uuid) | models.Q(siae_encoded=conv_uuid))
return self.get(Q(sender_encoded__endswith=conv_uuid) | Q(siae_encoded__endswith=conv_uuid))


class Conversation(models.Model):
Expand Down Expand Up @@ -128,9 +128,9 @@ def save(self, *args, **kwargs):

def get_user_kind(self, conv_uuid):
# method only available in version >= 1
if conv_uuid == self.sender_encoded:
if self.sender_encoded.endswith(conv_uuid):
return self.USER_KIND_SENDER_TO_BUYER
elif conv_uuid == self.siae_encoded:
elif self.siae_encoded.endswith(conv_uuid):
return self.USER_KIND_SENDER_TO_SIAE

@property
Expand Down Expand Up @@ -170,31 +170,6 @@ def nb_messages(self):
"""
return len(self.data) + 1

@staticmethod
def get_info_from_email_prefix(email_prefix: str) -> list:
"""
Extract info from email_prefix
version 0 format: uuid_b, uuid_s (long uuid)
vesion 1 format: prenom_nom_uuid (short uuid)
Args:
email_prefix (str): _description_
Returns:
[VERSION, UUID, KIND_SENDER]
"""
email_prefix_infos = email_prefix.split("_")
# specificity of version 0: only 1 char at the end
if len(email_prefix_infos[-1]) == 1:
version = 0
uuid = email_prefix_infos[0]
kind_sender = email_prefix_infos[1]
else: # version 1
version = 1
uuid = email_prefix
kind_sender = None # not useful
return version, uuid, kind_sender

@property
def is_validated(self) -> bool:
return self.validated_at is not None
Expand Down
23 changes: 23 additions & 0 deletions lemarche/conversations/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def get_info_from_email_prefix(email_prefix: str) -> list:
"""
Extract info from email_prefix
version 0 format: uuid_b, uuid_s (long uuid)
vesion 1 format: prenom_nom_uuid (short uuid)
Args:
email_prefix (str): _description_
Returns:
[VERSION, UUID, KIND_SENDER]
"""
email_prefix_infos = email_prefix.split("_")
# specificity of version 0: only 1 char at the end
if len(email_prefix_infos[-1]) == 1:
version = 0
uuid = email_prefix_infos[0]
kind_sender = email_prefix_infos[1]
else: # version 1
version = 1
uuid = email_prefix[-1]
kind_sender = None # not useful
return version, uuid, kind_sender

0 comments on commit 88496d5

Please sign in to comment.