Skip to content

Commit 3971d8d

Browse files
authored
update-channels update django channels config (#219)
* update-channels update django channels config * update-channels smoke tests with django channels * update-channels install websocat
1 parent 8be8ada commit 3971d8d

File tree

10 files changed

+227
-221
lines changed

10 files changed

+227
-221
lines changed

Diff for: .github/workflows/ci.yml

+5-20
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,6 @@ jobs:
8585
name: DB.io - CLI Tests
8686
needs: [build]
8787
runs-on: ubuntu-24.04
88-
steps:
89-
- run: sudo apt-get install -y libsqlite3-dev
90-
- uses: actions/checkout@v4
91-
- uses: actions/setup-python@v5
92-
with:
93-
python-version: '3.13'
94-
- name: Install uv
95-
uses: astral-sh/setup-uv@v4
96-
with:
97-
# Install a specific version of uv.
98-
version: "0.5.6"
99-
- run: yarn
100-
- run: make smoke_test
101-
102-
cli_tests_postgres:
103-
name: DB.io - CLI Tests for Postgres
104-
needs: [build]
105-
runs-on: ubuntu-24.04
10688
# Service containers to run with `container-job`
10789
services:
10890
# Label used to access the service container
@@ -121,7 +103,10 @@ jobs:
121103
ports:
122104
- 5432:5432
123105
steps:
124-
# - run: sudo apt-get install -y libsqlite3-dev
106+
- name: Install websocat
107+
uses: baptiste0928/cargo-install@v3
108+
with:
109+
crate: websocat
125110
- uses: actions/checkout@v4
126111
- uses: actions/setup-python@v5
127112
with:
@@ -133,7 +118,7 @@ jobs:
133118
version: "0.5.6"
134119
- run: yarn
135120
- name: Run Smoke tests with Postgres
136-
run: make smoke_test_postgres_ci
121+
run: make smoke_test_ci
137122
env:
138123
POSTGRES_URL: postgres://postgres:mysecretpassword@localhost:5432/postgres
139124

Diff for: Makefile

+30-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ EXAMPLE_TAR_OUTPUT = $(abspath ./example_project.tar)
44
EXAMPLE_PROJECT_JSON = $(abspath ./example_projects/example-project.json)
55
EXAMPLE_PROJECT_POSTGRES_JSON = $(abspath ./example_projects/example-project-postgres.json)
66

7+
PARTIAL_WITH_CHANNELS_JSON = $(abspath ./example_projects/with-channels.partial.json)
8+
79
PROJECT_NAME=`cat ${EXAMPLE_PROJECT_POSTGRES_JSON} | jq -r '.name'`
10+
PROJECT_NAME_WITH_CHANNELS=`cat ${PARTIAL_WITH_CHANNELS_JSON} | jq -r '.name'`
811

912
deploy:
1013
ifeq "$(name)" ""
@@ -21,13 +24,12 @@ else
2124
endif
2225

2326
smoke_test:
24-
./script/cli_test.sh ${EXAMPLE_PROJECT_JSON}
25-
26-
smoke_test_postgres:
27-
./script/cli_test.sh ${EXAMPLE_PROJECT_POSTGRES_JSON} start_docker
27+
jq -s '.[0] * .[1]' ${EXAMPLE_PROJECT_POSTGRES_JSON} ${PARTIAL_WITH_CHANNELS_JSON} > /tmp/project-with-channels.json
28+
./script/cli_test.sh /tmp/project-with-channels.json start_docker
2829

29-
smoke_test_postgres_ci:
30-
./script/cli_test.sh ${EXAMPLE_PROJECT_POSTGRES_JSON}
30+
smoke_test_ci:
31+
jq -s '.[0] * .[1]' ${EXAMPLE_PROJECT_POSTGRES_JSON} ${PARTIAL_WITH_CHANNELS_JSON} > /tmp/project-with-channels.json
32+
./script/cli_test.sh /tmp/project-with-channels.json
3133

3234
create:
3335
yarn run cli ${EXAMPLE_PROJECT_POSTGRES_JSON} ${EXAMPLE_TAR_OUTPUT}
@@ -49,3 +51,25 @@ test_django: create
4951
cd ${PROJECT_NAME} && uv run python manage.py makemigrations
5052
cd ${PROJECT_NAME} && uv run python manage.py migrate
5153
cd ${PROJECT_NAME} && uv run pytest
54+
55+
run_django_channels: create_channels
56+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv venv --python 3.13
57+
cd ${PROJECT_NAME_WITH_CHANNELS} && . .venv/bin/activate
58+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv pip install -r requirements.txt -r requirements-dev.txt
59+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv run python manage.py makemigrations
60+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv run python manage.py migrate
61+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv run python manage.py runserver
62+
63+
create_channels:
64+
jq -s '.[0] * .[1]' ${EXAMPLE_PROJECT_POSTGRES_JSON} ${PARTIAL_WITH_CHANNELS_JSON} > /tmp/project-with-channels.json
65+
yarn run cli /tmp/project-with-channels.json ${EXAMPLE_TAR_OUTPUT}
66+
echo "Project created at ${PROJECT_NAME_WITH_CHANNELS}"
67+
tar -xvf ${EXAMPLE_TAR_OUTPUT}
68+
69+
test_django_channels: create_channels
70+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv venv --python 3.13
71+
cd ${PROJECT_NAME_WITH_CHANNELS} && . .venv/bin/activate
72+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv pip install -r requirements.txt -r requirements-dev.txt
73+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv run python manage.py makemigrations
74+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv run python manage.py migrate
75+
cd ${PROJECT_NAME_WITH_CHANNELS} && uv run pytest

Diff for: example_projects/with-channels.partial.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "DjangoProjectWithChannels",
3+
"description": "A Django Project with Channels",
4+
"channels": true
5+
}

Diff for: lib/djangobuilder-core/src/django/python/asgi.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ export const template = `
33
ASGI entrypoint for {{project.name}}. Configures Django and then runs the application
44
defined in the ASGI_APPLICATION setting.
55
"""
6-
76
import os
8-
import django
9-
from channels.routing import get_default_application
7+
8+
from channels.routing import ProtocolTypeRouter, URLRouter
9+
from channels.security.websocket import AllowedHostsOriginValidator
10+
from django.core.asgi import get_asgi_application
1011
1112
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{project.name}}.settings")
12-
django.setup()
13-
application = get_default_application()
13+
# Initialize Django ASGI application early to ensure the AppRegistry
14+
# is populated before importing code that may import ORM models.
15+
django_asgi_app = get_asgi_application()
16+
17+
from .routing import websocket_urlpatterns
18+
19+
application = ProtocolTypeRouter(
20+
{
21+
"http": django_asgi_app,
22+
"websocket": URLRouter(websocket_urlpatterns)
23+
}
24+
)
25+
1426
`

Diff for: lib/djangobuilder-core/src/django/python/consumers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class {{project.name}}WebSocketConsumer(WebsocketConsumer):
1010
def receive(self, text_data=None, bytes_data=None):
1111
if text_data:
1212
print(f"Received text={text_data}")
13-
self.send(text_data="Hello from {{project.name}} WebSocketConsumer!")
13+
self.send(text_data=f"Hello from {{project.name}} WebSocketConsumer! {text_data}")
1414
if bytes_data:
1515
print(f"Received bytes={bytes_data}")
16-
self.send(bytes_data=b"Hello from {{project.name}} WebSocketConsumer!")
16+
self.send(bytes_data=b"Hello from {{project.name}} WebSocketConsumer! " + bytes_data)
1717
1818
def disconnect(self, message):
1919
# Called when disconnected

Diff for: lib/djangobuilder-core/src/django/python/routing.ts

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
11
export const template = `
2-
from django.conf.urls import url
2+
from django.urls import re_path
33
4-
from channels.routing import ChannelNameRouter, ProtocolTypeRouter, URLRouter
5-
from channels.auth import AuthMiddlewareStack
4+
from .consumers import {{project.name}}WebSocketConsumer
65
7-
from {{project.name}}.consumers import {{project.name}}_WebSocketConsumer
6+
websocket_urlpatterns = [
7+
re_path(r"^ws/$", {{project.name}}WebSocketConsumer.as_asgi()),
8+
]
89
9-
{{#project.apps}}
10-
from {{name}}.consumers import {{ camelCase name }}Consumer
11-
{{/project.apps}}
12-
13-
application = ProtocolTypeRouter({
14-
15-
# WebSocket handler
16-
"websocket": AuthMiddlewareStack(
17-
URLRouter([
18-
url(r"^ws/$", XXX_PROJECT_NAME_XXX_WebSocketConsumer.as_asgi()),
19-
])
20-
),
21-
"channel": ChannelNameRouter({
22-
{{#project.apps}}
23-
"{{name}}": {{ camelCase name }}Consumer,
24-
{{/project.apps}}
25-
})
26-
})
2710
`

Diff for: lib/djangobuilder-core/src/django/python/settings.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ ALLOWED_HOSTS = []
3232
3333
# Application definition
3434
INSTALLED_APPS = [
35+
{{#project.channels}}'daphne',{{/project.channels}}
3536
'django.contrib.admin',
3637
'django.contrib.auth',
3738
'django.contrib.contenttypes',
3839
'django.contrib.sessions',
3940
'django.contrib.messages',
4041
'django.contrib.staticfiles',
4142
'rest_framework',
42-
{{#project.channels}}'channels',{{/project.channels}}
4343
{{#project.htmx}}'django_htmx',{{/project.htmx}}
4444
{{#project.apps}}
4545
'{{name}}',
@@ -72,7 +72,7 @@ TEMPLATES = [
7272
]
7373
7474
WSGI_APPLICATION = '{{project.name}}.wsgi.application'
75-
75+
{{#project.channels}}ASGI_APPLICATION = "{{project.name}}.asgi.application"{{/project.channels}}
7676
7777
# Database
7878
# https://docs.djangoproject.com/en/{{project.version}}/ref/settings/#databases

Diff for: lib/djangobuilder-core/src/django/requirements/requirements.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Django=={{project.version}}
33
djangorestframework
44
asgiref
55
{{#project.channels}}
6-
channels
6+
channels[daphne]
77
channels_redis
88
{{/project.channels}}
99
{{#project.htmx}}

0 commit comments

Comments
 (0)