Skip to content

Commit

Permalink
Merge pull request #435 from weni-ai/fix/chat-messages-timestamps
Browse files Browse the repository at this point in the history
Fix/chat messages timestamps
  • Loading branch information
kallilsouza authored Jan 7, 2025
2 parents 8ce1fb3 + 5a7e354 commit 9126de2
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 4.1.2 on 2025-01-06 19:32

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


class Migration(migrations.Migration):

dependencies = [
("msgs", "0007_alter_message_options"),
]

operations = [
migrations.AlterField(
model_name="message",
name="created_on",
field=models.DateTimeField(
default=django.utils.timezone.now,
editable=False,
verbose_name="Created on",
),
),
migrations.AlterField(
model_name="messagemedia",
name="created_on",
field=models.DateTimeField(
default=django.utils.timezone.now,
editable=False,
verbose_name="Created on",
),
),
]
6 changes: 3 additions & 3 deletions chats/apps/msgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import ValidationError

from chats.core.models import BaseModel
from chats.core.models import BaseModelWithManualCreatedOn
from chats.core.requests import get_request_session_with_retries


class Message(BaseModel):
class Message(BaseModelWithManualCreatedOn):
room = models.ForeignKey(
"rooms.Room",
related_name="messages",
Expand Down Expand Up @@ -105,7 +105,7 @@ def project(self):
return self.room.project


class MessageMedia(BaseModel):
class MessageMedia(BaseModelWithManualCreatedOn):
message = models.ForeignKey(
Message,
related_name="medias",
Expand Down
47 changes: 47 additions & 0 deletions chats/apps/msgs/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.test import TestCase
from django.utils import timezone
from django.utils.timezone import timedelta

from chats.apps.msgs.models import Message, MessageMedia
from chats.apps.rooms.models import Room


class TestMessageModel(TestCase):
def setUp(self):
self.room = Room.objects.create()

def test_create_message_passing_created_on(self):
timestamp = timezone.now() - timedelta(days=2)

msg = Message.objects.create(room=self.room, created_on=timestamp)

self.assertEqual(msg.created_on.date(), timestamp.date())
self.assertEqual(msg.created_on.hour, timestamp.hour)
self.assertEqual(msg.created_on.minute, timestamp.minute)
self.assertEqual(msg.created_on.second, timestamp.second)

def test_create_message_without_passing_created_on(self):
msg = Message.objects.create(room=self.room)

self.assertEqual(msg.created_on.date(), timezone.now().date())


class TestMessageMediaModel(TestCase):
def setUp(self):
self.room = Room.objects.create()
self.msg = Message.objects.create(room=self.room)

def test_create_message_media_passing_created_on(self):
timestamp = timezone.now() - timedelta(days=2)

msg_media = MessageMedia.objects.create(message=self.msg, created_on=timestamp)

self.assertEqual(msg_media.created_on.date(), timestamp.date())
self.assertEqual(msg_media.created_on.hour, timestamp.hour)
self.assertEqual(msg_media.created_on.minute, timestamp.minute)
self.assertEqual(msg_media.created_on.second, timestamp.second)

def test_create_message_media_without_passing_created_on(self):
msg_media = MessageMedia.objects.create(message=self.msg)

self.assertEqual(msg_media.created_on.date(), timezone.now().date())
18 changes: 12 additions & 6 deletions chats/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

class WebSocketsNotifiableMixin:
@property
def serialized_ws_data(self) -> dict:
...
def serialized_ws_data(self) -> dict: ...

@property
def notification_groups(self) -> list:
...
def notification_groups(self) -> list: ...

def get_action(self, action: str) -> str:
...
def get_action(self, action: str) -> str: ...

def notify(self, action: str, groups: list = [], content: dict = {}) -> None:
if "." not in action:
Expand Down Expand Up @@ -48,6 +45,15 @@ def edited(self) -> bool:
return bool(self.modified_by)


class BaseModelWithManualCreatedOn(BaseModel):
created_on = models.DateTimeField(
_("Created on"), editable=False, default=timezone.now
)

class Meta:
abstract = True


class BaseSoftDeleteModel(models.Model):
is_deleted = models.BooleanField(_("is deleted?"), default=False)

Expand Down

0 comments on commit 9126de2

Please sign in to comment.