From 0cb110a0729f57bfe2f6de4f5c7024308da641b8 Mon Sep 17 00:00:00 2001 From: DonHaul Date: Tue, 13 Aug 2024 13:48:58 +0200 Subject: [PATCH] author decisions: decisions draft --- backoffice/backoffice/workflows/admin.py | 77 ++++++++++++------- .../backoffice/workflows/api/serializers.py | 8 +- backoffice/backoffice/workflows/api/views.py | 42 +++++++++- backoffice/backoffice/workflows/constants.py | 1 + .../workflows/migrations/0009_decision.py | 56 ++++++++++++++ ...0_remove_decision_user_id_decision_user.py | 31 ++++++++ backoffice/backoffice/workflows/models.py | 13 ++++ .../backoffice/workflows/tests/test_views.py | 41 ++++++++++ backoffice/config/api_router.py | 2 + workflows/__init__.py | 0 .../author_create/author_create_approved.py | 35 +++------ .../author_create/author_create_rejected.py | 11 ++- .../process_until_breakpoint.py | 0 .../dags/author/author_create/shared_tasks.py | 12 +++ workflows/plugins/hooks/backoffice/base.py | 8 ++ .../backoffice/decision_management_hook.py | 38 +++++++++ 16 files changed, 322 insertions(+), 53 deletions(-) create mode 100644 backoffice/backoffice/workflows/migrations/0009_decision.py create mode 100644 backoffice/backoffice/workflows/migrations/0010_remove_decision_user_id_decision_user.py create mode 100644 workflows/__init__.py rename workflows/dags/{ => author/author_create}/process_until_breakpoint.py (100%) create mode 100644 workflows/dags/author/author_create/shared_tasks.py create mode 100644 workflows/plugins/hooks/backoffice/decision_management_hook.py diff --git a/backoffice/backoffice/workflows/admin.py b/backoffice/backoffice/workflows/admin.py index 308def4b6..fc3dd09f9 100644 --- a/backoffice/backoffice/workflows/admin.py +++ b/backoffice/backoffice/workflows/admin.py @@ -3,7 +3,7 @@ from django_json_widget.widgets import JSONEditorWidget from backoffice.management.permissions import IsAdminOrCuratorUser -from backoffice.workflows.models import Workflow +from backoffice.workflows.models import Decision, Workflow class WorkflowsAdminSite(admin.AdminSite): @@ -30,8 +30,37 @@ def has_permission(self, request): ) +class ModelAdmin(admin.ModelAdmin): + def has_view_permission(self, request, obj=None): + """ + Returns True if the user has permission to view the Workflow model. + """ + permission_check = IsAdminOrCuratorUser() + return request.user.is_superuser or permission_check.has_permission( + request, self + ) + + def has_change_permission(self, request, obj=None): + """ + Returns True if the user has permission to change the Workflow model. + """ + permission_check = IsAdminOrCuratorUser() + return request.user.is_superuser or permission_check.has_permission( + request, self + ) + + def has_delete_permission(self, request, obj=None): + """ + Returns True if the user has permission to delete the Workflow model. + """ + permission_check = IsAdminOrCuratorUser() + return request.user.is_superuser or permission_check.has_permission( + request, self + ) + + @admin.register(Workflow) -class WorkflowAdmin(admin.ModelAdmin): +class WorkflowAdmin(ModelAdmin): """ Admin class for Workflow model. Define get, update and delete permissions. """ @@ -60,29 +89,25 @@ class WorkflowAdmin(admin.ModelAdmin): JSONField: {"widget": JSONEditorWidget}, } - def has_view_permission(self, request, obj=None): - """ - Returns True if the user has permission to view the Workflow model. - """ - permission_check = IsAdminOrCuratorUser() - return request.user.is_superuser or permission_check.has_permission( - request, self - ) - def has_change_permission(self, request, obj=None): - """ - Returns True if the user has permission to change the Workflow model. - """ - permission_check = IsAdminOrCuratorUser() - return request.user.is_superuser or permission_check.has_permission( - request, self - ) +@admin.register(Decision) +class DecisionAdmin(admin.ModelAdmin): + """ + Admin class for Decision model. Define get, update and delete permissions. + """ - def has_delete_permission(self, request, obj=None): - """ - Returns True if the user has permission to delete the Workflow model. - """ - permission_check = IsAdminOrCuratorUser() - return request.user.is_superuser or permission_check.has_permission( - request, self - ) + ordering = ("-_updated_at",) + search_fields = ["id", "data"] + list_display = ( + "id", + "action", + "user", + ) + list_filter = [ + "action", + "user", + ] + + formfield_overrides = { + JSONField: {"widget": JSONEditorWidget}, + } diff --git a/backoffice/backoffice/workflows/api/serializers.py b/backoffice/backoffice/workflows/api/serializers.py index cabb7cb4b..0fca9029a 100644 --- a/backoffice/backoffice/workflows/api/serializers.py +++ b/backoffice/backoffice/workflows/api/serializers.py @@ -4,7 +4,7 @@ from backoffice.workflows.constants import ResolutionDags, StatusChoices, WorkflowType from backoffice.workflows.documents import WorkflowDocument -from backoffice.workflows.models import Workflow, WorkflowTicket +from backoffice.workflows.models import Decision, Workflow, WorkflowTicket class WorkflowSerializer(serializers.ModelSerializer): @@ -19,6 +19,12 @@ class Meta: fields = "__all__" +class DecisionSerializer(serializers.ModelSerializer): + class Meta: + model = Decision + fields = "__all__" + + class WorkflowDocumentSerializer(DocumentSerializer): class Meta: document = WorkflowDocument diff --git a/backoffice/backoffice/workflows/api/views.py b/backoffice/backoffice/workflows/api/views.py index 3772df0df..625a6b22a 100644 --- a/backoffice/backoffice/workflows/api/views.py +++ b/backoffice/backoffice/workflows/api/views.py @@ -25,6 +25,7 @@ from backoffice.workflows import airflow_utils from backoffice.workflows.api.serializers import ( AuthorResolutionSerializer, + DecisionSerializer, WorkflowAuthorSerializer, WorkflowDocumentSerializer, WorkflowSerializer, @@ -37,7 +38,7 @@ WorkflowType, ) from backoffice.workflows.documents import WorkflowDocument -from backoffice.workflows.models import Workflow, WorkflowTicket +from backoffice.workflows.models import Decision, Workflow, WorkflowTicket logger = logging.getLogger(__name__) @@ -100,6 +101,43 @@ def create(self, request, *args, **kwargs): ) +class DecisionViewSet(viewsets.ViewSet): + def retrieve(self, request, *args, **kwargs): + workflow_id = kwargs.get("pk") + try: + decision = Decision.objects.get(workflow_id=workflow_id) + serializer = DecisionSerializer(decision) + return Response(serializer.data) + except Decision.DoesNotExist: + return Response( + {"error": "Workflow ticket not found."}, + status=status.HTTP_404_NOT_FOUND, + ) + + def create(self, request, *args, **kwargs): + workflow_id = request.data.get("workflow_id") + action = request.data.get("action") + + if not all([workflow_id, action]): + return Response( + {"error": "workflow_id and action are required."}, + status=status.HTTP_400_BAD_REQUEST, + ) + + try: + workflow = Workflow.objects.get(id=workflow_id) + + decision = Decision.objects.create( + workflow_id=workflow, user=request.user, action=action + ) + serializer = DecisionSerializer(decision) + return Response(serializer.data, status=status.HTTP_201_CREATED) + except Exception as e: + return Response( + {"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR + ) + + class AuthorWorkflowViewSet(viewsets.ViewSet): serializer_class = WorkflowAuthorSerializer @@ -160,7 +198,7 @@ def resolve(self, request, pk=None): logger.info("Resolving data: %s", request.data) serializer = AuthorResolutionSerializer(data=request.data) if serializer.is_valid(raise_exception=True): - extra_data = {"create_ticket": serializer.validated_data["create_ticket"]} + extra_data = serializer.validated_data logger.info( "Trigger Airflow DAG: %s for %s", ResolutionDags[serializer.validated_data["value"]], diff --git a/backoffice/backoffice/workflows/constants.py b/backoffice/backoffice/workflows/constants.py index 273faeaca..6cb4c3529 100644 --- a/backoffice/backoffice/workflows/constants.py +++ b/backoffice/backoffice/workflows/constants.py @@ -33,6 +33,7 @@ class WorkflowType(models.TextChoices): class ResolutionDags(models.TextChoices): accept = "accept", "author_create_approved_dag" reject = "reject", "author_create_rejected_dag" + accept_curate = "accept_curate", "author_create_approved_dag" class AuthorCreateDags(models.TextChoices): diff --git a/backoffice/backoffice/workflows/migrations/0009_decision.py b/backoffice/backoffice/workflows/migrations/0009_decision.py new file mode 100644 index 000000000..dcefd6d35 --- /dev/null +++ b/backoffice/backoffice/workflows/migrations/0009_decision.py @@ -0,0 +1,56 @@ +# Generated by Django 4.2.6 on 2024-08-13 08:38 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("workflows", "0008_alter_workflow_status_alter_workflow_workflow_type"), + ] + + operations = [ + migrations.CreateModel( + name="Decision", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "action", + models.CharField( + choices=[ + ("accept", "author_create_approved_dag"), + ("reject", "author_create_rejected_dag"), + ("accept_curate", "author_create_approved_dag"), + ], + max_length=30, + ), + ), + ("_created_at", models.DateTimeField(auto_now_add=True)), + ("_updated_at", models.DateTimeField(auto_now=True)), + ( + "user_id", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ( + "workflow_id", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="workflows.workflow", + ), + ), + ], + ), + ] diff --git a/backoffice/backoffice/workflows/migrations/0010_remove_decision_user_id_decision_user.py b/backoffice/backoffice/workflows/migrations/0010_remove_decision_user_id_decision_user.py new file mode 100644 index 000000000..92216fe5d --- /dev/null +++ b/backoffice/backoffice/workflows/migrations/0010_remove_decision_user_id_decision_user.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.6 on 2024-08-14 09:10 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("workflows", "0009_decision"), + ] + + operations = [ + migrations.RemoveField( + model_name="decision", + name="user_id", + ), + migrations.AddField( + model_name="decision", + name="user", + field=models.ForeignKey( + db_column="email", + default=1, + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + to_field="email", + ), + preserve_default=False, + ), + ] diff --git a/backoffice/backoffice/workflows/models.py b/backoffice/backoffice/workflows/models.py index 265641cbf..c30652eb0 100644 --- a/backoffice/backoffice/workflows/models.py +++ b/backoffice/backoffice/workflows/models.py @@ -2,11 +2,13 @@ from django.db import models +from backoffice.users.models import User from backoffice.workflows.constants import ( DEFAULT_STATUS_CHOICE, DEFAULT_TICKET_TYPE, DEFAULT_WORKFLOW_TYPE, TICKET_TYPES, + ResolutionDags, StatusChoices, WorkflowType, ) @@ -41,3 +43,14 @@ class WorkflowTicket(models.Model): ticket_type = models.CharField( max_length=30, choices=TICKET_TYPES, default=DEFAULT_TICKET_TYPE ) + + +class Decision(models.Model): + user = models.ForeignKey( + User, to_field="email", db_column="email", on_delete=models.CASCADE + ) + workflow_id = models.ForeignKey(Workflow, on_delete=models.CASCADE) + action = models.CharField(max_length=30, choices=ResolutionDags.choices) + + _created_at = models.DateTimeField(auto_now_add=True) + _updated_at = models.DateTimeField(auto_now=True) diff --git a/backoffice/backoffice/workflows/tests/test_views.py b/backoffice/backoffice/workflows/tests/test_views.py index 0c8737dd1..f1f3dd658 100644 --- a/backoffice/backoffice/workflows/tests/test_views.py +++ b/backoffice/backoffice/workflows/tests/test_views.py @@ -11,6 +11,8 @@ from django.test import TransactionTestCase from django.urls import reverse from django_opensearch_dsl.registries import registry +from opensearch_dsl import Index +from rest_framework import status from rest_framework.test import APIClient from backoffice.workflows import airflow_utils @@ -20,6 +22,7 @@ User = get_user_model() Workflow = apps.get_model(app_label="workflows", model_name="Workflow") +Decision = apps.get_model(app_label="workflows", model_name="Decision") class BaseTransactionTestCase(TransactionTestCase): @@ -471,3 +474,41 @@ def test_ordering(self): if previous_date is not None: assert cur_date < previous_date previous_date = cur_date + + +class TestDecisionsViewSet(BaseTransactionTestCase): + endpoint = "/api/decisions" + reset_sequences = True + fixtures = ["backoffice/fixtures/groups.json"] + + def setUp(self): + super().setUp() + self.workflow = Workflow.objects.create( + data={}, status="running", core=True, is_update=False + ) + + def test_create_decision(self): + self.api_client.force_authenticate(user=self.curator) + data = { + "workflow_id": self.workflow.id, + "action": "accept", + } + + url = reverse("api:decisions-list") + response = self.api_client.post(url, format="json", data=data) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + def test_get_decisoin(self): + self.api_client.force_authenticate(user=self.curator) + + decision = Decision.objects.create( + workflow_id=self.workflow, action="accept", user=self.curator + ) + + url = reverse("api:decisions-detail", kwargs={"pk": decision.id}) + + print("wawa") + print(url) + response = self.api_client.get(f"{url}/{decision.id}") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(decision.id, response.data[id]) diff --git a/backoffice/config/api_router.py b/backoffice/config/api_router.py index 179a686bb..fce8e177b 100644 --- a/backoffice/config/api_router.py +++ b/backoffice/config/api_router.py @@ -4,6 +4,7 @@ from backoffice.users.api.views import UserViewSet from backoffice.workflows.api.views import ( AuthorWorkflowViewSet, + DecisionViewSet, WorkflowTicketViewSet, WorkflowViewSet, ) @@ -20,5 +21,6 @@ ) router.register("workflows", WorkflowViewSet, basename="workflows") (router.register("workflow-ticket", WorkflowTicketViewSet, basename="workflow-ticket"),) +router.register("decisions", DecisionViewSet, basename="decisions") app_name = "api" urlpatterns = router.urls diff --git a/workflows/__init__.py b/workflows/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/workflows/dags/author/author_create/author_create_approved.py b/workflows/dags/author/author_create/author_create_approved.py index 12ff699bf..fa7d715b4 100644 --- a/workflows/dags/author/author_create/author_create_approved.py +++ b/workflows/dags/author/author_create/author_create_approved.py @@ -4,6 +4,7 @@ from airflow.decorators import dag, task from airflow.models.param import Param from airflow.utils.trigger_rule import TriggerRule +from author.author_create.shared_tasks import create_decision_on_curation_choice from hooks.backoffice.workflow_management_hook import AUTHORS, WorkflowManagementHook from hooks.backoffice.workflow_ticket_management_hook import ( WorkflowTicketManagementHook, @@ -68,8 +69,6 @@ def author_check_approval_branch(**context: dict) -> None: """ if context["params"]["create_ticket"]: return "create_author_create_curation_ticket" - else: - return "empty_task" @task def create_author_create_curation_ticket(**context: dict) -> None: @@ -138,11 +137,6 @@ def set_author_create_workflow_status_to_completed(**context: dict) -> None: collection=AUTHORS, ) - @task - def empty_task() -> None: - # Logic to combine the results of branches - pass - @task() def set_author_create_workflow_status_to_error(**context: dict) -> None: ti = context["ti"] @@ -165,24 +159,9 @@ def set_author_create_workflow_status_to_error(**context: dict) -> None: set_author_create_workflow_status_to_completed() ) set_workflow_status_to_error_task = set_author_create_workflow_status_to_error() - combine_ticket_and_no_ticket_task = empty_task() + create_decision_on_curation_choice_task = create_decision_on_curation_choice() # task dependencies - ticket_branch = create_author_create_curation_ticket_task - ( - ticket_branch - >> close_author_create_user_ticket_task - >> set_workflow_status_to_completed_task - ) - - no_ticket_branch = combine_ticket_and_no_ticket_task - ( - no_ticket_branch - >> close_author_create_user_ticket_task - >> set_workflow_status_to_completed_task - ) - - author_check_approval_branch_task >> [ticket_branch, no_ticket_branch] ( set_status_to_running_task >> create_author_on_inspire_task @@ -192,6 +171,16 @@ def set_author_create_workflow_status_to_error(**context: dict) -> None: author_check_approval_branch_task, set_workflow_status_to_error_task, ] + ( + [ + author_check_approval_branch_task + >> create_author_create_curation_ticket_task, + author_check_approval_branch_task, + ] + >> close_author_create_user_ticket_task + >> create_decision_on_curation_choice_task + >> set_workflow_status_to_completed_task + ) author_create_approved_dag() diff --git a/workflows/dags/author/author_create/author_create_rejected.py b/workflows/dags/author/author_create/author_create_rejected.py index 9751856e6..5bd3419d0 100644 --- a/workflows/dags/author/author_create/author_create_rejected.py +++ b/workflows/dags/author/author_create/author_create_rejected.py @@ -2,6 +2,9 @@ from airflow.decorators import dag, task from airflow.models.param import Param + +# from .shared_tasks +from author.author_create.shared_tasks import create_decision_on_curation_choice from hooks.backoffice.workflow_management_hook import AUTHORS, WorkflowManagementHook from hooks.backoffice.workflow_ticket_management_hook import ( WorkflowTicketManagementHook, @@ -67,9 +70,15 @@ def set_workflow_status_to_running(**context): set_status_to_running_task = set_workflow_status_to_running() close_ticket_task = close_author_create_user_ticket() set_status_completed_task = set_author_create_workflow_status_to_completed() + create_decision_on_curation_choice_task = create_decision_on_curation_choice() # task dependencies - set_status_to_running_task >> close_ticket_task >> set_status_completed_task + ( + set_status_to_running_task + >> close_ticket_task + >> create_decision_on_curation_choice_task + >> set_status_completed_task + ) author_create_rejected_dag() diff --git a/workflows/dags/process_until_breakpoint.py b/workflows/dags/author/author_create/process_until_breakpoint.py similarity index 100% rename from workflows/dags/process_until_breakpoint.py rename to workflows/dags/author/author_create/process_until_breakpoint.py diff --git a/workflows/dags/author/author_create/shared_tasks.py b/workflows/dags/author/author_create/shared_tasks.py new file mode 100644 index 000000000..544cca60c --- /dev/null +++ b/workflows/dags/author/author_create/shared_tasks.py @@ -0,0 +1,12 @@ +from airflow.decorators import task +from hooks.backoffice.base import BackofficeHook + + +@task() +def create_decision_on_curation_choice(**context): + data = { + "action": context["params"]["data"]["value"], + "workflow_id": context["params"]["workflow_id"], + } + + BackofficeHook().request(method="POST", data=data, endpoint="api/decisions/") diff --git a/workflows/plugins/hooks/backoffice/base.py b/workflows/plugins/hooks/backoffice/base.py index 7a266e30f..e443d4b20 100644 --- a/workflows/plugins/hooks/backoffice/base.py +++ b/workflows/plugins/hooks/backoffice/base.py @@ -58,3 +58,11 @@ def run( prepped_request = session.prepare_request(req) self.log.info("Sending '%s' to url: %s", method, url) return self.run_and_check(session, prepped_request, extra_options) + + def request(self, method, data, endpoint): + return self.run_with_advanced_retry( + _retry_args=self.tenacity_retry_kwargs, + method=method, + data=data, + endpoint=endpoint, + ) diff --git a/workflows/plugins/hooks/backoffice/decision_management_hook.py b/workflows/plugins/hooks/backoffice/decision_management_hook.py new file mode 100644 index 000000000..67c6274de --- /dev/null +++ b/workflows/plugins/hooks/backoffice/decision_management_hook.py @@ -0,0 +1,38 @@ +from hooks.backoffice.base import BackofficeHook +from requests import Response + + +class WorkflowTicketManagementHook(BackofficeHook): + """ + A hook to update the status of a workflow in the backoffice system. + + :param method: The HTTP method to use for the request (default: "GET"). + :type method: str + :param http_conn_id: The ID of the HTTP connection to use ( + default: "backoffice_conn"). + :type http_conn_id: str + """ + + def __init__( + self, + method: str = "GET", + http_conn_id: str = "backoffice_conn", + headers: dict = None, + ) -> None: + super().__init__(method, http_conn_id, headers) + self.endpoint = "api/decision/" + + def create_decision_entry( + self, workflow_id: str, user_id: str, action: str + ) -> Response: + data = { + "user_id": user_id, + "action": action, + "workflow_id": workflow_id, + } + return self.run_with_advanced_retry( + _retry_args=self.tenacity_retry_kwargs, + method="POST", + data=data, + endpoint=self.endpoint, + )