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({ self.send(text_data=json.dumps({
'type':'chat', 'type':'chat',
'username':self.scope["user"].username,
'message':message '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 client = new Client(location.protocol + "//" + location.host)
let lastView = undefined
const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$"); const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$");
const getParams = match => { const getParams = match => {
@ -57,7 +59,12 @@ const router = async (uri = "") => {
result: [uri] result: [uri]
}; };
} }
if (lastView !== undefined)
await lastView.leavePage();
const view = new match.route.view(getParams(match)); const view = new match.route.view(getParams(match));
lastView = view;
let content = await view.getHtml(); let content = await view.getHtml();
if (content == null) if (content == null)

View File

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

View File

@ -14,8 +14,9 @@ export default class extends AbstractView {
if (data.type === 'chat') { if (data.type === 'chat') {
let messages = document.getElementById('messages') let messages = document.getElementById('messages')
let username = data.username === null || data.username.length <= 0 ? "NoName" : data.username;
messages.insertAdjacentHTML('beforeend', ` 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() form.reset()
}) })
}
async leavePage() {
this.chatSocket.close();
} }
async getHtml() { async getHtml() {

View File

@ -6,4 +6,5 @@ djangorestframework==3.14.0
install==1.3.5 install==1.3.5
pytz==2023.3.post1 pytz==2023.3.post1
sqlparse==0.4.4 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 import os
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import chat.routing
from django.core.asgi import get_asgi_application from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings') 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 # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'channels',
'daphne',
'accounts.apps.AccountsConfig', 'accounts.apps.AccountsConfig',
'profiles.apps.ProfilesConfig', 'profiles.apps.ProfilesConfig',
'frontend.apps.FrontendConfig', 'frontend.apps.FrontendConfig',
@ -52,6 +55,14 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
] ]
ASGI_APPLICATION = 'trancendence.asgi.application'
CHANNEL_LAYERS = {
'default' :{
'BACKEND':'channels.layers.InMemoryChannelLayer'
}
}
MIDDLEWARE = [ MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware', 'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',