From 7b6a8ba57b0f7bd60c7bb8aa8c756abb08fee7ec Mon Sep 17 00:00:00 2001 From: Xamora Date: Mon, 27 Nov 2023 15:47:00 +0100 Subject: [PATCH 1/4] Minor Edit and clear one file --- frontend/consumers.py | 1 + frontend/static/js/views/Chat.js | 4 +++- frontend/templates/chat.html | 24 ------------------------ 3 files changed, 4 insertions(+), 25 deletions(-) delete mode 100644 frontend/templates/chat.html diff --git a/frontend/consumers.py b/frontend/consumers.py index f3e4e6e..a86a0c7 100644 --- a/frontend/consumers.py +++ b/frontend/consumers.py @@ -31,6 +31,7 @@ class ChatConsumer(WebsocketConsumer): self.send(text_data=json.dumps({ 'type':'chat', + 'username':self.scope["user"].pk, 'message':message })) diff --git a/frontend/static/js/views/Chat.js b/frontend/static/js/views/Chat.js index cbde8e7..c620d9c 100644 --- a/frontend/static/js/views/Chat.js +++ b/frontend/static/js/views/Chat.js @@ -14,8 +14,9 @@ export default class extends AbstractView { if (data.type === 'chat') { let messages = document.getElementById('messages') + let username = data.username === null ? "NoName" : data.username; messages.insertAdjacentHTML('beforeend', ` -

${data.message}

+

${username}: ${data.message}

`) } @@ -32,6 +33,7 @@ export default class extends AbstractView { })) form.reset() }) + } async getHtml() { diff --git a/frontend/templates/chat.html b/frontend/templates/chat.html deleted file mode 100644 index 39b695c..0000000 --- a/frontend/templates/chat.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - - Chat - - -

Chat

- -
- -
- - - Date: Mon, 27 Nov 2023 15:47:33 +0100 Subject: [PATCH 2/4] forget push file --- requirements.txt | 1 + trancendence/asgi.py | 13 ++++++++++++- trancendence/settings.py | 11 +++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index f301200..46ca335 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ install==1.3.5 pytz==2023.3.post1 sqlparse==0.4.4 channels==3.0.5 +daphne diff --git a/trancendence/asgi.py b/trancendence/asgi.py index eaaad8a..5608a42 100644 --- a/trancendence/asgi.py +++ b/trancendence/asgi.py @@ -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 frontend.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( + frontend.routing.websocket_urlpatterns + ) + ) +}) + diff --git a/trancendence/settings.py b/trancendence/settings.py index a8d3760..2c57c6a 100644 --- a/trancendence/settings.py +++ b/trancendence/settings.py @@ -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', From 8bce7d33ca3d8aa063156cfce6b7dd3829463c13 Mon Sep 17 00:00:00 2001 From: Xamora Date: Mon, 27 Nov 2023 21:59:41 +0100 Subject: [PATCH 3/4] The project advance --- frontend/consumers.py | 3 +-- frontend/static/js/index.js | 5 +++++ frontend/static/js/views/AbstractView.js | 3 +++ frontend/static/js/views/Chat.js | 4 ++++ requirements.txt | 2 +- 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/frontend/consumers.py b/frontend/consumers.py index a86a0c7..a1a6602 100644 --- a/frontend/consumers.py +++ b/frontend/consumers.py @@ -31,7 +31,6 @@ class ChatConsumer(WebsocketConsumer): self.send(text_data=json.dumps({ 'type':'chat', - 'username':self.scope["user"].pk, + 'username':self.scope["user"].username, 'message':message })) - diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 20380ea..871f2d8 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -10,6 +10,8 @@ import RegisterView from "./views/accounts/RegisterView.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 => { @@ -54,7 +56,10 @@ const router = async () => { }; } + if (lastView !== undefined) + await lastView.leavePage(); const view = new match.route.view(getParams(match)); + lastView = view; document.querySelector("#app").innerHTML = await view.getHtml(); diff --git a/frontend/static/js/views/AbstractView.js b/frontend/static/js/views/AbstractView.js index f9f699b..f1a14f2 100644 --- a/frontend/static/js/views/AbstractView.js +++ b/frontend/static/js/views/AbstractView.js @@ -6,6 +6,9 @@ export default class { async postInit() { } + async leavePage() { + } + setTitle(title) { document.title = title; } diff --git a/frontend/static/js/views/Chat.js b/frontend/static/js/views/Chat.js index c620d9c..0f2b71d 100644 --- a/frontend/static/js/views/Chat.js +++ b/frontend/static/js/views/Chat.js @@ -36,6 +36,10 @@ export default class extends AbstractView { } + async leavePage() { + this.chatSocket.close(); + } + async getHtml() { return `

Chat

diff --git a/requirements.txt b/requirements.txt index 46ca335..53e09ab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,5 +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 From b12c03074af1d9ec0c66aa55e52d60bf862bb231 Mon Sep 17 00:00:00 2001 From: Xamora Date: Mon, 27 Nov 2023 23:31:31 +0100 Subject: [PATCH 4/4] Chat advance --- chat/__init__.py | 0 chat/admin.py | 3 +++ chat/apps.py | 6 ++++++ {frontend => chat}/consumers.py | 0 chat/models.py | 3 +++ {frontend => chat}/routing.py | 0 chat/tests.py | 3 +++ chat/views.py | 3 +++ frontend/static/js/views/Chat.js | 2 +- trancendence/asgi.py | 4 ++-- 10 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 chat/__init__.py create mode 100644 chat/admin.py create mode 100644 chat/apps.py rename {frontend => chat}/consumers.py (100%) create mode 100644 chat/models.py rename {frontend => chat}/routing.py (100%) create mode 100644 chat/tests.py create mode 100644 chat/views.py diff --git a/chat/__init__.py b/chat/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chat/admin.py b/chat/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/chat/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/chat/apps.py b/chat/apps.py new file mode 100644 index 0000000..2fe899a --- /dev/null +++ b/chat/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ChatConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'chat' diff --git a/frontend/consumers.py b/chat/consumers.py similarity index 100% rename from frontend/consumers.py rename to chat/consumers.py diff --git a/chat/models.py b/chat/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/chat/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/frontend/routing.py b/chat/routing.py similarity index 100% rename from frontend/routing.py rename to chat/routing.py diff --git a/chat/tests.py b/chat/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/chat/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/chat/views.py b/chat/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/chat/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/frontend/static/js/views/Chat.js b/frontend/static/js/views/Chat.js index 0f2b71d..7d071b3 100644 --- a/frontend/static/js/views/Chat.js +++ b/frontend/static/js/views/Chat.js @@ -14,7 +14,7 @@ export default class extends AbstractView { if (data.type === 'chat') { let messages = document.getElementById('messages') - let username = data.username === null ? "NoName" : data.username; + let username = data.username === null || data.username.length <= 0 ? "NoName" : data.username; messages.insertAdjacentHTML('beforeend', `

${username}: ${data.message}

`) diff --git a/trancendence/asgi.py b/trancendence/asgi.py index 5608a42..a229228 100644 --- a/trancendence/asgi.py +++ b/trancendence/asgi.py @@ -10,7 +10,7 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ import os from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack -import frontend.routing +import chat.routing from django.core.asgi import get_asgi_application @@ -20,7 +20,7 @@ application = ProtocolTypeRouter({ 'http':get_asgi_application(), 'websocket':AuthMiddlewareStack( URLRouter( - frontend.routing.websocket_urlpatterns + chat.routing.websocket_urlpatterns ) ) })