person deletion method overload

This commit is contained in:
asamihassan 2022-10-04 19:21:57 +05:00
parent 3b30b9666a
commit 44bab85ba7

View file

@ -9,6 +9,7 @@ from django.http import HttpResponse
import csv
from django import forms
from django.contrib.admin.widgets import AutocompleteSelect
from django.contrib import messages # import messages
class autocomplete_fields_form(forms.ModelForm):
@ -124,6 +125,81 @@ class PersonAdmin(admin.ModelAdmin):
'country', 'organisation_english', 'organisation_2', 'organisation_3', 'acronym')
return queryset
def get_actions(self, request):
actions = super().get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
def delete_model(self, request, obj):
self.delete_person(request, obj)
return super(PersonAdmin, self).delete_model(request, obj)
def delete_person(self, request, obj):
taxa_people = TaxaPeople.objects.filter(person=obj.id)
for i in taxa_people:
i.delete()
ranges_people = RangesPeople.objects.filter(person=obj.id)
for i in ranges_people:
i.delete()
resources_people = ResourcesPeople.objects.filter(person=obj.id)
for i in resources_people:
i.delete()
scales_people = ScalesPeople.objects.filter(person=obj.id)
for i in scales_people:
i.delete()
people_function = PeopleFunction.objects.filter(person=obj.id)
for i in people_function:
i.delete()
people_range = PeopleRange.objects.filter(person=obj.id)
for i in people_range:
i.delete()
people_resources = PeopleResource.objects.filter(person=obj.id)
for i in people_resources:
i.delete()
fields_people = FieldsPeople.objects.filter(person=obj.id)
for i in fields_people:
i.delete()
methods_people = MethodsPeople.objects.filter(person=obj.id)
for i in methods_people:
i.delete()
def delete_persons(self, request, obj):
for o in obj.all():
taxa_people = TaxaPeople.objects.filter(person=o.id)
for i in taxa_people:
i.delete()
ranges_people = RangesPeople.objects.filter(person=o.id)
for i in ranges_people:
i.delete()
resources_people = ResourcesPeople.objects.filter(person=o.id)
for i in resources_people:
i.delete()
scales_people = ScalesPeople.objects.filter(person=o.id)
for i in scales_people:
i.delete()
people_function = PeopleFunction.objects.filter(person=o.id)
for i in people_function:
i.delete()
people_range = PeopleRange.objects.filter(person=o.id)
for i in people_range:
i.delete()
people_resources = PeopleResource.objects.filter(person=o.id)
for i in people_resources:
i.delete()
fields_people = FieldsPeople.objects.filter(person=o.id)
for i in fields_people:
i.delete()
methods_people = MethodsPeople.objects.filter(person=o.id)
for i in methods_people:
i.delete()
o.delete()
messages.success(request, "Successfully deleted")
delete_persons.short_description = 'Delete Person & Related Fields'
actions = [download_csv, delete_persons]
class ResourceKeywordInline(admin.TabularInline):
autocomplete_fields = ['keyword']
@ -192,7 +268,26 @@ class ResourceAdmin(admin.ModelAdmin):
del actions['delete_selected']
return actions
def delete_model(self, request, obj):
self.delete_resource(request, obj)
return super(ResourceAdmin, self).delete_model(request, obj)
def delete_resource(self, request, obj):
resource_keyword = ResourceKeyword.objects.filter(resource=obj.id)
for i in resource_keyword:
i.delete()
resource_range = ResourceRange.objects.filter(resource_title=obj.id)
for i in resource_range:
i.delete()
resource_people = ResourcesPeople.objects.filter(resource=obj.id)
for i in resource_people:
i.delete()
resource_people_resource = PeopleResource.objects.filter(
resource=obj.id)
for i in resource_people_resource:
i.delete()
def delete_resources(self, request, obj):
for o in obj.all():
resource_keyword = ResourceKeyword.objects.filter(resource=o.id)
for i in resource_keyword:
@ -208,9 +303,10 @@ class ResourceAdmin(admin.ModelAdmin):
for i in resource_people_resource:
i.delete()
o.delete()
delete_resource.short_description = 'Delete Resource & Related Fields'
messages.success(request, "Successfully deleted")
delete_resources.short_description = 'Delete Resource & Related Fields'
actions = [download_csv, delete_resource]
actions = [download_csv, delete_resources]
class RangeNameTranslationInline(admin.TabularInline):