From bd11ddbd601fb8d54278dfc1618d9954836f471c Mon Sep 17 00:00:00 2001 From: rmanach Date: Sun, 17 Sep 2023 14:06:23 +0200 Subject: [PATCH] add sse server --- .../migrations/0007_alter_deployment_id.py | 22 ++++++++ deployment/models.py | 9 +++ deployment/views.py | 8 ++- mumui/asgi.py | 24 +++++++- mumui/settings.py | 5 ++ requirements.txt | 43 +++++++++++++++ templates/base.html | 4 +- templates/deployment/board.html | 55 ++++++++++++++++++- templates/deployment/details.html | 10 ++-- 9 files changed, 165 insertions(+), 15 deletions(-) create mode 100644 deployment/migrations/0007_alter_deployment_id.py create mode 100644 requirements.txt diff --git a/deployment/migrations/0007_alter_deployment_id.py b/deployment/migrations/0007_alter_deployment_id.py new file mode 100644 index 0000000..2132d6e --- /dev/null +++ b/deployment/migrations/0007_alter_deployment_id.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.5 on 2023-09-16 17:21 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + dependencies = [ + ("deployment", "0006_alter_deployment_id_alter_deployment_status_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="deployment", + name="id", + field=models.UUIDField( + default=uuid.UUID("0cc4ae03-f52e-4f3a-9fe1-fecda707b20e"), + primary_key=True, + serialize=False, + ), + ), + ] diff --git a/deployment/models.py b/deployment/models.py index 1a21c23..3e61d99 100644 --- a/deployment/models.py +++ b/deployment/models.py @@ -4,6 +4,9 @@ from uuid import uuid4 from django.contrib.auth.models import User from django.db import models +from django.db.models.signals import pre_save, pre_delete +from django.dispatch import receiver +from django_eventstream import send_event class Type(Enum): @@ -41,3 +44,9 @@ class Deployment(models.Model): def __str__(self): return f"{self.name} | {self.type} | {self.status}" + + +@receiver(pre_save, sender=Deployment) +@receiver(pre_delete, sender=Deployment) +def deployment_update_handler(sender, instance, **kwargs): + send_event("deployment", "update", {"id": instance.id, "status": instance.status}) diff --git a/deployment/views.py b/deployment/views.py index 03f980e..64066bd 100644 --- a/deployment/views.py +++ b/deployment/views.py @@ -9,7 +9,7 @@ from django.http import ( from django.shortcuts import render, get_object_or_404 from deployment.forms import DeploymentForm -from deployment.models import Deployment +from deployment.models import Deployment, Status def index(request): @@ -19,14 +19,16 @@ def index(request): page_number = request.GET.get("page") page_obj = paginator.get_page(page_number) - return render(request, "deployment/board.html", {"page_obj": page_obj}) + return render( + request, "deployment/board.html", {"page_obj": page_obj, "url": "/events/"} + ) def details(request, deployment_id): deployment = get_object_or_404(Deployment, id=deployment_id) if request.method == "POST": - if deployment.status == deployment.RUNNING: + if deployment.status == Status.RUNNING: return HttpResponseBadRequest("deployment is running") try: deployment.delete() diff --git a/mumui/asgi.py b/mumui/asgi.py index 60caa81..17f6c5d 100644 --- a/mumui/asgi.py +++ b/mumui/asgi.py @@ -8,9 +8,27 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ """ import os - from django.core.asgi import get_asgi_application +from django.urls import path, re_path +from channels.routing import ProtocolTypeRouter, URLRouter +from channels.auth import AuthMiddlewareStack +import django_eventstream -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mumui.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings") -application = get_asgi_application() +application = ProtocolTypeRouter( + { + "http": URLRouter( + [ + path( + "events/", + AuthMiddlewareStack( + URLRouter(django_eventstream.routing.urlpatterns) + ), + {"channels": ["test"]}, + ), + re_path(r"", get_asgi_application()), + ] + ), + } +) diff --git a/mumui/settings.py b/mumui/settings.py index 3666c3e..b8a4ee6 100644 --- a/mumui/settings.py +++ b/mumui/settings.py @@ -38,9 +38,12 @@ INSTALLED_APPS = [ "django.contrib.messages", "django.contrib.staticfiles", "deployment", + "channels", + "django_eventstream", ] MIDDLEWARE = [ + "django_grip.GripMiddleware", "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", @@ -125,3 +128,5 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" LOGIN_REDIRECT_URL = "home" LOGOUT_REDIRECT_URL = "home" + +ASGI_APPLICATION = "mumui.asgi.application" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..56da695 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,43 @@ +asgiref==3.7.2 +attrs==23.1.0 +autobahn==23.6.2 +Automat==22.10.0 +black==23.9.1 +certifi==2023.7.22 +cffi==1.15.1 +channels==3.0.5 +charset-normalizer==3.2.0 +click==8.1.7 +constantly==15.1.0 +cryptography==41.0.3 +daphne==3.0.2 +Django==4.2.5 +django-eventstream==4.5.1 +django-grip==3.4.0 +gripcontrol==4.2.0 +hyperlink==21.0.0 +idna==3.4 +incremental==22.10.0 +MarkupSafe==2.1.3 +mypy-extensions==1.0.0 +packaging==23.1 +pathspec==0.11.2 +platformdirs==3.10.0 +pubcontrol==3.5.0 +pyasn1==0.5.0 +pyasn1-modules==0.3.0 +pycparser==2.21 +PyJWT==2.8.0 +pyOpenSSL==23.2.0 +requests==2.31.0 +ruff==0.0.290 +service-identity==23.1.0 +six==1.16.0 +sqlparse==0.4.4 +tomli==2.0.1 +Twisted==23.8.0 +txaio==23.1.1 +typing_extensions==4.7.1 +urllib3==2.0.4 +Werkzeug==2.3.7 +zope.interface==6.0 diff --git a/templates/base.html b/templates/base.html index 2776337..98c11fb 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,8 +3,9 @@ {% block title %}mumui{% endblock %} + {% block headscript %}{% endblock %} - +
{% if user.is_authenticated or "login" in request.path %} {% block content %} @@ -17,5 +18,6 @@ log In {% endif %}
+ {% block script %}{% endblock %} \ No newline at end of file diff --git a/templates/deployment/board.html b/templates/deployment/board.html index 33f5d33..20115b9 100644 --- a/templates/deployment/board.html +++ b/templates/deployment/board.html @@ -1,15 +1,42 @@ {% extends 'base.html' %} +{% load static %} + {% block title %}deployment's board{% endblock %} +{% block headscript %} + + + +{% endblock %} + +{% block bodyattr %} + onload="start();" +{% endblock %} + {% block content %} {{ user.username }}'s board {% if page_obj %} -