Advance don't merge

This commit is contained in:
Xamora 2023-12-12 10:05:13 +01:00
parent bc892bc157
commit 0e3b19fcd9
8 changed files with 74 additions and 7 deletions

View File

@ -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'],

View File

@ -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()

8
chat/serializers.py Normal file
View File

@ -0,0 +1,8 @@
from rest_framework import serializers
from .models import ChannelModel
class ChannelSerializer(serializers.ModelSerializer):
class Meta:
model = ChannelModel
fields = []

9
chat/urls.py Normal file
View File

@ -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("<int:pk>", views.ChatView.as_view(), name="chat_page"),
]

View File

@ -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")

View File

@ -10,10 +10,10 @@
<body>
<nav class="nav">
<a href="/" class="nav__link" data-link>Dashboard</a>
<a href="/profiles" class="nav__link" data-link>Profiles</a>
<a href="/search" class="nav__link" data-link>Search</a>
<a href="/home" class="nav__link" data-link>Home</a>
<a href="/login" class="nav__link" data-link>Login</a>
<a href="/register" class="nav__link" data-link>Register</a>
<a href="/search" class="nav__link" data-link>Search</a>
</nav>
<div id="app"></div>
<script type="module" src="{% static 'js/index.js' %}"></script>

View File

@ -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'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

View File

@ -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')),
]
]