Skip to content

Commit 0fc4030

Browse files
committed
django projects
1 parent 86b7d6e commit 0fc4030

37 files changed

+736
-0
lines changed

DJANGO PROJECTS/Chat/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Django Chat System
2+
3+
## Chat system build with Django and Channels
4+
5+
Django == 4.1.4
6+
channels == 3.0.4

DJANGO PROJECTS/Chat/chat/__init__.py

Whitespace-only changes.

DJANGO PROJECTS/Chat/chat/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

DJANGO PROJECTS/Chat/chat/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ChatConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'chat'

DJANGO PROJECTS/Chat/chat/forms.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django import forms
2+
from django.contrib.auth.forms import UserCreationForm
3+
from django.contrib.auth.models import User
4+
5+
class NewUserForm(UserCreationForm):
6+
email = forms.EmailField(required=True)
7+
8+
class Meta:
9+
model = User
10+
fields = ('username', 'email', 'password1', 'password2')
11+
12+
def save(self, commit=True):
13+
user = super(NewUserForm,self).save(commit=False)
14+
user.email = self.cleaned_data['email']
15+
if commit:
16+
user.save()
17+
return user
18+
19+
def __init__(self, *args, **kwargs):
20+
super(UserCreationForm, self).__init__(*args, **kwargs)
21+
self.fields['username'].help_text = None
22+
self.fields['password2'].help_text = None
23+
self.fields['password1'].help_text = None

DJANGO PROJECTS/Chat/chat/migrations/__init__.py

Whitespace-only changes.

DJANGO PROJECTS/Chat/chat/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{% extends 'base.html' %}
2+
{% block title %}Home{% endblock %}
3+
4+
{% block content %}
5+
6+
<div class="container">
7+
{% if user.is_authenticated %}
8+
<div class="container fixed-card card">
9+
<h1>User is authenticated</h1>
10+
<p>This is a <strong>chat system app</strong> build in <strong>Python-Django</strong>.<br>
11+
12+
<ul style="text-align: left;">
13+
Featues:
14+
<li>Sign up to chat.</li>
15+
<li>Log in and log out from the app.</li>
16+
<li>Send messages and read messages.</li>
17+
<li>Create new rooms.</li>
18+
</ul></p>
19+
</div>
20+
{% else %}
21+
<div class="container fixed-card card">
22+
<h1>User is not authenticated</h1>
23+
<p>Make sure or <strong>logged in</strong> else you can <strong>sign up</strong>.</p>
24+
<a href="{% url 'login' %}" style="text-decoration: underline;">Follow this link to login or check the top right corner.</a>
25+
<a href="{% url 'register' %}" style="text-decoration: underline;">Follow this link to sign up or check the top right corner.</a>
26+
</div>
27+
{% endif %}
28+
</div>
29+
30+
{% endblock %}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% extends 'base.html' %}
2+
{% block title %}LogIn{% endblock %}
3+
4+
{% block content %}
5+
6+
<div class="container card">
7+
<h1 class="head">Log In</h1>
8+
9+
<form method="post">
10+
{% csrf_token %}
11+
{{ form.as_p }}
12+
<input type="submit" value="LogIn" class="btn btn-dark in">
13+
</form>
14+
</div>
15+
16+
{% endblock %}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends 'base.html' %}
2+
3+
{% block title %}Sign In{% endblock %}
4+
5+
{% block content %}
6+
<div class="container card register-card">
7+
<h1>Register</h1>
8+
<form method="post">
9+
{% csrf_token %}
10+
{{ form.as_p }}
11+
<input type="submit" value="Sign In" class="btn btn-dark in">
12+
</form>
13+
</div>
14+
{% endblock %}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends 'base.html' %}
2+
3+
{% block title %}User Detail{% endblock %}
4+
5+
{% block content %}
6+
<div class="container user-card card">
7+
<h1>User Detail</h1>
8+
<h5>Username : {{ user.username }}</h5>
9+
<h5>Email : {{ user.email }}</h5>
10+
<a href="{% url 'logout' %}"><input type="submit" value="Log Out" class="btn btn-dark in new-btn"></a>
11+
</div>
12+
13+
{% endblock %}

DJANGO PROJECTS/Chat/chat/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

DJANGO PROJECTS/Chat/chat/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.urls import path
2+
from . import views
3+
from django.contrib.auth.views import LoginView, LogoutView
4+
5+
urlpatterns = [
6+
path('',views.index, name="index"),
7+
path('accounts/login/',LoginView.as_view(), name="login"),
8+
path('accounts/logout/',LogoutView.as_view(), name='logout'),
9+
path('accounts/register/',views.register, name='register'),
10+
path('user/',views.user, name="user")
11+
]

DJANGO PROJECTS/Chat/chat/views.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.shortcuts import render, redirect
2+
from .forms import NewUserForm
3+
from django.contrib.auth import login
4+
from django.contrib import messages
5+
6+
# Create your views here.
7+
def index(request):
8+
return render(request, 'index.html')
9+
10+
def register(request):
11+
if request.method == "POST":
12+
form = NewUserForm(request.POST)
13+
if form.is_valid():
14+
user = form.save()
15+
login(request, user)
16+
messages.success(request, 'Sign In successfull!')
17+
return redirect('index')
18+
messages.error(request, 'Invalid Information, Please try again!')
19+
form = NewUserForm
20+
return render(request, 'signin.html', context={'form':form})
21+
22+
def user(request):
23+
user = request.user
24+
context = {
25+
'user' : user
26+
}
27+
28+
return render(request, 'user.html', context)

DJANGO PROJECTS/Chat/manage.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

DJANGO PROJECTS/Chat/project/__init__.py

Whitespace-only changes.

DJANGO PROJECTS/Chat/project/asgi.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
ASGI config for project project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
from channels.routing import ProtocolTypeRouter, URLRouter
14+
from channels.security.websocket import AllowedHostsOriginValidator
15+
from channels.auth import AuthMiddlewareStack
16+
17+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
18+
19+
import room.routing
20+
21+
application = ProtocolTypeRouter(
22+
{
23+
"http":get_asgi_application(),
24+
"websocket": AllowedHostsOriginValidator(
25+
AuthMiddlewareStack(URLRouter(room.routing.websocket_urlpatterns))
26+
)
27+
}
28+
)
29+
30+
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
"""
2+
Django settings for project project.
3+
4+
Generated by 'django-admin startproject' using Django 4.1.3.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.1/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/4.1/ref/settings/
11+
"""
12+
13+
from pathlib import Path
14+
15+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
16+
BASE_DIR = Path(__file__).resolve().parent.parent
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
from environ import Env
24+
25+
env = Env()
26+
env.read_env()
27+
SECRET_KEY = 'MY_SECRET_KEY'
28+
29+
30+
# SECURITY WARNING: don't run with debug turned on in production!
31+
DEBUG = True
32+
33+
ALLOWED_HOSTS = []
34+
35+
36+
# Application definition
37+
38+
INSTALLED_APPS = [
39+
40+
'channels',
41+
'django.contrib.admin',
42+
'django.contrib.auth',
43+
'django.contrib.contenttypes',
44+
'django.contrib.sessions',
45+
'django.contrib.messages',
46+
'django.contrib.staticfiles',
47+
48+
'chat',
49+
'room',
50+
]
51+
52+
ASGI_APPLICATION = 'project.asgi.application'
53+
54+
MIDDLEWARE = [
55+
'django.middleware.security.SecurityMiddleware',
56+
'django.contrib.sessions.middleware.SessionMiddleware',
57+
'django.middleware.common.CommonMiddleware',
58+
'django.middleware.csrf.CsrfViewMiddleware',
59+
'django.contrib.auth.middleware.AuthenticationMiddleware',
60+
'django.contrib.messages.middleware.MessageMiddleware',
61+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
62+
]
63+
64+
ROOT_URLCONF = 'project.urls'
65+
66+
TEMPLATES = [
67+
{
68+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
69+
'DIRS': ['project/templates'],
70+
'APP_DIRS': True,
71+
'OPTIONS': {
72+
'context_processors': [
73+
'django.template.context_processors.debug',
74+
'django.template.context_processors.request',
75+
'django.contrib.auth.context_processors.auth',
76+
'django.contrib.messages.context_processors.messages',
77+
],
78+
},
79+
},
80+
]
81+
82+
83+
84+
85+
86+
# Database
87+
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
88+
89+
DATABASES = {
90+
'default': {
91+
'ENGINE': 'django.db.backends.sqlite3',
92+
'NAME': BASE_DIR / 'db.sqlite3',
93+
}
94+
}
95+
96+
97+
# Password validation
98+
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
99+
100+
AUTH_PASSWORD_VALIDATORS = [
101+
{
102+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
103+
},
104+
{
105+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
106+
},
107+
{
108+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
109+
},
110+
{
111+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
112+
},
113+
]
114+
115+
116+
# Internationalization
117+
# https://docs.djangoproject.com/en/4.1/topics/i18n/
118+
119+
LANGUAGE_CODE = 'en-us'
120+
121+
TIME_ZONE = 'UTC'
122+
123+
USE_I18N = True
124+
125+
USE_TZ = True
126+
127+
128+
# Static files (CSS, JavaScript, Images)
129+
# https://docs.djangoproject.com/en/4.1/howto/static-files/
130+
131+
STATIC_URL = 'static/'
132+
133+
# Default primary key field type
134+
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
135+
136+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
137+
138+
LOGIN_REDIRECT_URL = '/'
139+
LOGOUT_REDIRECT_URL = 'login'
140+
141+
CHANNEL_LAYERS = {
142+
'default':{
143+
'BACKEND':'channels.layers.InMemoryChannelLayer'
144+
}
145+
}

0 commit comments

Comments
 (0)