Skip to content

Commit

Permalink
add time to chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mrf7777 committed Jan 23, 2024
1 parent ed8eb11 commit 577e37e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions furfolio/models/chat.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion furfolio/models/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
8 changes: 4 additions & 4 deletions furfolio/templates/furfolio/chat/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
<div class="col-md-6"></div>
<div class="col-md-6">
{% 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 %}
</div>
{% else %}
<div class="col-md-6">
{% 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 %}
</div>
<div class="col-md-6"></div>
Expand Down
7 changes: 7 additions & 0 deletions furfolio/templates/furfolio/chat/message_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,8 +23,13 @@
{{ text|urlize }}
</p>
{% if attachment_name and attachment_url %}
<p>
<a href="{{ attachment_url }}">{{ attachment_name }}</a>
</p>
{% endif %}
<div class="fw-light">
{{ timesince }} ago
</div>
</div>
</div>
</div>

0 comments on commit 577e37e

Please sign in to comment.