Merge branch 'Chatte' into feat/accounts

This commit is contained in:
starnakin 2023-11-29 18:36:08 +01:00
commit 9947ea37e2
14 changed files with 62 additions and 5 deletions

0
chat/__init__.py Normal file
View File

3
chat/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
chat/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ChatConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'chat'

View File

@ -31,6 +31,6 @@ class ChatConsumer(WebsocketConsumer):
self.send(text_data=json.dumps({
'type':'chat',
'username':self.scope["user"].username,
'message':message
}))

3
chat/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
chat/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
chat/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@ -12,6 +12,8 @@ import { Client } from "./api/client.js";
let client = new Client(location.protocol + "//" + location.host)
let lastView = undefined
const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$");
const getParams = match => {
@ -57,7 +59,12 @@ const router = async (uri = "") => {
result: [uri]
};
}
if (lastView !== undefined)
await lastView.leavePage();
const view = new match.route.view(getParams(match));
lastView = view;
let content = await view.getHtml();
if (content == null)
@ -81,4 +88,4 @@ document.addEventListener("DOMContentLoaded", () => {
router(location.pathname);
});
export { client, navigateTo }
export { client, navigateTo }

View File

@ -6,6 +6,9 @@ export default class {
async postInit() {
}
async leavePage() {
}
setTitle(title) {
document.title = title;
}

View File

@ -14,8 +14,9 @@ export default class extends AbstractView {
if (data.type === 'chat') {
let messages = document.getElementById('messages')
let username = data.username === null || data.username.length <= 0 ? "NoName" : data.username;
messages.insertAdjacentHTML('beforeend', `
<div><p>${data.message}</p></div>
<div><p>${username}: ${data.message}</p></div>
`)
}
@ -32,6 +33,11 @@ export default class extends AbstractView {
}))
form.reset()
})
}
async leavePage() {
this.chatSocket.close();
}
async getHtml() {

View File

@ -6,4 +6,5 @@ djangorestframework==3.14.0
install==1.3.5
pytz==2023.3.post1
sqlparse==0.4.4
channels==3.0.5
channels==4.0.0
daphne

View File

@ -8,9 +8,20 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
"""
import os
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import chat.routing
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings')
application = get_asgi_application()
application = ProtocolTypeRouter({
'http':get_asgi_application(),
'websocket':AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
)
})

View File

@ -38,6 +38,9 @@ CORS_ORIGIN_WHITELIST = (
# Application definition
INSTALLED_APPS = [
'channels',
'daphne',
'accounts.apps.AccountsConfig',
'profiles.apps.ProfilesConfig',
'frontend.apps.FrontendConfig',
@ -52,6 +55,14 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
]
ASGI_APPLICATION = 'trancendence.asgi.application'
CHANNEL_LAYERS = {
'default' :{
'BACKEND':'channels.layers.InMemoryChannelLayer'
}
}
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',