Skip to content

Commit

Permalink
feat(notification): simplify collect_first_replies using Topic new me…
Browse files Browse the repository at this point in the history
…thods and qs filter
  • Loading branch information
vincentporte committed Sep 11, 2023
1 parent f06a44f commit d92ff8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 44 deletions.
44 changes: 10 additions & 34 deletions lacommunaute/notification/tests/tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,33 @@ def test_email_sent_track_with_kind(self):

class CollectFirstRepliesTestCase(TestCase):
@classmethod
def setUp(cls):
def setUpTestData(cls):
cls.topic = TopicFactory(with_post=True)

def test_no_reply_ever(self):
post = PostFactory(topic=self.topic)
self.assertEqual(
collect_first_replies(),
[
(
f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{post.topic.get_absolute_url()}",
post.topic.subject,
post.topic.poster_email,
post.poster_display_name,
)
],
)
def test_no_reply_to_be_notified(self):
self.assertEqual(len(list(collect_first_replies())), 0)

def test_no_reply_since_last_notification(self):
PostFactory(topic=self.topic)
EmailSentTrackFactory(kind="first_reply")

self.assertEqual(len(collect_first_replies()), 0)

def test_replies_since_last_notification(self):
EmailSentTrackFactory(kind="first_reply")
def test_first_reply(self):
post = PostFactory(topic=self.topic)

self.assertEqual(
collect_first_replies(),
[
(
f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{post.topic.get_absolute_url()}",
post.topic.subject,
post.topic.poster_email,
self.topic.get_absolute_url(with_fqdn=True),
self.topic.subject,
[self.topic.poster_email],
post.poster_display_name,
)
],
)

def test_unapproved_reply_in_previous_hour(self):
PostFactory(topic=self.topic, approved=False)
self.assertEqual(len(collect_first_replies()), 0)

def test_last_emailsenttrack_with_kind(self):
def test_first_reply_since_last_notification(self):
PostFactory(topic=self.topic)
EmailSentTrackFactory(kind="other")

EmailSentTrackFactory(kind="dummy")
self.assertEqual(len(collect_first_replies()), 1)

EmailSentTrackFactory(kind="first_reply")

EmailSentTrackFactory(kind=EmailSentTrackKind.FIRST_REPLY)
self.assertEqual(len(collect_first_replies()), 0)


Expand Down
16 changes: 6 additions & 10 deletions lacommunaute/notification/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import Count, F, Q
from django.utils.timezone import now, timedelta

from lacommunaute.forum_conversation.models import Post, Topic
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.notification.enums import EmailSentTrackKind
from lacommunaute.notification.models import BouncedEmail, EmailSentTrack
from lacommunaute.users.models import User
Expand All @@ -15,16 +15,12 @@ def last_notification(kind=None) -> datetime:
def collect_first_replies():
return [
(
f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{post.topic.get_absolute_url()}",
post.topic.subject,
post.topic.poster_email,
post.poster_display_name,
)
for post in Post.objects.filter(
created__gte=last_notification(kind=EmailSentTrackKind.FIRST_REPLY),
approved=True,
topic.get_absolute_url(with_fqdn=True),
topic.subject,
topic.mails_to_notify(),
topic.last_post.poster_display_name,
)
if post.position == 2
for topic in Topic.objects.with_first_reply(last_notification(kind=EmailSentTrackKind.FIRST_REPLY))
]


Expand Down

0 comments on commit d92ff8a

Please sign in to comment.