Skip to content

Commit 5fe62ac

Browse files
committed
Finished patch-work to be able to invoke Trigger when named_object typed Attribute was changed via creating, editing and reverting processing of APIv1
1 parent ac435e3 commit 5fe62ac

File tree

3 files changed

+52
-32
lines changed

3 files changed

+52
-32
lines changed

entry/models.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2385,9 +2385,12 @@ def _get_value(attrname, attrtype, value):
23852385
return [_get_value(attrname, attrtype, x) for x in value]
23862386

23872387
elif attrtype & AttrTypeValue["named"]:
2388-
[co_value] = list(value.values())
2388+
[name, ref] = list(value.keys()) + list(value.values())
23892389

2390-
return co_value["id"] if co_value else None
2390+
return {
2391+
"id": ref["id"] if ref else None,
2392+
"name": name,
2393+
}
23912394

23922395
elif attrtype & AttrTypeValue["object"]:
23932396
return value["id"] if value else None

entry/tests/test_view.py

+44-27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import yaml
99
from django.conf import settings
10+
from django.db.models import Q
1011
from django.http import HttpResponse
1112
from django.http.response import JsonResponse
1213
from django.urls import reverse
@@ -4926,45 +4927,61 @@ def test_invoke_trigger_by_revert_attrv(self):
49264927
user,
49274928
"Entity",
49284929
[
4929-
{"name": "cond", "type": AttrTypeValue["string"]},
4930+
{"name": "cond_str", "type": AttrTypeValue["string"]},
4931+
{"name": "cond_name", "type": AttrTypeValue["named_object"]},
49304932
{"name": "action", "type": AttrTypeValue["string"]},
49314933
],
49324934
)
4933-
entry = self.add_entry(
4934-
user,
4935-
"TestEntry",
4936-
entity,
4937-
values={
4938-
"cond": "hoge",
4939-
},
4940-
)
4941-
4942-
# changed value to retrieve
4943-
changing_attr = entry.attrs.get(name="cond")
4944-
changing_attr.add_value(user, "changed")
49454935

49464936
# register TriggerAction configuration before creating an Entry
49474937
TriggerCondition.register(
49484938
entity,
4949-
[{"attr_id": entity.attrs.get(name="cond").id, "cond": "hoge"}],
4939+
[{"attr_id": entity.attrs.get(name="cond_str").id, "cond": "hoge"}],
49504940
[{"attr_id": entity.attrs.get(name="action").id, "value": "fuga"}],
49514941
)
4952-
4953-
# send request to revert attribute value of "cond"
4954-
revert_attrv = changing_attr.values.filter(value="hoge").last()
4955-
params = {"attr_id": str(changing_attr.id), "attrv_id": str(revert_attrv.id)}
4956-
resp = self.client.post(
4957-
reverse("entry:revert_attrv"), json.dumps(params), "application/json"
4942+
TriggerCondition.register(
4943+
entity,
4944+
[{"attr_id": entity.attrs.get(name="cond_name").id, "cond": "foo"}],
4945+
[{"attr_id": entity.attrs.get(name="action").id, "value": "fuga"}],
49584946
)
4959-
self.assertEqual(resp.status_code, 200)
49604947

4961-
# This check that Attribute value of "action" would be updated by TriggerAction
4962-
self.assertEqual(entry.get_attrv("action").value, "fuga")
4948+
# changed value to retrieve
4949+
testing_params = [
4950+
("cond_str", "hoge", "changed", Q(value="hoge")),
4951+
(
4952+
"cond_name",
4953+
{"name": "foo", "id": None},
4954+
{"name": "changed", "id": None},
4955+
Q(value="foo"),
4956+
),
4957+
]
4958+
for index, (attrname, initial_value, changed_value, query) in enumerate(testing_params):
4959+
entry = self.add_entry(
4960+
user,
4961+
"TestEntry",
4962+
entity,
4963+
values={
4964+
attrname: initial_value,
4965+
},
4966+
)
4967+
changing_attr = entry.attrs.get(name=attrname)
4968+
changing_attr.add_value(user, changed_value)
49634969

4964-
# check trigger action was worked properly
4965-
job_query = Job.objects.filter(operation=JobOperation.MAY_INVOKE_TRIGGER)
4966-
self.assertEqual(job_query.count(), 1)
4967-
self.assertEqual(job_query.first().status, JobStatus.DONE)
4970+
# send request to revert attribute value of "cond"
4971+
revert_attrv = changing_attr.values.filter(query).last()
4972+
params = {"attr_id": str(changing_attr.id), "attrv_id": str(revert_attrv.id)}
4973+
resp = self.client.post(
4974+
reverse("entry:revert_attrv"), json.dumps(params), "application/json"
4975+
)
4976+
self.assertEqual(resp.status_code, 200)
4977+
4978+
# This check that Attribute value of "action" would be updated by TriggerAction
4979+
self.assertEqual(entry.get_attrv("action").value, "fuga")
4980+
4981+
# check trigger action was worked properly
4982+
job_query = Job.objects.filter(operation=JobOperation.MAY_INVOKE_TRIGGER)
4983+
self.assertEqual(job_query.count(), 1 + index)
4984+
self.assertEqual(job_query.last().status, JobStatus.DONE)
49684985

49694986
def test_revert_attrv_with_invalid_value(self):
49704987
user = self.guest_login()

trigger/models.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ def get_invoked_actions(cls, entity: Entity, recv_data: list):
451451
# for naemd_object typed Attribute
452452
v = [
453453
{
454-
"index": v["index"],
454+
"index": v["index"] if v else "0",
455455
"data": {
456-
"name": r["data"],
457-
"id": v["data"],
456+
"name": r["data"] if r else "",
457+
"id": v["data"] if v else None,
458458
},
459459
}
460460
for (v, r) in itertools.zip_longest(

0 commit comments

Comments
 (0)