Skip to content

Commit

Permalink
Emails : nouveau modèle TemplateTransactional pour stocker nos config…
Browse files Browse the repository at this point in the history
…urations (#1018)

* New conversations.TemplateTransactional model

* Add to admin

* Add source, is_active & code fields

* New property get_template_id
  • Loading branch information
raphodn authored Dec 19, 2023
1 parent 8d58481 commit c0f7819
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lemarche/conversations/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.http import HttpResponseRedirect

from lemarche.conversations.models import Conversation
from lemarche.conversations.models import Conversation, TemplateTransactional
from lemarche.utils.admin.admin_site import admin_site
from lemarche.utils.fields import pretty_print_readonly_jsonfield_to_table
from lemarche.www.conversations.tasks import send_first_email_from_conversation
Expand Down Expand Up @@ -130,3 +130,11 @@ def data_display(self, conversation: Conversation = None):
return "-"

data_display.short_description = "Messages de la conversation"


@admin.register(TemplateTransactional, site=admin_site)
class TemplateTransactionalAdmin(admin.ModelAdmin):
list_display = ["id", "name", "mailjet_id", "brevo_id", "source", "is_active", "created_at", "updated_at"]
search_fields = ["id", "name", "code", "mailjet_id", "brevo_id"]

readonly_fields = ["code", "created_at", "updated_at"]
8 changes: 8 additions & 0 deletions lemarche/conversations/constants.py
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
ATTRIBUTES_TO_SAVE_FOR_INBOUND = ["From", "To", "CC", "ReplyTo", "SentAtDate", "Attachments"]

SOURCE_MAILJET = "MAILJET"
SOURCE_BREVO = "BREVO"

SOURCE_CHOICES = (
(SOURCE_MAILJET, "Mailjet"),
(SOURCE_BREVO, "Brevo"),
)
66 changes: 66 additions & 0 deletions lemarche/conversations/migrations/0012_templatetransactional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated by Django 4.2.2 on 2023-12-14 09:41

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("conversations", "0011_conversation_sender_siae_encoded_unique"),
]

operations = [
migrations.CreateModel(
name="TemplateTransactional",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("name", models.CharField(max_length=255, verbose_name="Nom")),
(
"code",
models.CharField(
blank=True, db_index=True, max_length=255, null=True, unique=True, verbose_name="Nom technique"
),
),
("description", models.TextField(blank=True, verbose_name="Description")),
(
"mailjet_id",
models.IntegerField(
blank=True,
db_index=True,
null=True,
unique=True,
verbose_name="Identifiant Mailjet",
),
),
(
"brevo_id",
models.IntegerField(
blank=True,
db_index=True,
null=True,
unique=True,
verbose_name="Identifiant Brevo",
),
),
("is_active", models.BooleanField(default=False, verbose_name="Actif")),
(
"source",
models.CharField(
blank=True,
choices=[("MAILJET", "Mailjet"), ("BREVO", "Brevo")],
max_length=20,
verbose_name="Source",
),
),
(
"created_at",
models.DateTimeField(default=django.utils.timezone.now, verbose_name="Date de création"),
),
("updated_at", models.DateTimeField(auto_now=True, verbose_name="Date de modification")),
],
options={
"verbose_name": "Template transactionnel",
"verbose_name_plural": "Templates transactionnels",
},
),
]
38 changes: 38 additions & 0 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django_extensions.db.fields import ShortUUIDField
from shortuuid import uuid

from lemarche.conversations import constants as conversation_constants


class ConversationQuerySet(models.QuerySet):
def has_answer(self):
Expand Down Expand Up @@ -182,3 +184,39 @@ def is_validated(self) -> bool:
def set_validated(self):
self.validated_at = timezone.now()
self.save()


class TemplateTransactional(models.Model):
name = models.CharField(verbose_name="Nom", max_length=255)
code = models.CharField(
verbose_name="Nom technique", max_length=255, unique=True, db_index=True, blank=True, null=True
)
description = models.TextField(verbose_name="Description", blank=True)
mailjet_id = models.IntegerField(
verbose_name="Identifiant Mailjet", unique=True, db_index=True, blank=True, null=True
)
brevo_id = models.IntegerField(verbose_name="Identifiant Brevo", unique=True, db_index=True, blank=True, null=True)

source = models.CharField(
verbose_name="Source", max_length=20, choices=conversation_constants.SOURCE_CHOICES, blank=True
)
is_active = models.BooleanField(verbose_name="Actif", default=False)

created_at = models.DateTimeField(verbose_name="Date de création", default=timezone.now)
updated_at = models.DateTimeField(verbose_name="Date de modification", auto_now=True)

class Meta:
verbose_name = "Template transactionnel"
verbose_name_plural = "Templates transactionnels"

def __str__(self):
return self.name

@property
def get_template_id(self):
if self.is_active and self.source and self.code:
if self.source == conversation_constants.SOURCE_MAILJET:
return self.mailjet_id
elif self.source == conversation_constants.SOURCE_BREVO:
return self.brevo_id
return None
26 changes: 25 additions & 1 deletion lemarche/conversations/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.test import TestCase

from lemarche.conversations import constants as conversation_constants
from lemarche.conversations.factories import ConversationFactory
from lemarche.conversations.models import Conversation
from lemarche.conversations.models import Conversation, TemplateTransactional
from lemarche.siaes.factories import SiaeFactory


Expand Down Expand Up @@ -64,3 +65,26 @@ def test_with_answer_stats(self):
conversation_queryset = Conversation.objects.with_answer_stats()
self.assertEqual(conversation_queryset.get(id=self.conversation.id).answer_count_annotated, 0)
self.assertEqual(conversation_queryset.get(id=self.conversation_with_answer.id).answer_count_annotated, 1)


class TemplateTransactionalModelTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.tt_inactive = TemplateTransactional(
code="EMAIL_1", mailjet_id=10, brevo_id=11, source=conversation_constants.SOURCE_MAILJET, is_active=False
)
cls.tt_active_empty = TemplateTransactional(
code="EMAIL_2", source=conversation_constants.SOURCE_MAILJET, is_active=False
)
cls.tt_active_mailjet = TemplateTransactional(
code="EMAIL_3", mailjet_id=30, brevo_id=31, source=conversation_constants.SOURCE_MAILJET, is_active=True
)
cls.tt_active_brevo = TemplateTransactional(
code="EMAIL_4", mailjet_id=40, brevo_id=41, source=conversation_constants.SOURCE_BREVO, is_active=True
)

def test_get_template_id(self):
self.assertIsNone(self.tt_inactive.get_template_id)
self.assertIsNone(self.tt_active_empty.get_template_id)
self.assertEqual(self.tt_active_mailjet.get_template_id, self.tt_active_mailjet.mailjet_id)
self.assertEqual(self.tt_active_brevo.get_template_id, self.tt_active_brevo.brevo_id)

0 comments on commit c0f7819

Please sign in to comment.