chat messages get saved to database, all timestamps localized
This commit is contained in:
parent
08baf42812
commit
37b1aca048
|
@ -37,13 +37,13 @@
|
||||||
<li><a onclick="toggleHeader();" href="javascript:void(0);">⤓</a></li>
|
<li><a onclick="toggleHeader();" href="javascript:void(0);">⤓</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
{#<br /> <!-- TODO: make this more exact. -->#}
|
{#<br /> <!-- TODO: make this more exact. -->#}
|
||||||
<div id="messages">
|
<div id="pageMessages">
|
||||||
<ul class="messages">
|
<ul class="pageMessages">
|
||||||
{% for message in get_messages(request) %}
|
{% for message in get_messages(request) %}
|
||||||
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
|
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div id="content">{% block content %}{% endblock %} </div>
|
<div id="content">{% block content %}{% endblock %}</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -8,26 +8,35 @@ import types
|
||||||
|
|
||||||
import bleach
|
import bleach
|
||||||
|
|
||||||
|
from quest.models import Message, Quest
|
||||||
|
|
||||||
def message(socket, data):
|
def message(socket, data):
|
||||||
"""
|
"""
|
||||||
Gets called when the server receives a 'message' event.
|
Gets called when the server receives a 'message' event.
|
||||||
"""
|
"""
|
||||||
|
# TODO: validation
|
||||||
message = data.get('message')
|
message = data.get('message')
|
||||||
|
quest_id = data.get('quest_id')
|
||||||
|
|
||||||
message = message.strip()
|
message = message.strip()
|
||||||
if not message:
|
if not message:
|
||||||
return
|
return
|
||||||
tags = ["b", "code", "i", "s"]
|
tags = ["b", "code", "i", "s"]
|
||||||
message = bleach.clean(message, tags=tags)
|
message = bleach.clean(message, tags=tags)
|
||||||
message_id = 1
|
user = socket.scope['user']
|
||||||
date = int(time.time())
|
|
||||||
name = "Anonymous"
|
m = Message(
|
||||||
|
quest=Quest.objects.get(id=quest_id),
|
||||||
|
message=message)
|
||||||
|
if user.username:
|
||||||
|
m.user = user
|
||||||
|
m.save()
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
data['message_id'] = message_id
|
data['message_id'] = m.id
|
||||||
data['message'] = message
|
data['message'] = message
|
||||||
data['date'] = date
|
data['date'] = int(time.time())
|
||||||
data['name'] = name
|
data['name'] = user.username
|
||||||
socket.send('message', data)
|
socket.send('message', data)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
<div class="questPost pollPost{% if post == posts|last %} activePost{% endif %}">
|
<div class="questPost pollPost{% if post == posts|last %} activePost{% endif %}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<div class="questPostMeta">
|
<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 request.user == quest.owner %}
|
||||||
{% if post.post_type == "text" %}
|
{% if post.post_type == "text" %}
|
||||||
<br /><a href="javascript:void(0);" id="editPost-{{ post.id }}" onclick="edit_post({{ post.id }})">Edit</a>
|
<br /><a href="javascript:void(0);" id="editPost-{{ post.id }}" onclick="edit_post({{ post.id }})">Edit</a>
|
||||||
|
@ -140,17 +140,17 @@
|
||||||
<h1>Chat</h1>
|
<h1>Chat</h1>
|
||||||
<div id="chatWindow">
|
<div id="chatWindow">
|
||||||
{% autoescape false %}
|
{% autoescape false %}
|
||||||
{# for message in messages %}
|
{% for message in messages %}
|
||||||
<div id="msg-{{ message[0] }}" class="message">
|
<div id="msg-{{ message.id }}" class="message">
|
||||||
<div class="messageHeader">
|
<div class="messageHeader">
|
||||||
<span class="messageName">Anonymous</span>
|
<span class="messageName">{{ message.user.username or 'Anonymous' }}</span>
|
||||||
<span class="messageDate">{{ message[3] | strftime }}</span>
|
<span class="messageDate">{{ localtime(message.timestamp).strftime('%Y-%m-%d %H:%M:%S') }}</span>
|
||||||
<span class="messageID">No.<a href="javascript:quote('{{ message[0] }}')">{{ message[0] }}</a></span>
|
<span class="messageID">No.<a href="javascript:quote('{{ message }}')">{{ message.id }}</a></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="messageContent">{{ message[4] }}</div>
|
<div class="messageContent">{{ message.message }}</div>
|
||||||
</div>
|
</div>
|
||||||
<hr>
|
<hr>
|
||||||
{% endfor #}
|
{% endfor %}
|
||||||
{% endautoescape %}
|
{% endautoescape %}
|
||||||
</div>
|
</div>
|
||||||
<div id="messageTextDiv"><textarea id="messageTextArea"></textarea></div>
|
<div id="messageTextDiv"><textarea id="messageTextArea"></textarea></div>
|
||||||
|
|
26
quest/migrations/0003_message.py
Normal file
26
quest/migrations/0003_message.py
Normal 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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
20
quest/migrations/0004_auto_20180817_0724.py
Normal file
20
quest/migrations/0004_auto_20180817_0724.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
20
quest/migrations/0005_auto_20180817_0739.py
Normal file
20
quest/migrations/0005_auto_20180817_0739.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
18
quest/migrations/0006_auto_20180817_0921.py
Normal file
18
quest/migrations/0006_auto_20180817_0921.py
Normal 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',
|
||||||
|
),
|
||||||
|
]
|
|
@ -40,3 +40,16 @@ class PageTitle(models.Model):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
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)
|
||||||
|
|
|
@ -47,7 +47,7 @@ function load() {
|
||||||
mtarea.value = '';
|
mtarea.value = '';
|
||||||
if (text == '') { return; }
|
if (text == '') { return; }
|
||||||
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
|
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
|
||||||
socket.send('message', {message: text});
|
socket.send('message', {message: text, quest_id: quest_id});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ def quest(request, quest_id, page_num=1):
|
||||||
Arbituary quest page view.
|
Arbituary quest page view.
|
||||||
"""
|
"""
|
||||||
quest = Quest.objects.get(id=quest_id)
|
quest = Quest.objects.get(id=quest_id)
|
||||||
|
messages = quest.message_set.all()
|
||||||
posts = quest.post_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)
|
return render(request, 'quest/quest.html', context)
|
||||||
|
|
|
@ -5,6 +5,7 @@ Custom Jinja2 environment.
|
||||||
import jinja2
|
import jinja2
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
from django.utils.timezone import template_localtime
|
||||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||||
|
|
||||||
def environment(**options):
|
def environment(**options):
|
||||||
|
@ -21,5 +22,6 @@ def environment(**options):
|
||||||
'url': reverse,
|
'url': reverse,
|
||||||
'static': staticfiles_storage.url,
|
'static': staticfiles_storage.url,
|
||||||
'get_messages': messages.get_messages,
|
'get_messages': messages.get_messages,
|
||||||
|
'localtime': template_localtime,
|
||||||
})
|
})
|
||||||
return env
|
return env
|
||||||
|
|
|
@ -119,7 +119,7 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||||
|
|
||||||
LANGUAGE_CODE = 'en-us'
|
LANGUAGE_CODE = 'en-us'
|
||||||
|
|
||||||
TIME_ZONE = 'America/New_York'
|
TIME_ZONE = 'America/Chicago'
|
||||||
|
|
||||||
USE_I18N = False
|
USE_I18N = False
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user