chat messages get saved to database, all timestamps localized

This commit is contained in:
iou1name 2018-08-20 06:57:36 -04:00
parent 08baf42812
commit 37b1aca048
12 changed files with 129 additions and 20 deletions

View File

@ -37,8 +37,8 @@
<li><a onclick="toggleHeader();" href="javascript:void(0);"></a></li>
</ul>
{#<br /> <!-- TODO: make this more exact. -->#}
<div id="messages">
<ul class="messages">
<div id="pageMessages">
<ul class="pageMessages">
{% for message in get_messages(request) %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}

View File

@ -8,26 +8,35 @@ import types
import bleach
from quest.models import Message, Quest
def message(socket, data):
"""
Gets called when the server receives a 'message' event.
"""
# TODO: validation
message = data.get('message')
quest_id = data.get('quest_id')
message = message.strip()
if not message:
return
tags = ["b", "code", "i", "s"]
message = bleach.clean(message, tags=tags)
message_id = 1
date = int(time.time())
name = "Anonymous"
user = socket.scope['user']
m = Message(
quest=Quest.objects.get(id=quest_id),
message=message)
if user.username:
m.user = user
m.save()
data = {}
data['message_id'] = message_id
data['message_id'] = m.id
data['message'] = message
data['date'] = date
data['name'] = name
data['date'] = int(time.time())
data['name'] = user.username
socket.send('message', data)

View File

@ -39,7 +39,7 @@
<div class="questPost pollPost{% if post == posts|last %} activePost{% endif %}">
{% endif %}
<div class="questPostMeta">
{{ post.timestamp.strftime('%Y-%m-%d %H:%M') }}
{{ localtime(post.timestamp).strftime('%Y-%m-%d %H:%M') }}
{% if request.user == quest.owner %}
{% if post.post_type == "text" %}
<br /><a href="javascript:void(0);" id="editPost-{{ post.id }}" onclick="edit_post({{ post.id }})">Edit</a>
@ -140,17 +140,17 @@
<h1>Chat</h1>
<div id="chatWindow">
{% autoescape false %}
{# for message in messages %}
<div id="msg-{{ message[0] }}" class="message">
{% for message in messages %}
<div id="msg-{{ message.id }}" class="message">
<div class="messageHeader">
<span class="messageName">Anonymous</span>
<span class="messageDate">{{ message[3] | strftime }}</span>
<span class="messageID">No.<a href="javascript:quote('{{ message[0] }}')">{{ message[0] }}</a></span>
<span class="messageName">{{ message.user.username or 'Anonymous' }}</span>
<span class="messageDate">{{ localtime(message.timestamp).strftime('%Y-%m-%d %H:%M:%S') }}</span>
<span class="messageID">No.<a href="javascript:quote('{{ message }}')">{{ message.id }}</a></span>
</div>
<div class="messageContent">{{ message[4] }}</div>
<div class="messageContent">{{ message.message }}</div>
</div>
<hr>
{% endfor #}
{% endfor %}
{% endautoescape %}
</div>
<div id="messageTextDiv"><textarea id="messageTextArea"></textarea></div>

View File

@ -0,0 +1,26 @@
# Generated by Django 2.1 on 2018-08-17 11:19
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('quest', '0002_auto_20180812_1612'),
]
operations = [
migrations.CreateModel(
name='Message',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateTimeField(auto_now=True)),
('message', models.TextField(max_length=512)),
('quest', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quest.Quest')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 2.1 on 2018-08-17 11:24
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('quest', '0003_message'),
]
operations = [
migrations.AlterField(
model_name='message',
name='user',
field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 2.1 on 2018-08-17 11:39
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('quest', '0004_auto_20180817_0724'),
]
operations = [
migrations.AlterField(
model_name='message',
name='user',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1 on 2018-08-17 13:21
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('quest', '0005_auto_20180817_0739'),
]
operations = [
migrations.RenameField(
model_name='message',
old_name='date',
new_name='timestamp',
),
]

View File

@ -40,3 +40,16 @@ class PageTitle(models.Model):
def __str__(self):
return self.title
class Message(models.Model):
"""
Represents a chat message.
"""
quest = models.ForeignKey(Quest, on_delete=models.CASCADE)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
blank=True,
null=True)
timestamp = models.DateTimeField(auto_now=True)
message = models.TextField(max_length=512)

View File

@ -47,7 +47,7 @@ function load() {
mtarea.value = '';
if (text == '') { return; }
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
socket.send('message', {message: text});
socket.send('message', {message: text, quest_id: quest_id});
}
});
}

View File

@ -20,6 +20,7 @@ def quest(request, quest_id, page_num=1):
Arbituary quest page view.
"""
quest = Quest.objects.get(id=quest_id)
messages = quest.message_set.all()
posts = quest.post_set.all()
context = {'quest': quest, 'posts': posts, 'page_num': page_num}
context = {'quest': quest, 'posts': posts, 'messages': messages, 'page_num': page_num}
return render(request, 'quest/quest.html', context)

View File

@ -5,6 +5,7 @@ Custom Jinja2 environment.
import jinja2
from django.urls import reverse
from django.contrib import messages
from django.utils.timezone import template_localtime
from django.contrib.staticfiles.storage import staticfiles_storage
def environment(**options):
@ -21,5 +22,6 @@ def environment(**options):
'url': reverse,
'static': staticfiles_storage.url,
'get_messages': messages.get_messages,
'localtime': template_localtime,
})
return env

View File

@ -119,7 +119,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
TIME_ZONE = 'America/Chicago'
USE_I18N = False