Skip to content

Commit

Permalink
Update UI message feed via polling
Browse files Browse the repository at this point in the history
  • Loading branch information
cc-a committed Oct 10, 2024
1 parent e231526 commit 057dd3e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 32 deletions.
6 changes: 3 additions & 3 deletions process_manager/templates/process_manager/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
</div>
<div class="card-body" id="message-list">
<ul class="list-group">
{% if messages %}
{% for message in messages %}<li class="list-group-item">{{ message }}</li>{% endfor %}
{% endif %}
<div hx-get="{% url 'process_manager:messages' %}"
hx-trigger="every 1s"
hx-swap="afterend"></div>
</ul>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% for message in messages %}<li class="list-group-item">{{ message }}</li>{% endfor %}
1 change: 1 addition & 0 deletions process_manager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

partial_urlpatterns = [
path("process_table/", partials.process_table, name="process_table"),
path("messages/", partials.messages, name="messages"),
]

urlpatterns = [
Expand Down
12 changes: 1 addition & 11 deletions process_manager/views/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db import transaction
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.urls import reverse_lazy
Expand All @@ -17,16 +16,7 @@
@login_required
def index(request: HttpRequest) -> HttpResponse:
"""View that renders the index/home page."""
with transaction.atomic():
# atomic to avoid race condition with kafka consumer
messages = request.session.load().get("messages", [])
request.session.pop("messages", [])
request.session.save()

context = {"messages": messages}
return render(
request=request, context=context, template_name="process_manager/index.html"
)
return render(request=request, template_name="process_manager/index.html")


@login_required
Expand Down
17 changes: 17 additions & 0 deletions process_manager/views/partials.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import django_tables2
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from druncschema.process_manager_pb2 import (
Expand Down Expand Up @@ -52,3 +53,19 @@ def process_table(request: HttpRequest) -> HttpResponse:
context=dict(table=table),
template_name="process_manager/partials/process_table.html",
)


@login_required
def messages(request: HttpRequest) -> HttpResponse:
"""Renders and pops Kafka messages from the user's session."""
with transaction.atomic():
# atomic to avoid race condition with kafka consumer
messages = request.session.load().get("messages", [])
request.session.pop("messages", [])
request.session.save()

return render(
request=request,
context=dict(messages=messages[::-1]),
template_name="process_manager/partials/message_items.html",
)
18 changes: 0 additions & 18 deletions tests/process_manager/views/test_page_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,6 @@ def test_authenticated(self, auth_client):
response = auth_client.get(self.endpoint)
assert response.status_code == HTTPStatus.OK

def test_session_messages(self, auth_client):
"""Test the rendering of messages from the user session into the view."""
from django.contrib.sessions.backends.db import SessionStore
from django.contrib.sessions.models import Session

session = Session.objects.get()
message_data = ["message 1", "message 2"]
store = SessionStore(session_key=session.session_key)
store["messages"] = message_data
store.save()

response = auth_client.get(self.endpoint)
assert response.status_code == HTTPStatus.OK

# messages have been removed from the session and added to the context
assert response.context["messages"] == message_data
assert "messages" not in store.load()


class TestLogsView(PermissionRequiredTest):
"""Tests for the logs view."""
Expand Down
26 changes: 26 additions & 0 deletions tests/process_manager/views/test_partial_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
from django.urls import reverse
from pytest_django.asserts import assertTemplateUsed

from process_manager.tables import ProcessTable

Expand Down Expand Up @@ -47,3 +48,28 @@ def test_post_checked_rows(self, mocker, auth_client):

for row in table.data.data:
assert row["checked"] == (row["uuid"] in selected_uuids)


class TestMessagesView(LoginRequiredTest):
"""Test the process_manager.views.messages view function."""

endpoint = reverse("process_manager:messages")

def test_get(self, auth_client):
"""Tests basic calls of view method."""
from django.contrib.sessions.backends.db import SessionStore
from django.contrib.sessions.models import Session

session = Session.objects.get()
message_data = ["message 1", "message 2"]
store = SessionStore(session_key=session.session_key)
store["messages"] = message_data
store.save()

with assertTemplateUsed("process_manager/partials/message_items.html"):
response = auth_client.get(self.endpoint)
assert response.status_code == HTTPStatus.OK

# messages have been removed from the session and added to the context
assert response.context["messages"] == message_data[::-1]
assert "messages" not in store.load()

0 comments on commit 057dd3e

Please sign in to comment.