Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstate fieldsets for admin and make test server boot #70

Merged
merged 7 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions django_twined/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
from jsoneditor.forms import JSONEditor
from octue.log_handlers import LOG_RECORD_ATTRIBUTES_WITH_TIMESTAMP, create_octue_formatter

from .fieldsets import (
question_basic_fieldset,
question_delivery_ack_fieldset,
question_exceptions_fieldset,
question_log_records_fieldset,
question_monitor_messages_fieldset,
question_result_fieldset,
)
from .mixins import CreatableFieldsMixin


Expand Down Expand Up @@ -48,25 +56,14 @@ class QuestionAdmin(admin.ModelAdmin):
)

fieldsets = (
(
None,
{
"fields": (
"id",
"status",
"service_revision",
"asked",
"answered",
"latest_heartbeat",
)
},
),
("Inputs", {"classes": ("collapse",), "fields": ("input_values",)}),
("Delivery Acknowledgement", {"classes": ("collapse",), "fields": ("delivery_acknowledgement",)}),
("Log Records", {"classes": ("collapse",), "fields": ("log_records",)}),
("Monitor Messages", {"classes": ("collapse",), "fields": ("monitor_messages",)}),
("Result", {"classes": ("collapse",), "fields": ("result",)}),
("Exceptions", {"classes": ("collapse",), "fields": ("exceptions",)}),
question_basic_fieldset,
# question_db_input_values_fieldset,
# question_db_output_values_fieldset,
question_delivery_ack_fieldset,
question_log_records_fieldset,
question_monitor_messages_fieldset,
question_result_fieldset,
question_exceptions_fieldset,
)

# @staticmethod
Expand Down
26 changes: 26 additions & 0 deletions django_twined/admin/fieldsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
question_basic_fieldset = (
None,
{
"fields": (
"id",
"status",
"service_revision",
"asked",
"answered",
"latest_heartbeat",
"duration",
)
},
)

question_db_input_values_fieldset = ("Input Values", {"classes": ("collapse",), "fields": ("input_values",)})
question_db_output_values_fieldset = ("Output Values", {"classes": ("collapse",), "fields": ("output_values",)})

question_delivery_ack_fieldset = (
"Delivery Acknowledgement",
{"classes": ("collapse",), "fields": ("delivery_acknowledgement",)},
)
question_log_records_fieldset = ("Log Records", {"classes": ("collapse",), "fields": ("log_records",)})
question_monitor_messages_fieldset = ("Monitor Messages", {"classes": ("collapse",), "fields": ("monitor_messages",)})
question_result_fieldset = ("Result", {"classes": ("collapse",), "fields": ("result",)})
question_exceptions_fieldset = ("Exceptions", {"classes": ("collapse",), "fields": ("exceptions",)})
9 changes: 9 additions & 0 deletions django_twined/models/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def status_message(self):
"""
return STATUS_MESSAGE_MAP[self.status]

@property
def duration(self):
"""Show the time it took to answer the question in seconds.

:return int|None:
"""
if self.answered and self.asked:
return (self.answered - self.asked).seconds

def get_duplicate(self, save=True):
"""Duplicate the question instance and optionally save to the database"""
kwargs = {}
Expand Down
2 changes: 1 addition & 1 deletion django_twined/signals/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def receive_event(sender, event_kind, event_reference, event_payload, event_para
if event_kind == "delivery_acknowledgement":
delivery_acknowledgement_received.send(sender=ServiceUsageEvent, service_usage_event=sue)

if event_kind == "exception":
elif event_kind == "exception":
exception_received.send(sender=ServiceUsageEvent, service_usage_event=sue)

elif event_kind == "heartbeat":
Expand Down
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-twined"
version = "0.7.1"
version = "0.7.2"
description = "A django app to manage octue services"
authors = ["Tom Clark <[email protected]>", "Marcus Lugg <[email protected]>"]
license = "MIT"
Expand All @@ -10,11 +10,8 @@ classifiers = [
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries :: Python Modules",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Operating System :: OS Independent",
]
repository = "https://github.com/octue/django-twined"
Expand Down
7 changes: 2 additions & 5 deletions tests/server/asgi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import django_twined.routing
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application


# TESTS ONLY - this sets up an asgi application for use in async testing of the consumer.
# The main django application which you're writing an app for will need to set up something similar


application = ProtocolTypeRouter(
{"http": get_asgi_application(), "websocket": URLRouter(django_twined.routing.websocket_urlpatterns)}
)
application = ProtocolTypeRouter({"http": get_asgi_application()})
23 changes: 23 additions & 0 deletions tests/test_models/test_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# pylint: disable=missing-docstring
# pylint: disable=protected-access
# pylint: disable=too-many-public-methods
import datetime
import os
import time
from unittest import skipIf
from unittest.mock import patch

Expand All @@ -18,6 +20,12 @@


class QuestionTestCase(TestCase):
def test_unasked_question_duration(self):
"""Test that the duration of an unasked question is `None`."""
sr = ServiceRevision.objects.create(name="test-service")
q = QuestionWithValuesDatabaseStorage.objects.create(service_revision=sr)
self.assertIsNone(q.duration)

@patch("django_twined.models.ServiceRevision.ask", return_value=("subscription", "question_uuid"))
def test_ask_question(self, mock):
"""Ensures that a question can be asked"""
Expand All @@ -32,6 +40,21 @@ def test_ask_question(self, mock):
self.assertIn("input_manifest", mock.call_args.kwargs)
self.assertIn("question_attribute", mock.call_args.kwargs["input_values"])

# Check that the duration is `None` as the question hasn't been answered.
self.assertIsNone(q.duration)

@patch("django_twined.models.ServiceRevision.ask", return_value=("subscription", "question_uuid"))
def test_answered_question_duration(self, mock):
"""Test that the duration of an answered question is a non-zero integer."""
sr = ServiceRevision.objects.create(name="test-service")
q = QuestionWithValuesDatabaseStorage.objects.create(service_revision=sr)
q.input_values = {"question_attribute": "1"}
q.ask()

time.sleep(1)
q.answered = datetime.datetime.now(tz=datetime.timezone.utc)
self.assertTrue(q.duration > 0)

def test_input_values_get_saved(self):
"""Ensures that input values get saved on the mixin.
I'm testing this because the objects.create() doesn't accept the input_values
Expand Down
Loading