Skip to content

Commit

Permalink
fix: message api filters for protocol and occurrences
Browse files Browse the repository at this point in the history
KK-1108.
Re-add the protocol and occurrences filters that were earlier removed,
because they were hard to fix (during the upgrade process).
  • Loading branch information
nikomakela committed Aug 20, 2024
1 parent 105a15b commit 97bd17c
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 4 deletions.
64 changes: 63 additions & 1 deletion messaging/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from copy import deepcopy

import django_filters
import graphene
from django.apps import apps
from django.db import transaction
Expand All @@ -10,6 +11,7 @@

from common.schema import LanguageEnum
from common.utils import (
get_node_id_from_global_id,
get_obj_if_user_can_administer,
map_enums_to_values_in_kwargs,
project_user_required,
Expand Down Expand Up @@ -46,6 +48,66 @@ class RecipientSelectionEnum(graphene.Enum):
)


class MessageFilter(django_filters.FilterSet):
occurrences = django_filters.ModelMultipleChoiceFilter(
queryset=Occurrence.objects.all(),
conjoined=False,
method="filter_occurrences",
label="Occurrences",
help_text="Filter by multiple occurrences.",
)

def __init__(self, data=None, *args, **kwargs):
"""
Initializes the MessageFilter instance and processes occurrence global IDs.
Args:
data (dict, optional): The filter data.
*args: Additional positional arguments.
**kwargs: Additional keyword arguments.
"""
if data:
data = self._fix_occurrences_global_ids(data)
super().__init__(data, *args, **kwargs)

class Meta:
model = Message
fields = ["project_id", "protocol", "occurrences"]

def filter_occurrences(self, qs, name, value):
if value:
return qs.filter(occurrences__in=value)
return qs

def _fix_occurrences_global_ids(self, data):
"""
Extracts and converts occurrence global IDs to their corresponding node IDs.
Args:
data (dict): The filter data containing the 'occurrences' key.
Returns:
dict: The updated filter data with 'occurrences'
replaced by a list of global node IDs.
"""
value = data.pop("occurrences", None)
occurrences_node_ids = None
occurrences_ids = None

if isinstance(value, (list, tuple)):
occurrences_node_ids = value
elif isinstance(value, str):
occurrences_node_ids = [value]

if occurrences_node_ids:
occurrences_ids = [
get_node_id_from_global_id(node_id, "OccurrenceNode")
for node_id in occurrences_node_ids
]

return {**data, "occurrences": occurrences_ids}


class MessageNode(DjangoObjectType):
subject = graphene.String()
body_text = graphene.String()
Expand All @@ -69,7 +131,7 @@ class Meta:
"recipient_count",
"translations",
)
filter_fields = ("project_id",)
filterset_class = MessageFilter

@classmethod
@project_user_required
Expand Down
225 changes: 225 additions & 0 deletions messaging/tests/snapshots/snap_test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,83 @@
}
}

snapshots["test_messages_query_occurrences_filter 1"] = {
"data": {
"messages": {
"edges": [
{
"node": {
"bodyText": "Free heart significant machine try. President compare room hotel town south among. Fall long respond draw military dog. Increase thank certainly again thought summer. Beyond than trial western.",
"event": None,
"occurrences": {
"edges": [
{"node": {"time": "1980-08-23T04:46:09.363315+00:00"}}
]
},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Him question stay.",
}
},
{
"node": {
"bodyText": "Child care any. Minute defense level church. Alone our very television beat at success.",
"event": None,
"occurrences": {
"edges": [
{"node": {"time": "1980-08-23T04:46:09.363315+00:00"}}
]
},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Business hot PM.",
}
},
{
"node": {
"bodyText": "Focus significant kind. Laugh smile behavior whom gas. Significant minute rest. Special far magazine.",
"event": None,
"occurrences": {
"edges": [
{"node": {"time": "2003-01-02T18:10:46.571751+00:00"}}
]
},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Attention practice.",
}
},
{
"node": {
"bodyText": "Conference carry factor front Mr amount conference thing. Positive cold start rest tonight including believe. Respond range bit college question. Stop treatment suggest. Sometimes growth check court.",
"event": None,
"occurrences": {
"edges": [
{"node": {"time": "2003-01-02T18:10:46.571751+00:00"}}
]
},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Past life thus.",
}
},
]
}
}
}

snapshots["test_messages_query_project_filter 1"] = {
"data": {
"messages": {
Expand All @@ -148,6 +225,154 @@
}
}

snapshots["test_messages_query_protocol_filter[email] 1"] = {
"data": {
"messages": {
"edges": [
{
"node": {
"bodyText": "Which president smile staff country actually generation. Age member whatever open effort clear. Difficult look can. Care figure mention wrong when lead involve. Event lay yes policy data control as receive.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Partner exist true.",
}
},
{
"node": {
"bodyText": "Girl middle same space speak. Person the probably deep center develop character situation. Score think turn argue present.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Decade every town.",
}
},
{
"node": {
"bodyText": "Voice radio happen color scene. Create state rock only. Several behavior media career decide season mission TV. Work head table city central deep response. Through resource professional debate produce college able.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Walk fish teach.",
}
},
{
"node": {
"bodyText": "Sea something western research. Candidate century network bar hear quite wonder. Up always sport return. Light a point charge stand store. Generation able take food share.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Product issue along.",
}
},
{
"node": {
"bodyText": "Site chance of performance. Hand cause receive kitchen middle. Step able last in able local garden. Nearly gun two born land military first.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "EMAIL",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Discussion remain.",
}
},
]
}
}
}

snapshots["test_messages_query_protocol_filter[sms] 1"] = {
"data": {
"messages": {
"edges": [
{
"node": {
"bodyText": "Free heart significant machine try. President compare room hotel town south among. Fall long respond draw military dog. Increase thank certainly again thought summer. Beyond than trial western.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "SMS",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Him question stay.",
}
},
{
"node": {
"bodyText": "Child care any. Minute defense level church. Alone our very television beat at success.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "SMS",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Business hot PM.",
}
},
{
"node": {
"bodyText": "Enter everything history remember stay public high. Exist shoulder write century. Never skill down subject town. According hard enough watch condition like lay.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "SMS",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Detail.",
}
},
{
"node": {
"bodyText": "From daughter order stay sign discover eight. Toward scientist service wonder everything. Middle moment strong hand push book and interesting. Near increase process truth list pressure. Capital city sing himself yard stuff.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "SMS",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Their tell.",
}
},
{
"node": {
"bodyText": "Base may middle good father boy economy. Fly discussion huge get this success. Science sort already name. Senior number scene today friend maintain marriage.",
"event": None,
"occurrences": {"edges": []},
"project": {"year": 2020},
"protocol": "SMS",
"recipientCount": 0,
"recipientSelection": "ALL",
"sentAt": None,
"subject": "Even perhaps that.",
}
},
]
}
}
}

snapshots["test_send_message 1"] = {
"data": {
"sendMessage": {
Expand Down
Loading

0 comments on commit 97bd17c

Please sign in to comment.