Skip to content

Commit c3fc5a9

Browse files
authored
initial commit
0 parents  commit c3fc5a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+499
-0
lines changed

README.md

+17

accounts/__init__.py

Whitespace-only changes.
153 Bytes
Binary file not shown.
194 Bytes
Binary file not shown.
373 Bytes
Binary file not shown.
191 Bytes
Binary file not shown.
303 Bytes
Binary file not shown.
610 Bytes
Binary file not shown.

accounts/admin.py

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

accounts/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountsConfig(AppConfig):
5+
name = 'accounts'

accounts/migrations/__init__.py

Whitespace-only changes.
Binary file not shown.

accounts/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

accounts/tests.py

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

accounts/urls.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.urls import path
2+
3+
from . import views
4+
5+
urlpatterns = [
6+
path('signup/', views.SignUp.as_view(), name='signup')
7+
]

accounts/views.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.shortcuts import render
2+
from django.contrib.auth.forms import UserCreationForm
3+
from django.urls import reverse_lazy
4+
from django.views import generic
5+
6+
# accounts views.py
7+
8+
class SignUp(generic.CreateView):
9+
form_class = UserCreationForm
10+
success_url = reverse_lazy('login')
11+
template_name = 'signup.html'

chat/__init__.py

Whitespace-only changes.
149 Bytes
Binary file not shown.

chat/__pycache__/admin.cpython-37.pyc

190 Bytes
Binary file not shown.

chat/__pycache__/apps.cpython-37.pyc

361 Bytes
Binary file not shown.
1.37 KB
Binary file not shown.
187 Bytes
Binary file not shown.
305 Bytes
Binary file not shown.

chat/__pycache__/urls.cpython-37.pyc

340 Bytes
Binary file not shown.

chat/__pycache__/views.cpython-37.pyc

578 Bytes
Binary file not shown.

chat/admin.py

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

chat/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ChatConfig(AppConfig):
5+
name = 'chat'

chat/consumers.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from asgiref.sync import async_to_sync
2+
from channels.generic.websocket import WebsocketConsumer
3+
import json
4+
5+
class ChatConsumer(WebsocketConsumer):
6+
def connect(self):
7+
self.room_name = self.scope['url_route']['kwargs']['room_name']
8+
self.room_group_name = 'chat_%s' % self.room_name
9+
10+
# Join room group
11+
async_to_sync(self.channel_layer.group_add)(
12+
self.room_group_name,
13+
self.channel_name
14+
)
15+
16+
self.accept()
17+
18+
def disconnect(self, close_code):
19+
# Leave room group
20+
async_to_sync(self.channel_layer.group_discard)(
21+
self.room_group_name,
22+
self.channel_name
23+
)
24+
25+
# Receive message from WebSocket
26+
def receive(self, text_data):
27+
text_data_json = json.loads(text_data)
28+
message = text_data_json['message']
29+
30+
# Send message to room group
31+
async_to_sync(self.channel_layer.group_send)(
32+
self.room_group_name,
33+
{
34+
'type': 'chat_message',
35+
'message': message
36+
}
37+
)
38+
39+
# Receive message from room group
40+
def chat_message(self, event):
41+
message = event['message']
42+
43+
# Send message to WebSocket
44+
self.send(text_data=json.dumps(
45+
{
46+
'message': message
47+
}
48+
))

chat/migrations/__init__.py

Whitespace-only changes.
160 Bytes
Binary file not shown.

chat/models.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.db import models
2+
3+
# Create your models here.
4+
5+
6+
#class User(models.Model):
7+
8+
9+
#class ChatRoom(models.Model):

chat/routing.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.urls import path
2+
from chat import consumers
3+
4+
websocket_urlpatterns = [
5+
path('ws/chat/<str:room_name>/', consumers.ChatConsumer),
6+
]

chat/templates/chat/index.html

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{% extends 'base.html' %}
3+
4+
{% block title %}Chat List{% endblock %}
5+
6+
{% block content %}
7+
<h1>Welcome to the chat room list, {{user.username}}.</h1>
8+
<h2>Pick a room to join:</h2>
9+
{% comment %}
10+
{% for room in room_list %}
11+
<div>
12+
<span>{{room.name}}</span>
13+
<span> | Users: {{ room.users.count }} </span>
14+
<a href="{% url room.name %}">Enter</a>
15+
</div>
16+
{% endfor %}
17+
{% endcomment %}
18+
19+
<h2>Or make a new room:</h2>
20+
{% comment %} <form method="post">
21+
{% csrf_token %}
22+
<input type="text" name="room-name" placeholder="Enter room name.."><br/>
23+
<input type="submit" value="Create Room">
24+
</form> {% endcomment %}
25+
{% endblock %}
26+
27+
{% block scripts %}
28+
<script type="text/javascript">
29+
30+
31+
</script>
32+
{% endblock %}

chat/templates/chat/room.html

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
{% extends 'base.html' %}
3+
4+
{% block title %}Chat Room{% endblock %}
5+
6+
{% block content %}
7+
<h1 id="room-name"></h1>
8+
<textarea id="chat-log" cols="100" rows="20" style="resize: none;"></textarea><br/>
9+
<input id="message-input" type="text" size="100" placeholder="Enter message here..."><br/>
10+
<input id="message-submit" type="button" value="Send">
11+
{% endblock %}
12+
13+
{% block scripts %}
14+
<script type='text/javascript'>
15+
16+
// document nodes used in script
17+
const chatLog = document.querySelector('#chat-log');
18+
const roomNameNode = document.querySelector('#room-name');
19+
20+
var roomNameValue = {{ room_name_json }};
21+
22+
changeNodeText(roomNameNode, roomNameValue);
23+
24+
// helper function
25+
function changeNodeText(node, text) {
26+
node.innerText = text.trim();
27+
}
28+
29+
// websocket
30+
var chatSocket = new WebSocket(
31+
'ws://' + window.location.host +
32+
'/ws/chat/' + roomNameValue + '/'
33+
);
34+
35+
// websocket listeners
36+
chatSocket.onmessage = function(e) {
37+
var data = JSON.parse(e.data);
38+
var message = data['message'];
39+
chatLog.value += (message + '\n');
40+
};
41+
42+
chatSocket.onclose = function(e) {
43+
console.error('Chat socket closed unexpectedly');
44+
};
45+
46+
// events listeners for message-input and message-submit
47+
document.querySelector('#message-input').focus();
48+
document.querySelector('#message-input').onkeyup = function(e) {
49+
// check if enter key is enter/return
50+
if (e.keycode === 13) {
51+
document.querySelector('#message-submit').click();
52+
}
53+
}
54+
document.querySelector('#message-submit').onclick = function(e) {
55+
var messageInput = document.querySelector('#message-input');
56+
var message = messageInput.value;
57+
chatSocket.send(JSON.stringify({
58+
'message': message
59+
}));
60+
61+
messageInput.value = '';
62+
}
63+
</script>
64+
{% endblock %}

chat/tests.py

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

chat/urls.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.urls import path
2+
from chat import views
3+
4+
app_name = 'chat'
5+
6+
urlpatterns = [
7+
path('', views.index, name='index'),
8+
path('<str:room_name>/', views.room, name='room')
9+
]

chat/views.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.shortcuts import render
2+
from django.utils.safestring import mark_safe
3+
import json
4+
5+
# chat views.py
6+
7+
def index(request):
8+
return render(request, 'chat/index.html', {})
9+
10+
def room(request, room_name):
11+
return render(request, 'chat/room.html', {
12+
'room_name_json': mark_safe(json.dumps(room_name))
13+
})

db.sqlite3

128 KB
Binary file not shown.

manage.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == '__main__':
6+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'messagingapp.settings')
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError as exc:
10+
raise ImportError(
11+
"Couldn't import Django. Are you sure it's installed and "
12+
"available on your PYTHONPATH environment variable? Did you "
13+
"forget to activate a virtual environment?"
14+
) from exc
15+
execute_from_command_line(sys.argv)

messagingapp/__init__.py

Whitespace-only changes.
157 Bytes
Binary file not shown.
456 Bytes
Binary file not shown.
Binary file not shown.
1.18 KB
Binary file not shown.
570 Bytes
Binary file not shown.

messagingapp/routing.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django.urls import URLPattern
2+
from channels.routing import ProtocolTypeRouter, URLRouter
3+
from channels.auth import AuthMiddlewareStack
4+
import chat.routing
5+
6+
application = ProtocolTypeRouter({
7+
'websocket' : AuthMiddlewareStack(
8+
URLRouter(
9+
chat.routing.websocket_urlpatterns
10+
)
11+
)
12+
})

0 commit comments

Comments
 (0)