Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
global: improve admin and logging (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
drjova authored Jul 22, 2024
1 parent 9772db3 commit bce0a7a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
19 changes: 14 additions & 5 deletions backoffice/workflows/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@ def has_permission(self, request):
)


@admin.register(Workflow)
class WorkflowAdmin(admin.ModelAdmin):
"""
Admin class for Workflow model. Define get, update and delete permissions.
"""

ordering = ("-_updated_at",)
search_fields = ["id", "data"]
list_display = (
"id",
"workflow_type",
"status",
"core",
"is_update",
"_created_at",
"_updated_at",
)
list_filter = ["workflow_type", "status", "core", "is_update", "_created_at", "_updated_at"]

def has_view_permission(self, request, obj=None):
"""
Returns True if the user has permission to view the Workflow model.
Expand All @@ -53,8 +67,3 @@ def has_delete_permission(self, request, obj=None):
"""
permission_check = IsAdminOrCuratorUser()
return request.user.is_superuser or permission_check.has_permission(request, self)


admin.site.register(Workflow)
workflow_admin_site = WorkflowsAdminSite(name="backoffice_admin")
workflow_admin_site.register(Workflow, WorkflowAdmin)
8 changes: 8 additions & 0 deletions backoffice/workflows/api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from django.shortcuts import get_object_or_404
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
from rest_framework import status, viewsets
Expand All @@ -17,6 +19,8 @@
WorkflowTicketSerializer,
)

logger = logging.getLogger(__name__)


class WorkflowViewSet(viewsets.ModelViewSet):
queryset = Workflow.objects.all()
Expand Down Expand Up @@ -82,18 +86,22 @@ class AuthorWorkflowViewSet(viewsets.ViewSet):
serializer_class = WorkflowSerializer

def create(self, request):
logger.info("Creating workflow with data: %s", request.data)
serializer = self.serializer_class(data=request.data)
if serializer.is_valid(raise_exception=True):
workflow = Workflow.objects.create(
data=serializer.validated_data["data"], workflow_type=serializer.validated_data["workflow_type"]
)
logger.info("Trigger Airflow DAG: %s for %s", WORKFLOW_DAG[workflow.workflow_type], workflow.id)
return airflow_utils.trigger_airflow_dag(WORKFLOW_DAG[workflow.workflow_type], str(workflow.id), workflow.data)

@action(detail=True, methods=["post"])
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"]}
logger.info("Trigger Airflow DAG: %s for %s", ResolutionDags[serializer.validated_data["value"]], pk)
return airflow_utils.trigger_airflow_dag(
ResolutionDags[serializer.validated_data["value"]].label, pk, extra_data
)
Expand Down
7 changes: 6 additions & 1 deletion backoffice/workflows/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ def test_patch_admin(self):
workflow = Workflow.objects.filter(id=str(self.workflow.id))[0]
self.assertEqual(response.status_code, 200)
self.assertEquals(workflow.status, "approval")
self.assertEquals(workflow.data, {"test": "test"})
self.assertEquals(
workflow.data,
{
"test": "test",
},
)

def test_patch_anonymous(self):
self.api_client.force_authenticate(user=self.user)
Expand Down
12 changes: 3 additions & 9 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@
from django.views.generic import TemplateView
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from rest_framework.authtoken.views import obtain_auth_token
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)

from backoffice.workflows.admin import workflow_admin_site
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

urlpatterns = [
path("dashboard/", workflow_admin_site.urls),
path("", TemplateView.as_view(template_name="pages/home.html"), name="home"),
path("about/", TemplateView.as_view(template_name="pages/about.html"), name="about"),
# Django Admin, use {% url 'admin:index' %}
Expand All @@ -42,8 +36,8 @@
SpectacularSwaggerView.as_view(url_name="api-schema"),
name="api-docs",
),
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
]

if settings.DEBUG:
Expand Down

0 comments on commit bce0a7a

Please sign in to comment.