From 577e37ed5695d776973030c749a6bdd8b54d6ef5 Mon Sep 17 00:00:00 2001 From: mrf7777 Date: Mon, 22 Jan 2024 21:05:11 -0600 Subject: [PATCH] add time to chat messages --- furfolio/models/chat.py | 4 ++++ furfolio/models/support.py | 2 +- furfolio/templates/furfolio/chat/chat.html | 8 ++++---- furfolio/templates/furfolio/chat/message_card.html | 7 +++++++ 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/furfolio/models/chat.py b/furfolio/models/chat.py index 0f971a6..c29e5af 100644 --- a/furfolio/models/chat.py +++ b/furfolio/models/chat.py @@ -1,6 +1,7 @@ from django.db import models from django.conf import settings from django.urls import reverse +from django.utils import timesince import math from .. import validators as furfolio_validators @@ -100,6 +101,9 @@ class ChatMessage(models.Model): def __str__(self): return f"\"{self.author}\" made message in \"{self.chat}\"" + + def timesince_created(self): + return timesince.timesince(self.created_date) def get_html_id(self) -> str: return "message_" + str(self.pk) diff --git a/furfolio/models/support.py b/furfolio/models/support.py index fdc2482..25ea93b 100644 --- a/furfolio/models/support.py +++ b/furfolio/models/support.py @@ -3,6 +3,7 @@ from django.core import validators from django.urls import reverse from django.utils.safestring import mark_safe + import math from .. import mixins @@ -82,6 +83,5 @@ def friendly_state_text(self) -> str: states_as_dict = dict(self.STATE_CHOICES) return states_as_dict[self.state] - created_date = models.DateTimeField(name="created_date", auto_now_add=True) updated_date = models.DateTimeField(name="updated_date", auto_now=True) diff --git a/furfolio/templates/furfolio/chat/chat.html b/furfolio/templates/furfolio/chat/chat.html index 95ac5d4..ea9eb71 100644 --- a/furfolio/templates/furfolio/chat/chat.html +++ b/furfolio/templates/furfolio/chat/chat.html @@ -14,17 +14,17 @@
{% if message.attachment %} - {% include "./message_card.html" with id=message.get_html_id text=message.message sender="CURRENT_USER" attachment_url=message.attachment.url attachment_name=message.attachment.name only %} + {% include "./message_card.html" with id=message.get_html_id timesince=message.timesince_created created_date=message.created_date text=message.message sender="CURRENT_USER" attachment_url=message.attachment.url attachment_name=message.attachment.name only %} {% else %} - {% include "./message_card.html" with id=message.get_html_id text=message.message sender="CURRENT_USER" only %} + {% include "./message_card.html" with id=message.get_html_id timesince=message.timesince_created created_date=message.created_date text=message.message sender="CURRENT_USER" only %} {% endif %}
{% else %}
{% if message.attachment %} - {% include "./message_card.html" with id=message.get_html_id user=message.author text=message.message sender="NON_CURRENT_USER" attachment_url=message.attachment.url attachment_name=message.attachment.name only %} + {% include "./message_card.html" with id=message.get_html_id timesince=message.timesince_created created_date=message.created_date user=message.author text=message.message sender="NON_CURRENT_USER" attachment_url=message.attachment.url attachment_name=message.attachment.name only %} {% else %} - {% include "./message_card.html" with id=message.get_html_id user=message.author text=message.message sender="NON_CURRENT_USER" only %} + {% include "./message_card.html" with id=message.get_html_id timesince=message.timesince_created created_date=message.created_date user=message.author text=message.message sender="NON_CURRENT_USER" only %} {% endif %}
diff --git a/furfolio/templates/furfolio/chat/message_card.html b/furfolio/templates/furfolio/chat/message_card.html index feba8d5..a3f7a38 100644 --- a/furfolio/templates/furfolio/chat/message_card.html +++ b/furfolio/templates/furfolio/chat/message_card.html @@ -4,6 +4,8 @@ - text: the text - attachment_name: (optional) the name of the attachment - attachment_url: (optional) the url of the attachment + - created_date: (optional) a date time that represents the created date time of the message + - timesince: (optional) timesince created - id: the HTML id of the card. - sender: an enum value: "CURRENT_USER" or "NON_CURRENT_USER" - "CURRENT_USER": use this to make card styles as if the message is by the signed in user @@ -21,8 +23,13 @@ {{ text|urlize }}

{% if attachment_name and attachment_url %} +

{{ attachment_name }} +

{% endif %} +
+ {{ timesince }} ago +
\ No newline at end of file