This commit is contained in:
Andros Fenollosa 2024-11-05 16:52:47 +01:00
parent b97d9383a5
commit 19d5317f0d
9 changed files with 87 additions and 6 deletions

View File

@ -14,9 +14,8 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.urls import path, include
from django.urls import path
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('', include('waiting_room.urls')),
] ]

View File

@ -9,15 +9,18 @@ class MyConsumer(WebsocketConsumer):
self.room_group_name = self.scope["url_route"]["kwargs"]["room_name"] self.room_group_name = self.scope["url_route"]["kwargs"]["room_name"]
async_to_sync(self.channel_layer.group_add)(self.room_group_name, self.channel_name) async_to_sync(self.channel_layer.group_add)(self.room_group_name, self.channel_name)
self.accept() self.accept()
calculate_min_distance(self.room_group_name)
def disconnect(self, close_code): def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)(self.room_group_name, self.channel_name) async_to_sync(self.channel_layer.group_discard)(self.room_group_name, self.channel_name)
self.close() self.close()
def receive(self, text_data): def receive(self, text_data):
json_data = json.loads(text_data)
# Echo # Echo
self.send(text_data=text_data) self.send(text_data=text_data)
# Run task
if json_data['task'] == 'calculate':
calculate_min_distance(self.room_group_name)
def channel_message(self, event): def channel_message(self, event):
message = event['message'] message = event['message']

View File

@ -13,7 +13,7 @@ def render_progress_bar(group_name, message):
group_name, group_name,
{ {
'type': 'channel_message', 'type': 'channel_message',
'message': message 'message': f'<div id="notification">{message}</div>',
} }
) )

View File

@ -0,0 +1,8 @@
{% extends 'base.html' %}
{% block title %}About us{% endblock %}
{% block content %}
<h1>About us</h1>
<a href="{% url 'waiting_room:index' %}">Go to index</a>
{% endblock %}

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>{% block title %}Waiting Room{% endblock %}</title>
<script src="https://unpkg.com/htmx.org@2.0.3" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script>
<script src="https://unpkg.com/htmx-ext-ws@2.0.1/ws.js"></script>
</head>
<body>
<main>
{% include 'progress_bar.html' %}
<section>{% block content %}{% endblock %}</section>
</main>
</body>
</html>

View File

@ -0,0 +1,21 @@
{% extends 'base.html' %}
{% block title %}Run task{% endblock %}
{% block content %}
<h1>Index</h1>
<section
hx-ext="ws"
ws-connect="/ws/nombre_temporal/"
>
<form id="form" ws-send>
<input type="submit" name="task" value="calculate">
</form>
</section>
<section>
{% if result %}
<h2>The result is {{ result }}</h2>
{% endif %}
</section>
<a href="{% url 'waiting_room:about_us' %}">Go to about us</a>
{% endblock %}

View File

@ -0,0 +1,20 @@
<section
hx-ext="ws"
ws-connect="/ws/nombre_temporal/"
hx-swap-oob="innerHTML:notification"
style="
position: fixed;
right: 0;
bottom: 0;
padding: 2rem;
background: lightgray;
">
<div id="notification"></div>
<!-- <h1>Task</h1>
{% if result %}
<h3>Done!</h3>
<a href="{% url 'waiting_room:index' %}?result={{ result }}">Show result</a>
{% else %}
<progress value="{{ progress }}" max="100">{{ progress }}</progress>
{% endif %} -->
</section>

9
waiting_room/urls.py Normal file
View File

@ -0,0 +1,9 @@
from django.urls import path
from . import views
app_name = 'waiting_room'
urlpatterns = [
path('', views.index, name='index'),
path('about_us/', views.about_us, name='about_us'),
]

View File

@ -1,3 +1,8 @@
from django.shortcuts import render from django.shortcuts import render
# Create your views here. def index(request):
result = request.GET.get('result', None)
return render(request, 'index.html', {'result': result})
def about_us(request):
return render(request, 'about_us.html')