Skip to content

Commit

Permalink
Merge pull request #1337 from dmm-com/bugfix/es_update_on_entry_delete
Browse files Browse the repository at this point in the history
Fix Elasticsearch update on entry deletion
  • Loading branch information
hinashi authored Dec 5, 2024
2 parents 1df8ae3 + a423440 commit 00c6594
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 8 deletions.
10 changes: 6 additions & 4 deletions entry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1990,14 +1990,16 @@ def _set_attrinfo(
elif entity_attr.type & AttrType.GROUP:
group = attrv.group
if group:
attrinfo["value"] = truncate(group.name)
attrinfo["referral_id"] = group.id
if group.is_active:
attrinfo["value"] = truncate(group.name)
attrinfo["referral_id"] = group.id

elif entity_attr.type & AttrType.ROLE:
role = attrv.role
if role:
attrinfo["value"] = truncate(role.name)
attrinfo["referral_id"] = role.id
if role.is_active:
attrinfo["value"] = truncate(role.name)
attrinfo["referral_id"] = role.id

# Basically register attribute information whatever value doesn't exist
if not (entity_attr.type & AttrType._ARRAY and not is_recursive):
Expand Down
18 changes: 16 additions & 2 deletions group/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def save(self, *args, **kwargs) -> None:
return super(Group, self).save(*args, **kwargs)

def delete(self):
from airone.lib import auto_complement
from job.models import Job, JobOperation
from user.models import User

"""
Override Model.delete method of Django
"""
Expand All @@ -41,8 +45,18 @@ def delete(self):
)
self.save()

for entry in self.get_referred_entries():
entry.register_es()
user = auto_complement.get_auto_complement_user(None)
if not user:
user = User.objects.create(username=settings.AIRONE["AUTO_COMPLEMENT_USER"])

job_register_referrals = Job.new_register_referrals(
user,
None,
operation_value=JobOperation.GROUP_REGISTER_REFERRAL.value,
params={"group_id": self.id},
)

job_register_referrals.run()

def has_permission(self, target_obj, permission_level):
"""[NOTE]
Expand Down
29 changes: 29 additions & 0 deletions group/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from unittest import mock

from django.conf import settings

from airone.lib.elasticsearch import AttrHint
from airone.lib.test import AironeTestCase
from airone.lib.types import AttrType
from entry.models import Entry
from entry.services import AdvancedSearchService
from group import tasks
from group.models import Group
from user.models import User

Expand Down Expand Up @@ -130,3 +135,27 @@ def test_max_groups(self):
# if the limit is not set, RuntimeError should not be raised
settings.MAX_GROUPS = None
Group.objects.create(name=f"group-{max_groups}")

@mock.patch(
"group.tasks.edit_group_referrals.delay", mock.Mock(side_effect=tasks.edit_group_referrals)
)
def test_delete_group(self):
testg = self._create_group("testg")
entity = self.create_entity(
user=self.user1,
name="Entity1",
attrs=[{"name": "group", "type": AttrType.GROUP}],
)

self.add_entry(
self.user1,
"e-1",
entity,
values={"group": testg},
)
testg.delete()

resp1 = AdvancedSearchService.search_entries(
self.user1, [entity.id], [AttrHint(name="group")]
)
self.assertEqual(resp1.ret_values[0].attrs["group"]["value"]["name"], "")
18 changes: 16 additions & 2 deletions role/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def save(self, *args, **kwargs) -> None:
return super(Role, self).save(*args, **kwargs)

def delete(self):
from airone.lib import auto_complement
from job.models import Job, JobOperation
from user.models import User

"""
Override Model.delete method of Django
"""
Expand All @@ -109,8 +113,18 @@ def delete(self):
)
self.save(update_fields=["is_active", "name"])

for entry in self.get_referred_entries():
entry.register_es()
user = auto_complement.get_auto_complement_user(None)
if not user:
user = User.objects.create(username=settings.AIRONE["AUTO_COMPLEMENT_USER"])

job_register_referrals = Job.new_register_referrals(
user,
None,
operation_value=JobOperation.ROLE_REGISTER_REFERRAL.value,
params={"role_id": self.id},
)

job_register_referrals.run()

def get_current_permission(self, aclbase) -> int:
permissions = [x for x in self.permissions.all() if x.get_objid() == aclbase.id]
Expand Down
29 changes: 29 additions & 0 deletions role/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from unittest import mock

from django.conf import settings

from airone.lib.acl import ACLType
from airone.lib.elasticsearch import AttrHint
from airone.lib.types import AttrType
from entity.models import Entity
from entry.services import AdvancedSearchService
from group.models import Group
from role import tasks
from role.models import Role

from .base import RoleTestBase
Expand Down Expand Up @@ -204,3 +209,27 @@ def test_max_roles(self):
# if the limit is not set, RuntimeError should not be raised
settings.MAX_ROLES = None
Role.objects.create(name=f"role-{max_roles}")

@mock.patch(
"role.tasks.edit_role_referrals.delay", mock.Mock(side_effect=tasks.edit_role_referrals)
)
def test_es_update_on_entry_delete(self):
user = self.users["userA"]
entity = self.create_entity(
**{
"user": user,
"name": "entity",
"attrs": [
{
"name": "role",
"type": AttrType.ROLE,
}
],
}
)

self.add_entry(user, "e-1", entity, values={"role": self.role})
self.role.delete()

resp1 = AdvancedSearchService.search_entries(user, [entity.id], [AttrHint(name="role")])
self.assertEqual(resp1.ret_values[0].attrs["role"]["value"]["name"], "")

0 comments on commit 00c6594

Please sign in to comment.