Skip to content

Commit

Permalink
update EmailLastSeen on Topic/Post create or update
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Feb 11, 2025
1 parent f6b6d27 commit d7c09d3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lacommunaute/forum_conversation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from lacommunaute.forum_moderation.enums import BlockedPostReason
from lacommunaute.forum_moderation.models import BlockedPost
from lacommunaute.forum_moderation.utils import check_post_approbation
from lacommunaute.users.enums import EmailLastSeenKind
from lacommunaute.users.models import EmailLastSeen


class CreateUpdatePostMixin:
Expand Down Expand Up @@ -35,6 +37,13 @@ def update_post(self, post):

post.updates_count = F("updates_count") + 1

def save(self):
post = super().save()
EmailLastSeen.objects.seen(
email=post.username if self.user.is_anonymous else self.user.email, kind=EmailLastSeenKind.POST
)
return post


class PostForm(CreateUpdatePostMixin, AbstractPostForm):
subject = CharField(widget=HiddenInput(), required=False)
Expand Down
32 changes: 32 additions & 0 deletions lacommunaute/forum_conversation/tests/tests_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
)
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.users.enums import EmailLastSeenKind
from lacommunaute.users.factories import UserFactory
from lacommunaute.users.models import EmailLastSeen


faker = Faker(settings.LANGUAGE_CODE)
Expand Down Expand Up @@ -86,6 +88,10 @@ def get_post_update_url(post):
)


def check_email_last_seen(email):
assert EmailLastSeen.objects.filter(email=email, last_seen_kind=EmailLastSeenKind.POST).exists()


superuser_hidden_fields = {
"poll-TOTAL_FORMS": 2,
"poll-INITIAL_FORMS": 0,
Expand All @@ -111,6 +117,8 @@ def test_create_topic_as_anonymous(self, db, client, public_forum):
assert topic.first_post.updates_count == 0
assert topic.first_post.updated_by is None

check_email_last_seen(username)

def test_update_anonymous_topic_as_self(self, db, client, public_forum):
topic = AnonymousTopicFactory(forum=public_forum, with_post=True)
username = topic.first_post.username
Expand All @@ -128,6 +136,9 @@ def test_update_anonymous_topic_as_self(self, db, client, public_forum):
assert topic.first_post.updates_count == 0 # surprisingly, this is not incremented
assert topic.first_post.updated_by is None # updated_by is a FK on User

# vincentporte TODO: assert UpdateTopicView override has to be fix up
# check_email_last_seen(username)

def test_update_anonymous_topic_as_superuser(self, db, client, public_forum):
topic = AnonymousTopicFactory(forum=public_forum, with_post=True)
username = topic.first_post.username
Expand All @@ -149,6 +160,8 @@ def test_update_anonymous_topic_as_superuser(self, db, client, public_forum):
assert topic.first_post.updates_count == 1
assert topic.first_post.updated_by == superuser

check_email_last_seen(superuser.email)

def test_create_topic_as_authenticated(self, db, client, public_forum):
user = UserFactory()
client.force_login(user)
Expand All @@ -165,6 +178,8 @@ def test_create_topic_as_authenticated(self, db, client, public_forum):
assert topic.first_post.updates_count == 0
assert topic.first_post.updated_by is None

check_email_last_seen(user.email)

def test_update_authenticated_topic_as_self(self, db, client, public_forum):
topic = TopicFactory(forum=public_forum, with_post=True)
user = topic.poster
Expand All @@ -181,6 +196,8 @@ def test_update_authenticated_topic_as_self(self, db, client, public_forum):
assert topic.first_post.updates_count == 1
assert topic.first_post.updated_by == user

check_email_last_seen(user.email)

def test_update_authenticated_topic_as_superuser(self, db, client, public_forum):
topic = TopicFactory(forum=public_forum, with_post=True)
user = topic.poster
Expand All @@ -202,6 +219,8 @@ def test_update_authenticated_topic_as_superuser(self, db, client, public_forum)
assert topic.first_post.updates_count == 1
assert topic.first_post.updated_by == superuser

check_email_last_seen(superuser.email)

def test_init_tags_when_creating_topic(self, db, client, public_forum):
response = client.get(get_create_topic_url(public_forum))
assert response.status_code == 200
Expand Down Expand Up @@ -240,6 +259,8 @@ def test_reply_as_anonymous(self, db, client, public_forum):
assert post.updates_count == 0
assert post.updated_by is None

check_email_last_seen(username)

def test_update_anonymous_reply_as_self(self, db, client, public_forum):
post = AnonymousPostFactory(topic=TopicFactory(forum=public_forum, with_post=True))
username = post.username
Expand All @@ -256,6 +277,9 @@ def test_update_anonymous_reply_as_self(self, db, client, public_forum):
assert post.updates_count == 0 # surprisingly, this is not incremented
assert post.updated_by is None

# vincentporte TODO: assert UpdatePostView override has to be fix up
# check_email_last_seen(username)

def test_update_anonymous_reply_as_superuser(self, db, client, public_forum):
post = AnonymousPostFactory(topic=TopicFactory(forum=public_forum, with_post=True))
username = post.username
Expand All @@ -272,6 +296,8 @@ def test_update_anonymous_reply_as_superuser(self, db, client, public_forum):
assert post.updates_count == 1
assert post.updated_by == superuser

check_email_last_seen(superuser.email)

def test_reply_as_authenticated(self, db, client, public_forum):
topic = TopicFactory(forum=public_forum, with_post=True)
user = UserFactory()
Expand All @@ -289,6 +315,8 @@ def test_reply_as_authenticated(self, db, client, public_forum):
assert post.updates_count == 0
assert post.updated_by is None

check_email_last_seen(user.email)

def test_update_authenticated_reply_as_self(self, db, client, public_forum):
post = PostFactory(topic=TopicFactory(forum=public_forum, with_post=True))
user = post.poster
Expand All @@ -304,6 +332,8 @@ def test_update_authenticated_reply_as_self(self, db, client, public_forum):
assert post.updates_count == 1
assert post.updated_by == user

check_email_last_seen(user.email)

def test_update_authenticated_reply_as_superuser(self, db, client, public_forum):
post = PostFactory(topic=TopicFactory(forum=public_forum, with_post=True))
user = post.poster
Expand All @@ -319,3 +349,5 @@ def test_update_authenticated_reply_as_superuser(self, db, client, public_forum)
assert post.poster == user
assert post.updates_count == 1
assert post.updated_by == superuser

check_email_last_seen(superuser.email)
31 changes: 31 additions & 0 deletions lacommunaute/forum_conversation/tests/tests_views_htmx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.test import RequestFactory, TestCase
Expand All @@ -6,6 +7,7 @@
from machina.core.db.models import get_model
from machina.core.loading import get_class

from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum_conversation.factories import CertifiedPostFactory, PostFactory, TopicFactory
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.models import CertifiedPost, Topic
Expand All @@ -15,7 +17,9 @@
from lacommunaute.forum_moderation.models import BlockedPost
from lacommunaute.forum_upvote.factories import UpVoteFactory
from lacommunaute.notification.factories import NotificationFactory
from lacommunaute.users.enums import EmailLastSeenKind
from lacommunaute.users.factories import UserFactory
from lacommunaute.users.models import EmailLastSeen


faker = Faker(settings.LANGUAGE_CODE)
Expand Down Expand Up @@ -424,6 +428,33 @@ def test_create_post_with_blocked_domain_name(self):
assert blocked_post.block_reason == BlockedPostReason.BLOCKED_DOMAIN


class TestPostFeedCreateView:
@pytest.mark.parametrize("logged", [True, False])
def test_email_last_seen_is_updated(self, client, db, logged):
topic = TopicFactory(with_post=True, forum=ForumFactory(with_public_perms=True))
url = reverse(
"forum_conversation_extension:post_create",
kwargs={
"forum_pk": topic.forum.pk,
"forum_slug": topic.forum.slug,
"pk": topic.pk,
"slug": topic.slug,
},
)
data = {"content": faker.paragraph(nb_sentences=5)}

if logged:
client.force_login(topic.poster)
else:
data["username"] = "[email protected]"

response = client.post(url, data=data)
assert response.status_code == 200

email = topic.poster.email if logged else data["username"]
assert EmailLastSeen.objects.filter(email=email, last_seen_kind=EmailLastSeenKind.POST).exists()


# vincentporte : not to futur self, rewrite it in pytest style
class CertifiedPostViewTest(TestCase):
@classmethod
Expand Down

0 comments on commit d7c09d3

Please sign in to comment.