From 0e3b19fcd9d59cc15f67bffd657989806815cf63 Mon Sep 17 00:00:00 2001 From: Xamora Date: Tue, 12 Dec 2023 10:05:13 +0100 Subject: [PATCH] Advance don't merge --- chat/consumers.py | 10 +++++++++- chat/models.py | 14 ++++++++++++++ chat/serializers.py | 8 ++++++++ chat/urls.py | 9 +++++++++ chat/views.py | 30 ++++++++++++++++++++++++++++-- frontend/templates/index.html | 4 ++-- trancendence/settings.py | 3 ++- trancendence/urls.py | 3 ++- 8 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 chat/serializers.py create mode 100644 chat/urls.py diff --git a/chat/consumers.py b/chat/consumers.py index b0ef820..fb7bba5 100644 --- a/chat/consumers.py +++ b/chat/consumers.py @@ -18,17 +18,25 @@ class ChatConsumer(WebsocketConsumer): text_data_json = json.loads(text_data) message = text_data_json['message'] + user = self.scope["user"] + if user.is_anonymous: + return; + async_to_sync(self.channel_layer.group_send)( self.room_group_name, { 'type':'chat_message', - 'username':self.scope["user"].username, + 'username':user.username, 'message':message } ) def chat_message(self, event): + user = self.scope["user"] + if user.is_anonymous: + return; + self.send(text_data=json.dumps({ 'type':'chat', 'username':event['username'], diff --git a/chat/models.py b/chat/models.py index 71a8362..3479bc7 100644 --- a/chat/models.py +++ b/chat/models.py @@ -1,3 +1,17 @@ from django.db import models +from django.db.models import IntegerField +from django.contrib.auth.models import User # Create your models here. +class ChannelModel(models.Model): + pass + +class MemberModel(models.Model): + member_id = IntegerField(primary_key=False) + channel_id = IntegerField(primary_key=False) + +class MessageModel(models.Model): + channel_id = IntegerField(primary_key=False) + author_id = IntegerField(primary_key=False) + content = models.CharField(max_length=255) + time = models.DateField() diff --git a/chat/serializers.py b/chat/serializers.py new file mode 100644 index 0000000..2d62490 --- /dev/null +++ b/chat/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from .models import ChannelModel + +class ChannelSerializer(serializers.ModelSerializer): + + class Meta: + model = ChannelModel + fields = [] diff --git a/chat/urls.py b/chat/urls.py new file mode 100644 index 0000000..f9300cf --- /dev/null +++ b/chat/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from django.conf import settings +from django.conf.urls.static import static + +from . import views + +urlpatterns = [ + path("", views.ChatView.as_view(), name="chat_page"), +] diff --git a/chat/views.py b/chat/views.py index 91ea44a..51dc909 100644 --- a/chat/views.py +++ b/chat/views.py @@ -1,3 +1,29 @@ -from django.shortcuts import render +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import authentication, permissions +from rest_framework.authentication import SessionAuthentication +from .models import ChannelModel, MemberModel, MessageModel -# Create your views here. +class ChatView(APIView): + permission_classes = (permissions.IsAuthenticated,) + authentication_classes = (SessionAuthentication,) + + def post(self, request, pk): + if (ChannelModel.objects.filter(pk = pk)): + return Response("Channel already exist") + + data: dict = request.data + users_id = request.data.get("users_id", []) + if len(users_id) < 2: + return Response("Not enought members to create the channel") + + new_channel = ChannelModel() + new_channel.save() + + for user_id in users_id: + new_member = MemberModel() + new_member.channel_id = new_channel.pk + new_member.member_id = user_id + new_member.save() + + return Response("Channel created") diff --git a/frontend/templates/index.html b/frontend/templates/index.html index b6d4f58..70f227e 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -10,10 +10,10 @@
diff --git a/trancendence/settings.py b/trancendence/settings.py index 4e89778..0ddacfc 100644 --- a/trancendence/settings.py +++ b/trancendence/settings.py @@ -46,6 +46,7 @@ INSTALLED_APPS = [ 'accounts.apps.AccountsConfig', 'profiles.apps.ProfilesConfig', 'frontend.apps.FrontendConfig', + 'chat.apps.ChatConfig', 'corsheaders', 'rest_framework', @@ -148,4 +149,4 @@ STATIC_URL = 'static/' # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' \ No newline at end of file +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/trancendence/urls.py b/trancendence/urls.py index 0f136f1..46a790f 100644 --- a/trancendence/urls.py +++ b/trancendence/urls.py @@ -21,5 +21,6 @@ urlpatterns = [ path('admin/', admin.site.urls), path('api/profiles/', include('profiles.urls')), path('api/accounts/', include('accounts.urls')), + path('api/chat/', include('chat.urls')), path('', include('frontend.urls')), -] \ No newline at end of file +]