-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add route to admin api to bulk delete objects
- Loading branch information
1 parent
4ac83d3
commit b825de9
Showing
9 changed files
with
234 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Mixins for Joanie Core app.""" | ||
# flake8: noqa | ||
# pylint: disable=wildcard-import | ||
|
||
from .admin import * |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Mixins for admin api | ||
""" | ||
|
||
from django.db import IntegrityError | ||
|
||
from rest_framework import mixins | ||
from rest_framework.response import Response | ||
|
||
|
||
class BulkDeleteMixin(mixins.DestroyModelMixin): | ||
""" | ||
Mixin for classes that need to be able to bulk delete | ||
""" | ||
|
||
def delete(self, request, *args, **kwargs): | ||
""" | ||
Performs a deletion. Handles cases where an id is present | ||
in the url (single deletion), if no id is present in url | ||
and ids are in the body, performs bulk delete | ||
""" | ||
if self.kwargs.get("id", None): | ||
return super().delete(request, *args, **kwargs) | ||
|
||
id_to_delete = request.data.get("id", []) | ||
elements_to_delete = self.queryset.filter(id__in=id_to_delete) | ||
(deleted_elements, error_elements) = self.perform_bulk_delete( | ||
elements_to_delete | ||
) | ||
|
||
response = {} | ||
if len(deleted_elements) > 0: | ||
response["deleted"] = deleted_elements | ||
if len(error_elements) > 0: | ||
response["error"] = error_elements | ||
|
||
return Response(response) | ||
|
||
def perform_bulk_delete(self, instances): | ||
""" | ||
Bulk deletion of model | ||
data = {"id": [<id_0>, <id_1>]} | ||
""" | ||
error_elements = [] | ||
deleted_elements = [] | ||
for element in instances.all(): | ||
try: | ||
deleted_element_id = element.id | ||
element.delete() | ||
deleted_elements.append(deleted_element_id) | ||
except IntegrityError as error: | ||
error_elements.append( | ||
{ | ||
"id": element.id, | ||
"error": str(error), | ||
} | ||
) | ||
|
||
return (deleted_elements, error_elements) |
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