From 0fc403051ee00c7b465bd82d56978197cfdd90a1 Mon Sep 17 00:00:00 2001 From: RitiKSiBJr Date: Thu, 15 Dec 2022 11:55:15 +0545 Subject: [PATCH 1/5] django projects --- DJANGO PROJECTS/Chat/README.md | 6 + DJANGO PROJECTS/Chat/chat/__init__.py | 0 DJANGO PROJECTS/Chat/chat/admin.py | 3 + DJANGO PROJECTS/Chat/chat/apps.py | 6 + DJANGO PROJECTS/Chat/chat/forms.py | 23 +++ .../Chat/chat/migrations/__init__.py | 0 DJANGO PROJECTS/Chat/chat/models.py | 3 + .../Chat/chat/templates/index.html | 30 ++++ .../chat/templates/registration/login.html | 16 ++ .../Chat/chat/templates/signin.html | 14 ++ DJANGO PROJECTS/Chat/chat/templates/user.html | 13 ++ DJANGO PROJECTS/Chat/chat/tests.py | 3 + DJANGO PROJECTS/Chat/chat/urls.py | 11 ++ DJANGO PROJECTS/Chat/chat/views.py | 28 ++++ DJANGO PROJECTS/Chat/manage.py | 22 +++ DJANGO PROJECTS/Chat/project/__init__.py | 0 DJANGO PROJECTS/Chat/project/asgi.py | 30 ++++ DJANGO PROJECTS/Chat/project/settings.py | 145 ++++++++++++++++++ .../Chat/project/static/css/styles.css | 60 ++++++++ .../Chat/project/templates/base.html | 61 ++++++++ DJANGO PROJECTS/Chat/project/urls.py | 23 +++ DJANGO PROJECTS/Chat/project/wsgi.py | 16 ++ DJANGO PROJECTS/Chat/room/__init__.py | 0 DJANGO PROJECTS/Chat/room/admin.py | 5 + DJANGO PROJECTS/Chat/room/apps.py | 6 + DJANGO PROJECTS/Chat/room/consumers.py | 31 ++++ DJANGO PROJECTS/Chat/room/forms.py | 7 + .../Chat/room/migrations/0001_initial.py | 22 +++ .../Chat/room/migrations/__init__.py | 0 DJANGO PROJECTS/Chat/room/models.py | 9 ++ DJANGO PROJECTS/Chat/room/routing.py | 6 + .../Chat/room/templates/create_room.html | 14 ++ .../Chat/room/templates/room_detail.html | 69 +++++++++ .../Chat/room/templates/rooms.html | 17 ++ DJANGO PROJECTS/Chat/room/tests.py | 3 + DJANGO PROJECTS/Chat/room/urls.py | 8 + DJANGO PROJECTS/Chat/room/views.py | 26 ++++ 37 files changed, 736 insertions(+) create mode 100644 DJANGO PROJECTS/Chat/README.md create mode 100644 DJANGO PROJECTS/Chat/chat/__init__.py create mode 100644 DJANGO PROJECTS/Chat/chat/admin.py create mode 100644 DJANGO PROJECTS/Chat/chat/apps.py create mode 100644 DJANGO PROJECTS/Chat/chat/forms.py create mode 100644 DJANGO PROJECTS/Chat/chat/migrations/__init__.py create mode 100644 DJANGO PROJECTS/Chat/chat/models.py create mode 100644 DJANGO PROJECTS/Chat/chat/templates/index.html create mode 100644 DJANGO PROJECTS/Chat/chat/templates/registration/login.html create mode 100644 DJANGO PROJECTS/Chat/chat/templates/signin.html create mode 100644 DJANGO PROJECTS/Chat/chat/templates/user.html create mode 100644 DJANGO PROJECTS/Chat/chat/tests.py create mode 100644 DJANGO PROJECTS/Chat/chat/urls.py create mode 100644 DJANGO PROJECTS/Chat/chat/views.py create mode 100644 DJANGO PROJECTS/Chat/manage.py create mode 100644 DJANGO PROJECTS/Chat/project/__init__.py create mode 100644 DJANGO PROJECTS/Chat/project/asgi.py create mode 100644 DJANGO PROJECTS/Chat/project/settings.py create mode 100644 DJANGO PROJECTS/Chat/project/static/css/styles.css create mode 100644 DJANGO PROJECTS/Chat/project/templates/base.html create mode 100644 DJANGO PROJECTS/Chat/project/urls.py create mode 100644 DJANGO PROJECTS/Chat/project/wsgi.py create mode 100644 DJANGO PROJECTS/Chat/room/__init__.py create mode 100644 DJANGO PROJECTS/Chat/room/admin.py create mode 100644 DJANGO PROJECTS/Chat/room/apps.py create mode 100644 DJANGO PROJECTS/Chat/room/consumers.py create mode 100644 DJANGO PROJECTS/Chat/room/forms.py create mode 100644 DJANGO PROJECTS/Chat/room/migrations/0001_initial.py create mode 100644 DJANGO PROJECTS/Chat/room/migrations/__init__.py create mode 100644 DJANGO PROJECTS/Chat/room/models.py create mode 100644 DJANGO PROJECTS/Chat/room/routing.py create mode 100644 DJANGO PROJECTS/Chat/room/templates/create_room.html create mode 100644 DJANGO PROJECTS/Chat/room/templates/room_detail.html create mode 100644 DJANGO PROJECTS/Chat/room/templates/rooms.html create mode 100644 DJANGO PROJECTS/Chat/room/tests.py create mode 100644 DJANGO PROJECTS/Chat/room/urls.py create mode 100644 DJANGO PROJECTS/Chat/room/views.py diff --git a/DJANGO PROJECTS/Chat/README.md b/DJANGO PROJECTS/Chat/README.md new file mode 100644 index 00000000..2e65cf75 --- /dev/null +++ b/DJANGO PROJECTS/Chat/README.md @@ -0,0 +1,6 @@ +# Django Chat System + +## Chat system build with Django and Channels + +Django == 4.1.4 +channels == 3.0.4 \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/chat/__init__.py b/DJANGO PROJECTS/Chat/chat/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/DJANGO PROJECTS/Chat/chat/admin.py b/DJANGO PROJECTS/Chat/chat/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/DJANGO PROJECTS/Chat/chat/apps.py b/DJANGO PROJECTS/Chat/chat/apps.py new file mode 100644 index 00000000..2fe899ad --- /dev/null +++ b/DJANGO PROJECTS/Chat/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/DJANGO PROJECTS/Chat/chat/forms.py b/DJANGO PROJECTS/Chat/chat/forms.py new file mode 100644 index 00000000..01446105 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/forms.py @@ -0,0 +1,23 @@ +from django import forms +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User + +class NewUserForm(UserCreationForm): + email = forms.EmailField(required=True) + + class Meta: + model = User + fields = ('username', 'email', 'password1', 'password2') + + def save(self, commit=True): + user = super(NewUserForm,self).save(commit=False) + user.email = self.cleaned_data['email'] + if commit: + user.save() + return user + + def __init__(self, *args, **kwargs): + super(UserCreationForm, self).__init__(*args, **kwargs) + self.fields['username'].help_text = None + self.fields['password2'].help_text = None + self.fields['password1'].help_text = None diff --git a/DJANGO PROJECTS/Chat/chat/migrations/__init__.py b/DJANGO PROJECTS/Chat/chat/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/DJANGO PROJECTS/Chat/chat/models.py b/DJANGO PROJECTS/Chat/chat/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/DJANGO PROJECTS/Chat/chat/templates/index.html b/DJANGO PROJECTS/Chat/chat/templates/index.html new file mode 100644 index 00000000..d22e2c43 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/templates/index.html @@ -0,0 +1,30 @@ +{% extends 'base.html' %} +{% block title %}Home{% endblock %} + +{% block content %} + +
+ {% if user.is_authenticated %} +
+

User is authenticated

+

This is a chat system app build in Python-Django.
+ +

    + Featues: +
  • Sign up to chat.
  • +
  • Log in and log out from the app.
  • +
  • Send messages and read messages.
  • +
  • Create new rooms.
  • +

+
+ {% else %} +
+

User is not authenticated

+

Make sure or logged in else you can sign up.

+ Follow this link to login or check the top right corner. + Follow this link to sign up or check the top right corner. +
+ {% endif %} +
+ +{% endblock %} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/chat/templates/registration/login.html b/DJANGO PROJECTS/Chat/chat/templates/registration/login.html new file mode 100644 index 00000000..707a5bf6 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/templates/registration/login.html @@ -0,0 +1,16 @@ +{% extends 'base.html' %} +{% block title %}LogIn{% endblock %} + +{% block content %} + +
+

Log In

+ +
+ {% csrf_token %} + {{ form.as_p }} + +
+
+ +{% endblock %} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/chat/templates/signin.html b/DJANGO PROJECTS/Chat/chat/templates/signin.html new file mode 100644 index 00000000..54e1c377 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/templates/signin.html @@ -0,0 +1,14 @@ +{% extends 'base.html' %} + +{% block title %}Sign In{% endblock %} + +{% block content %} +
+

Register

+
+ {% csrf_token %} + {{ form.as_p }} + +
+
+{% endblock %} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/chat/templates/user.html b/DJANGO PROJECTS/Chat/chat/templates/user.html new file mode 100644 index 00000000..4b8dfae6 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/templates/user.html @@ -0,0 +1,13 @@ +{% extends 'base.html' %} + +{% block title %}User Detail{% endblock %} + +{% block content %} +
+

User Detail

+
Username : {{ user.username }}
+
Email : {{ user.email }}
+ +
+ +{% endblock %} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/chat/tests.py b/DJANGO PROJECTS/Chat/chat/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/DJANGO PROJECTS/Chat/chat/urls.py b/DJANGO PROJECTS/Chat/chat/urls.py new file mode 100644 index 00000000..3d0eaeaf --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/urls.py @@ -0,0 +1,11 @@ +from django.urls import path +from . import views +from django.contrib.auth.views import LoginView, LogoutView + +urlpatterns = [ + path('',views.index, name="index"), + path('accounts/login/',LoginView.as_view(), name="login"), + path('accounts/logout/',LogoutView.as_view(), name='logout'), + path('accounts/register/',views.register, name='register'), + path('user/',views.user, name="user") +] \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/chat/views.py b/DJANGO PROJECTS/Chat/chat/views.py new file mode 100644 index 00000000..45112249 --- /dev/null +++ b/DJANGO PROJECTS/Chat/chat/views.py @@ -0,0 +1,28 @@ +from django.shortcuts import render, redirect +from .forms import NewUserForm +from django.contrib.auth import login +from django.contrib import messages + +# Create your views here. +def index(request): + return render(request, 'index.html') + +def register(request): + if request.method == "POST": + form = NewUserForm(request.POST) + if form.is_valid(): + user = form.save() + login(request, user) + messages.success(request, 'Sign In successfull!') + return redirect('index') + messages.error(request, 'Invalid Information, Please try again!') + form = NewUserForm + return render(request, 'signin.html', context={'form':form}) + +def user(request): + user = request.user + context = { + 'user' : user + } + + return render(request, 'user.html', context) \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/manage.py b/DJANGO PROJECTS/Chat/manage.py new file mode 100644 index 00000000..2c49f3ac --- /dev/null +++ b/DJANGO PROJECTS/Chat/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/DJANGO PROJECTS/Chat/project/__init__.py b/DJANGO PROJECTS/Chat/project/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/DJANGO PROJECTS/Chat/project/asgi.py b/DJANGO PROJECTS/Chat/project/asgi.py new file mode 100644 index 00000000..f8c5bc63 --- /dev/null +++ b/DJANGO PROJECTS/Chat/project/asgi.py @@ -0,0 +1,30 @@ +""" +ASGI config for project project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application +from channels.routing import ProtocolTypeRouter, URLRouter +from channels.security.websocket import AllowedHostsOriginValidator +from channels.auth import AuthMiddlewareStack + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings') + +import room.routing + +application = ProtocolTypeRouter( + { + "http":get_asgi_application(), + "websocket": AllowedHostsOriginValidator( + AuthMiddlewareStack(URLRouter(room.routing.websocket_urlpatterns)) + ) + } +) + + diff --git a/DJANGO PROJECTS/Chat/project/settings.py b/DJANGO PROJECTS/Chat/project/settings.py new file mode 100644 index 00000000..31a3cb78 --- /dev/null +++ b/DJANGO PROJECTS/Chat/project/settings.py @@ -0,0 +1,145 @@ +""" +Django settings for project project. + +Generated by 'django-admin startproject' using Django 4.1.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +from environ import Env + +env = Env() +env.read_env() +SECRET_KEY = 'MY_SECRET_KEY' + + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + + 'channels', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + 'chat', + 'room', +] + +ASGI_APPLICATION = 'project.asgi.application' + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'project.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': ['project/templates'], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + + + + + +# Database +# https://docs.djangoproject.com/en/4.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.1/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +LOGIN_REDIRECT_URL = '/' +LOGOUT_REDIRECT_URL = 'login' + +CHANNEL_LAYERS = { + 'default':{ + 'BACKEND':'channels.layers.InMemoryChannelLayer' + } +} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/project/static/css/styles.css b/DJANGO PROJECTS/Chat/project/static/css/styles.css new file mode 100644 index 00000000..717faa25 --- /dev/null +++ b/DJANGO PROJECTS/Chat/project/static/css/styles.css @@ -0,0 +1,60 @@ +.in{ + border-radius: 30px; +} +.gap-in{ + gap: 10px; +} +a{ + text-decoration: none; +} +.card{ + background-color: #5DADE2; + padding: 20px; + text-align: center; + width: 30%; + border-radius: 20px; + margin-top: 5vh; +} +.new-btn{ + width: 50%; + margin: 0 auto; +} +.user-card{ + margin-top: 20px; + height: 60vh; + gap: 10px; +} +.head{ + margin: 2vh; + text-align: center; +} +.fixed-card{ + width: 100%; + min-height: 40vh; + margin-bottom: 1em; +} +.mini-card{ + width: 100%; + background-color: #AED6F1; + padding: 0; + padding-left: 10px; + text-align: left; + margin: 0; + margin-bottom: 3px; + +} +.msg{ + width: 80%; + border-radius: 30px; + background-color: #AED6F1; + border-style: none; +} +.register-card{ + text-align: left; + width: 40%; +} +.create{ + position: fixed; + bottom: 5px; + left: 5px; +} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/project/templates/base.html b/DJANGO PROJECTS/Chat/project/templates/base.html new file mode 100644 index 00000000..dadd2673 --- /dev/null +++ b/DJANGO PROJECTS/Chat/project/templates/base.html @@ -0,0 +1,61 @@ + + + + + {% load static %} + + + ChatRoom-{% block title%}{% endblock %} + + + + + + + + {% block content %} + + {% endblock %} + + + + + \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/project/urls.py b/DJANGO PROJECTS/Chat/project/urls.py new file mode 100644 index 00000000..ab3f7d33 --- /dev/null +++ b/DJANGO PROJECTS/Chat/project/urls.py @@ -0,0 +1,23 @@ +"""project URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('',include('chat.urls')), + path('rooms/',include('room.urls')) +] diff --git a/DJANGO PROJECTS/Chat/project/wsgi.py b/DJANGO PROJECTS/Chat/project/wsgi.py new file mode 100644 index 00000000..67fa01cc --- /dev/null +++ b/DJANGO PROJECTS/Chat/project/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for project project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings') + +application = get_wsgi_application() diff --git a/DJANGO PROJECTS/Chat/room/__init__.py b/DJANGO PROJECTS/Chat/room/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/DJANGO PROJECTS/Chat/room/admin.py b/DJANGO PROJECTS/Chat/room/admin.py new file mode 100644 index 00000000..ee27c348 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Room + +# Register your models here. +admin.site.register(Room) diff --git a/DJANGO PROJECTS/Chat/room/apps.py b/DJANGO PROJECTS/Chat/room/apps.py new file mode 100644 index 00000000..38c33e75 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class RoomConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'room' diff --git a/DJANGO PROJECTS/Chat/room/consumers.py b/DJANGO PROJECTS/Chat/room/consumers.py new file mode 100644 index 00000000..7c7f9099 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/consumers.py @@ -0,0 +1,31 @@ +import json + +from channels.generic.websocket import AsyncWebsocketConsumer + + +class ChatConsumer(AsyncWebsocketConsumer): + async def connect(self): + self.room_name = self.scope["url_route"]["kwargs"]["room_name"] + self.room_group_name = "chat_%s" % self.room_name + + await self.channel_layer.group_add(self.room_group_name, self.channel_name) + + await self.accept() + + async def disconnect(self, close_code): + await self.channel_layer.group_discard(self.room_group_name, self.channel_name) + + async def receive(self, text_data): + text_data_json = json.loads(text_data) + message = text_data_json["message"] + username = text_data_json["username"] + + await self.channel_layer.group_send( + self.room_group_name, {"type": "chat_message", "message": message, "username":username} + ) + + async def chat_message(self, event): + message = event["message"] + username = event["username"] + + await self.send(text_data=json.dumps({"message": message, "username": username})) \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/room/forms.py b/DJANGO PROJECTS/Chat/room/forms.py new file mode 100644 index 00000000..376a0e9b --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/forms.py @@ -0,0 +1,7 @@ +from .models import Room +from django.forms import ModelForm + +class RoomForm(ModelForm): + class Meta: + model = Room + fields = ['name', 'slug'] diff --git a/DJANGO PROJECTS/Chat/room/migrations/0001_initial.py b/DJANGO PROJECTS/Chat/room/migrations/0001_initial.py new file mode 100644 index 00000000..920198d7 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 4.1.3 on 2022-11-24 08:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Room', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=200)), + ('slug', models.SlugField(max_length=40)), + ], + ), + ] diff --git a/DJANGO PROJECTS/Chat/room/migrations/__init__.py b/DJANGO PROJECTS/Chat/room/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/DJANGO PROJECTS/Chat/room/models.py b/DJANGO PROJECTS/Chat/room/models.py new file mode 100644 index 00000000..5ee11067 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/models.py @@ -0,0 +1,9 @@ +from django.db import models + +# Create your models here. +class Room(models.Model): + name = models.CharField(max_length=200) + slug = models.SlugField(max_length=40) + + def __str__(self): + return self.name \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/room/routing.py b/DJANGO PROJECTS/Chat/room/routing.py new file mode 100644 index 00000000..61660439 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/routing.py @@ -0,0 +1,6 @@ +from django.urls import re_path +from room.consumers import ChatConsumer + +websocket_urlpatterns = [ + re_path(r"ws/rooms/(?P\w+)/$", ChatConsumer.as_asgi()), +] \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/room/templates/create_room.html b/DJANGO PROJECTS/Chat/room/templates/create_room.html new file mode 100644 index 00000000..fdfa6ee5 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/templates/create_room.html @@ -0,0 +1,14 @@ +{% extends 'base.html' %} + +{% block title %}Create Room{% endblock %} + +{% block content %} +

Create Room

+
+
+ {% csrf_token %} + {{ form.as_p }} + +
+
+{% endblock %} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/room/templates/room_detail.html b/DJANGO PROJECTS/Chat/room/templates/room_detail.html new file mode 100644 index 00000000..098f6585 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/templates/room_detail.html @@ -0,0 +1,69 @@ +{% extends 'base.html' %} + +{% block title %}{{room.name}}{% endblock %} + +{% block content %} + +
+

{{room.name}}

+

{{request.user.username|json_script:"json-username"}}

+ +
+
+ +
+
+ + +
+
+
+ {% block script %} + {{request.user.username|json_script:"json-username"}} + + {% endblock %} + +{% endblock %} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/room/templates/rooms.html b/DJANGO PROJECTS/Chat/room/templates/rooms.html new file mode 100644 index 00000000..e59a01f2 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/templates/rooms.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} +{% block title %}Rooms{% endblock %} + +{% block content %} +

Rooms

+
+ {% for room in rooms %} +
+

{{room.name}}

+ +
+ {% endfor %} +
+ + + +{% endblock %} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/room/tests.py b/DJANGO PROJECTS/Chat/room/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/DJANGO PROJECTS/Chat/room/urls.py b/DJANGO PROJECTS/Chat/room/urls.py new file mode 100644 index 00000000..99930225 --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('',views.roomview, name="room"), + path('', views.room_detail,name="roomdetail"), + path('room/create',views.create, name="create") +] \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/room/views.py b/DJANGO PROJECTS/Chat/room/views.py new file mode 100644 index 00000000..dfda150e --- /dev/null +++ b/DJANGO PROJECTS/Chat/room/views.py @@ -0,0 +1,26 @@ +from django.shortcuts import render, redirect +from .models import Room +from .forms import RoomForm +from django.contrib.auth.decorators import login_required + +# Create your views here. +@login_required +def roomview(request): + rooms = Room.objects.all() + return render(request, 'rooms.html', context={'rooms':rooms}) + +def room_detail(request, slug): + room = Room.objects.get(slug=slug) + return render(request, 'room_detail.html', context={'room':room}) + +def create(request): + if request.method == "POST": + form = RoomForm(request.POST) + + if form.is_valid(): + form.save() + return redirect(roomview) + + else: + form = RoomForm + return render(request, 'create_room.html', {'form':form}) \ No newline at end of file From 1edb5d5fc6ba4a8ab17fde653a815eeab2fa6f34 Mon Sep 17 00:00:00 2001 From: RitiKSiBJr Date: Fri, 30 Dec 2022 08:17:29 +0545 Subject: [PATCH 2/5] added readme --- DJANGO PROJECTS/Chat/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DJANGO PROJECTS/Chat/README.md b/DJANGO PROJECTS/Chat/README.md index 2e65cf75..4522bfa1 100644 --- a/DJANGO PROJECTS/Chat/README.md +++ b/DJANGO PROJECTS/Chat/README.md @@ -2,5 +2,6 @@ ## Chat system build with Django and Channels -Django == 4.1.4 -channels == 3.0.4 \ No newline at end of file +This a simple web application where you can chat in real time. User can send the message to other users available inside the room and read the other's messages. + +You can test it creating two accounts and opening one in browser and other in private browser. Then you will be able to see the messages . \ No newline at end of file From 71f4cdc4b92ae1aa536a18779955f4d8cbd2296e Mon Sep 17 00:00:00 2001 From: RitiKSiBJr Date: Mon, 2 Jan 2023 13:28:05 +0545 Subject: [PATCH 3/5] updated readme --- DJANGO PROJECTS/Chat/README.md | 16 ++++- .../Chat/project/static/css/styles.css | 60 ----------------- .../Chat/project/templates/base.html | 63 +++++++++++++++++- DJANGO PROJECTS/Chat/requirements.txt | 3 + DJANGO PROJECTS/Chat/static/images/pic.png | Bin 0 -> 21175 bytes 5 files changed, 78 insertions(+), 64 deletions(-) delete mode 100644 DJANGO PROJECTS/Chat/project/static/css/styles.css create mode 100644 DJANGO PROJECTS/Chat/requirements.txt create mode 100644 DJANGO PROJECTS/Chat/static/images/pic.png diff --git a/DJANGO PROJECTS/Chat/README.md b/DJANGO PROJECTS/Chat/README.md index 4522bfa1..fb6c24e5 100644 --- a/DJANGO PROJECTS/Chat/README.md +++ b/DJANGO PROJECTS/Chat/README.md @@ -1,7 +1,17 @@ -# Django Chat System +# Django Chat Application -## Chat system build with Django and Channels +## Overview This a simple web application where you can chat in real time. User can send the message to other users available inside the room and read the other's messages. -You can test it creating two accounts and opening one in browser and other in private browser. Then you will be able to see the messages . \ No newline at end of file +You can test it creating two accounts and opening one in browser and other in private browser. Then you will be able to see the messages . + +## Libraries and Frameworks Used: +1.Django == 3.0.2 +2.django-environ == 4.1.4 +3.channels == 3.0.4 + +## Screenshot +![Screenshot](static/images/pic.png) + + diff --git a/DJANGO PROJECTS/Chat/project/static/css/styles.css b/DJANGO PROJECTS/Chat/project/static/css/styles.css deleted file mode 100644 index 717faa25..00000000 --- a/DJANGO PROJECTS/Chat/project/static/css/styles.css +++ /dev/null @@ -1,60 +0,0 @@ -.in{ - border-radius: 30px; -} -.gap-in{ - gap: 10px; -} -a{ - text-decoration: none; -} -.card{ - background-color: #5DADE2; - padding: 20px; - text-align: center; - width: 30%; - border-radius: 20px; - margin-top: 5vh; -} -.new-btn{ - width: 50%; - margin: 0 auto; -} -.user-card{ - margin-top: 20px; - height: 60vh; - gap: 10px; -} -.head{ - margin: 2vh; - text-align: center; -} -.fixed-card{ - width: 100%; - min-height: 40vh; - margin-bottom: 1em; -} -.mini-card{ - width: 100%; - background-color: #AED6F1; - padding: 0; - padding-left: 10px; - text-align: left; - margin: 0; - margin-bottom: 3px; - -} -.msg{ - width: 80%; - border-radius: 30px; - background-color: #AED6F1; - border-style: none; -} -.register-card{ - text-align: left; - width: 40%; -} -.create{ - position: fixed; - bottom: 5px; - left: 5px; -} \ No newline at end of file diff --git a/DJANGO PROJECTS/Chat/project/templates/base.html b/DJANGO PROJECTS/Chat/project/templates/base.html index dadd2673..a8471e1c 100644 --- a/DJANGO PROJECTS/Chat/project/templates/base.html +++ b/DJANGO PROJECTS/Chat/project/templates/base.html @@ -10,7 +10,68 @@ - +