-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Provider): add soft deletion for providers and related resources (…
- Loading branch information
Showing
7 changed files
with
124 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,46 @@ | ||
from celery.utils.log import get_task_logger | ||
from django.db import transaction | ||
|
||
from api.db_utils import batch_delete | ||
from api.models import Finding, Provider, Resource, Scan | ||
|
||
logger = get_task_logger(__name__) | ||
|
||
|
||
def delete_instance(model, pk: str): | ||
def delete_provider(pk: str): | ||
""" | ||
Deletes an instance of the specified model. | ||
This function retrieves an instance of the provided model using its primary key | ||
and deletes it from the database. | ||
Gracefully deletes an instance of a provider along with its related data. | ||
Args: | ||
model (Model): The Django model class from which to delete an instance. | ||
pk (str): The primary key of the instance to delete. | ||
pk (str): The primary key of the Provider instance to delete. | ||
Returns: | ||
tuple: A tuple containing the number of objects deleted and a dictionary | ||
with the count of deleted objects per model, | ||
including related models if applicable. | ||
dict: A dictionary with the count of deleted objects per model, | ||
including related models. | ||
Raises: | ||
model.DoesNotExist: If no instance with the provided primary key exists. | ||
Provider.DoesNotExist: If no instance with the provided primary key exists. | ||
""" | ||
return model.objects.get(pk=pk).delete() | ||
instance = Provider.all_objects.get(pk=pk) | ||
deletion_summary = {} | ||
|
||
with transaction.atomic(): | ||
# Delete Findings | ||
findings_qs = Finding.all_objects.filter(scan__provider=instance) | ||
_, findings_summary = batch_delete(findings_qs) | ||
deletion_summary.update(findings_summary) | ||
|
||
# Delete Resources | ||
resources_qs = Resource.all_objects.filter(provider=instance) | ||
_, resources_summary = batch_delete(resources_qs) | ||
deletion_summary.update(resources_summary) | ||
|
||
# Delete Scans | ||
scans_qs = Scan.all_objects.filter(provider=instance) | ||
_, scans_summary = batch_delete(scans_qs) | ||
deletion_summary.update(scans_summary) | ||
|
||
provider_deleted_count, provider_summary = instance.delete() | ||
deletion_summary.update(provider_summary) | ||
|
||
return deletion_summary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters