-
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
1664c62
commit 8a81d16
Showing
9 changed files
with
217 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,42 @@ | ||
""" | ||
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): | ||
""" | ||
Bulk deletion of model | ||
data = {"id": [<id_0>, <id_1>]} | ||
""" | ||
id_to_delete = request.data.get("id", []) | ||
elements_to_delete = self.queryset.filter(id__in=id_to_delete) | ||
error_elements = [] | ||
deleted_elements = [] | ||
for element in elements_to_delete.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), | ||
} | ||
) | ||
response = {} | ||
if len(deleted_elements) > 0: | ||
response["deleted"] = deleted_elements | ||
if len(error_elements) > 0: | ||
response["error"] = error_elements | ||
return Response(response) |
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