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

Aggregations #57

Merged
merged 1 commit into from
Aug 1, 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
48 changes: 46 additions & 2 deletions backoffice/backoffice/workflows/api/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import logging

from django.shortcuts import get_object_or_404
from django_elasticsearch_dsl_drf.filter_backends import (
CompoundSearchFilterBackend,
DefaultOrderingFilterBackend,
FacetedSearchFilterBackend,
FilteringFilterBackend,
OrderingFilterBackend,
)
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
from opensearch_dsl import TermsFacet
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
Expand Down Expand Up @@ -150,13 +158,49 @@ def __init__(self, *args, **kwargs):
document = WorkflowDocument
serializer_class = WorkflowSerializer
pagination_class = OSStandardResultsSetPagination

filter_backends = [
DefaultOrderingFilterBackend,
CompoundSearchFilterBackend,
FacetedSearchFilterBackend,
FilteringFilterBackend,
OrderingFilterBackend,
]
search_fields = {
"workflow_type",
"status",
"is_update",
}
ordering = ["_updated_at"]

filter_fields = {"status": "status", "workflow_type": "workflow_type"}

ordering_fields = {"_updated_at": "_updated_at"}

ordering = ("-_updated_at",)

faceted_search_fields = {
"status": {
"field": "status.keyword",
"facet": TermsFacet,
"options": {
"size": 10,
"order": {
"_key": "asc",
},
},
"enabled": True,
},
"workflow_type": {
"field": "workflow_type.keyword",
"facet": TermsFacet,
"options": {
"size": 10,
"order": {
"_key": "asc",
},
},
"enabled": True,
},
}

def get_serializer_class(self):
return WorkflowDocumentSerializer
101 changes: 101 additions & 0 deletions backoffice/backoffice/workflows/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import uuid

import dateutil
import dateutil.parser
import pytest
from django.apps import apps
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -362,3 +364,102 @@ def test_restart_with_params(self):
url, json={"params": {"workflow_id": self.workflow.id}}
)
self.assertEqual(response.status_code, 200)


class TestWorkflowSearchFilterViewSet(BaseTransactionTestCase):
endpoint = "/api/workflows/search/"
reset_sequences = True
fixtures = ["backoffice/fixtures/groups.json"]

def setUp(self):
super().setUp()

Workflow.objects.create(
data={},
status=StatusChoices.APPROVAL,
core=True,
is_update=False,
workflow_type=WorkflowType.AUTHOR_CREATE,
)
Workflow.objects.create(
data={},
status=StatusChoices.RUNNING,
core=True,
is_update=False,
workflow_type=WorkflowType.AUTHOR_CREATE,
)

def test_facets(self):
self.api_client.force_authenticate(user=self.admin)

response = self.api_client.get(reverse("search:workflow-list"))

assert "_filter_status" in response.json()["facets"]
assert "_filter_workflow_type" in response.json()["facets"]

def test_search_status(self):
self.api_client.force_authenticate(user=self.admin)

url = (
reverse("search:workflow-list") + f"?search=status:{StatusChoices.RUNNING}"
)

response = self.api_client.get(url)

for item in response.json()["results"]:
print(item["status"])
assert item["status"] == StatusChoices.RUNNING

def test_search_workflow_type(self):
self.api_client.force_authenticate(user=self.admin)

url = (
reverse("search:workflow-list")
+ f"?search=workflow_type:{WorkflowType.HEP_CREATE}"
)

response = self.api_client.get(url)

for item in response.json()["results"]:
print(item["workflow_type"])
assert item["workflow_type"] == WorkflowType.HEP_CREATE

def test_filter_status(self):
self.api_client.force_authenticate(user=self.admin)

url = reverse("search:workflow-list") + f'?status="={StatusChoices.RUNNING}'

response = self.api_client.get(url)

for item in response.json()["results"]:
assert item["status"] == StatusChoices.RUNNING

def test_filter_workflow_type(self):
self.api_client.force_authenticate(user=self.admin)

url = (
reverse("search:workflow-list")
+ f'?workflow_type="={WorkflowType.AUTHOR_CREATE}'
)

response = self.api_client.get(url)

for item in response.json()["results"]:
assert item["workflow_type"] == WorkflowType.AUTHOR_CREATE

def test_ordering(self):
self.api_client.force_authenticate(user=self.admin)

base_url = reverse("search:workflow-list")

urls = [base_url, base_url + "?ordering=-_updated_at"]

for url in urls:
response = self.api_client.get(url)

previous_date = None
for item in response.json()["results"]:
cur_date = dateutil.parser.parse(item["_updated_at"])
if previous_date is not None:
assert cur_date < previous_date
previous_date = cur_date
Loading