-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature delete records by source reference id (#3649)
* Add button * Feature to delete records by source reference id
- Loading branch information
1 parent
896eea6
commit 5a1aecd
Showing
8 changed files
with
323 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from django.http import ( | ||
Http404, HttpResponseServerError, HttpResponseForbidden | ||
) | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from bims.models.source_reference import ( | ||
SourceReference | ||
) | ||
from bims.models.biological_collection_record import ( | ||
BiologicalCollectionRecord | ||
) | ||
from bims.models.chemical_record import ( | ||
ChemicalRecord | ||
) | ||
from bims.models.decision_support_tool import DecisionSupportTool | ||
|
||
|
||
class DeleteRecordsByReferenceId(APIView): | ||
""" | ||
API endpoint for deleting BiologicalCollectionRecord and ChemicalRecord | ||
instances associated with a given SourceReference ID. | ||
""" | ||
|
||
def post(self, request, *args, **kwargs): | ||
if not request.user.is_superuser: | ||
return HttpResponseForbidden( | ||
'Only superusers are allowed to perform this action.' | ||
) | ||
|
||
source_reference_id = kwargs.get('source_reference_id') | ||
if not source_reference_id: | ||
raise Http404('Missing id') | ||
|
||
try: | ||
source_reference = get_object_or_404( | ||
SourceReference, | ||
pk=source_reference_id | ||
) | ||
messages = [] | ||
bio_records = BiologicalCollectionRecord.objects.filter(source_reference_id=source_reference_id) | ||
if bio_records.exists(): | ||
DecisionSupportTool.objects.filter( | ||
biological_collection_record__id__in=list(bio_records.values_list('id', flat=True)) | ||
).delete() | ||
BiologicalCollectionRecord.objects.filter(source_reference_id=source_reference_id).delete() | ||
messages.append( | ||
'BiologicalCollectionRecord successfully deleted' | ||
) | ||
else: | ||
messages.append("No BiologicalCollectionRecord found for the given reference ID.") | ||
|
||
if ChemicalRecord.objects.filter(source_reference_id=source_reference_id).exists(): | ||
ChemicalRecord.objects.filter(source_reference_id=source_reference_id).delete() | ||
messages.append( | ||
'ChemicalRecord successfully deleted' | ||
) | ||
else: | ||
messages.append("No ChemicalRecord found for the given reference ID.") | ||
|
||
return Response( | ||
{'message': messages}, | ||
status=status.HTTP_200_OK) | ||
|
||
except Exception as e: | ||
# In case of any other error, return a 500 Internal Server Error | ||
return HttpResponseServerError(f'An error occurred: {e}') |
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,59 @@ | ||
.card { | ||
margin-bottom: 10px; | ||
} | ||
|
||
.custom-control-label { | ||
max-width: 80%; | ||
} | ||
|
||
.reference-filter-title { | ||
margin-bottom: 0 !important; | ||
font-weight: bold; | ||
} | ||
.badge-occurrences { | ||
font-size: 9pt; | ||
padding-left: 10px; | ||
padding-right: 10px; | ||
padding-top: 8px; | ||
padding-bottom: 8px; | ||
} | ||
.badge-zero { | ||
background-color: #b8b8b8; | ||
} | ||
.reference-type-label { | ||
text-transform: uppercase; | ||
font-weight: 300; | ||
font-size: 10pt; | ||
} | ||
.active.active:hover { | ||
color: white; | ||
} | ||
.input-group-append:hover { | ||
cursor: pointer; | ||
} | ||
.loading-container { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
background: rgba(255, 255, 255, 0.8); | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
z-index: 9999; | ||
} | ||
|
||
.loader { | ||
border: 5px solid #f3f3f3; | ||
border-top: 5px solid #3498db; | ||
border-radius: 50%; | ||
width: 50px; | ||
height: 50px; | ||
animation: spin 2s linear infinite; | ||
} | ||
|
||
@keyframes spin { | ||
0% { transform: rotate(0deg); } | ||
100% { transform: rotate(360deg); } | ||
} |
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,76 @@ | ||
$('.copy-document-id').click(function (e) { | ||
let documentId = window.location.origin + '/documents/' + $(e.target).data('document-id'); | ||
/* Copy the text inside the text field */ | ||
navigator.clipboard.writeText(documentId); | ||
/* Alert the copied text */ | ||
alert("Copied the text: " + documentId); | ||
}) | ||
|
||
$('.custom-control-input').change(function (e) { | ||
let $parent = $(e.target).parent().parent(); | ||
let filterName = $parent.data('filter'); | ||
let checkedVals = $parent.find(':checkbox:checked').map(function () { | ||
return this.value; | ||
}).get(); | ||
insertParam(filterName, checkedVals); | ||
}) | ||
|
||
$('.input-group-append').click(function (e) { | ||
let value = $('#search-input').val(); | ||
insertParam('q', value); | ||
}) | ||
|
||
$('.delete-reference').click(function (e) { | ||
e.preventDefault(); | ||
const referenceId = $(e.target).data('reference-id'); | ||
let r = confirm("Are you sure you want to remove this reference?"); | ||
if (r === true) { | ||
$.ajax({ | ||
url: "/delete-source-reference/", | ||
headers: {"X-CSRFToken": csrfToken}, | ||
type: 'POST', | ||
data: { | ||
'reference_id': referenceId | ||
}, | ||
success: function (res) { | ||
location.reload(); | ||
} | ||
}); | ||
} | ||
}) | ||
|
||
$('.delete-records').click(function (e) { | ||
e.preventDefault(); | ||
const referenceId = $(e.target).data('reference-id'); | ||
const totalRecords = $(e.target).data('total-records'); | ||
const totalPhysico = $(e.target).data('total-physico-chemical-data'); | ||
let r = confirm( | ||
`Are you sure you want to remove ${totalRecords} records and ${totalPhysico} | ||
Physico-chemical data associated with this source reference?`); | ||
if (r) { | ||
document.getElementById('loading-animation').style.display = 'flex'; | ||
console.log('called'); | ||
$.ajax({ | ||
url: `/api/delete-records-by-source-reference-id/${referenceId}/`, | ||
type: 'POST', | ||
headers: {"X-CSRFToken": csrfToken}, | ||
success: function(response) { | ||
window.location.reload(); | ||
}, | ||
error: function(xhr, textStatus, errorThrown) { | ||
alert('Error: ' + errorThrown); | ||
document.getElementById('loading-animation').style.display = 'none'; | ||
} | ||
}); | ||
} | ||
|
||
}) | ||
|
||
|
||
$('.apply-author-filter').click(function(e) { | ||
const $target = $(e.target).parent(); | ||
const authorIds = $target.find('.author_result').map(function () { | ||
return $(this).data("author-id"); | ||
}).get(); | ||
insertParam('collectors', authorIds.join(',')) | ||
}) |
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
Oops, something went wrong.