Skip to content

Commit

Permalink
update the activity stream for inventories
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Layani <[email protected]>
  • Loading branch information
Avilir committed Dec 5, 2023
1 parent 90c48fc commit 65c540d
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions awx/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2262,28 +2262,35 @@ def validate(self, attrs):
errors[inv.name] = "Hosts can only be deleted from manual inventories."
if errors != {}:
raise serializers.ValidationError({"inventories": errors})

attrs['inventories'] = inv_list
return attrs

def delete(self, validated_data):
result = {"hosts": dict()}
changes = {'deleted_hosts': list()}
changes = {'deleted_hosts': dict()}
for inventory in validated_data['inventories']:
changes[inventory] = list()

for host in validated_data['hosts_data']:
result["hosts"][host["id"]] = f"The host {host['name']} was deleted"
changes['deleted_hosts'].append({"host_id": host["id"], "host_name": host["name"]})
changes['deleted_hosts'][host["inventory_id"]].append({"host_id": host["id"], "host_name": host["name"]})

try:
validated_data['host_qs'].delete()
except Exception as e:
raise serializers.ValidationError({"detail": _(f"cannot delete hosts, host deletion error {e}")})

request = self.context.get('request', None)
ActivityStream.objects.create(
operation='delete',
object1='inventory',
changes=json.dumps(changes),
actor=request.user,
)

for inventory in validated_data['inventories']:
activity_entry = ActivityStream.objects.create(
operation='update',
object1='inventory',
changes=json.dumps(changes['deleted_hosts'][inventory]),
actor=request.user,
)
activity_entry.inventory.add(inventory)

return result


Expand Down

0 comments on commit 65c540d

Please sign in to comment.