resource and relevant objs del

This commit is contained in:
asamihassan 2022-09-07 23:48:58 +05:00
parent b7f8e237fa
commit 2c4fb66176

View file

@ -9,16 +9,18 @@ from django.http import HttpResponse
import csv
from django import forms
from django.contrib.admin.widgets import AutocompleteSelect
class autocomplete_fields_form(forms.ModelForm):
"""This form overrides autocomplete
organization field for Person class
"""
def __init__(self, *args, **kwargs):
super(autocomplete_fields_form, self).__init__(*args, **kwargs)
# self.fields['organization'].label = 'My new label'
class Meta:
model = Person
fields = '__all__'
@ -27,7 +29,6 @@ class autocomplete_fields_form(forms.ModelForm):
}
def download_csv(modeladmin, request, queryset):
name = queryset[0].__class__.__name__
response = HttpResponse()
@ -44,8 +45,10 @@ def download_csv(modeladmin, request, queryset):
writer.writerow(temp_list)
return response
download_csv.short_description = 'export (.csv)'
class PeopleResourceInline(admin.TabularInline):
autocomplete_fields = ['resource']
model = PeopleResource
@ -64,7 +67,8 @@ class PersonAdmin(admin.ModelAdmin):
PeopleRangeInline
]
readonly_fields = ['id']
search_fields = ['title', 'first_name', 'last_name', 'organisation', 'position', 'country__short_name', 'contact_email']
search_fields = ['title', 'first_name', 'last_name', 'organisation',
'position', 'country__short_name', 'contact_email']
fieldsets = (
(None, {
'fields': (
@ -116,8 +120,11 @@ class PersonAdmin(admin.ModelAdmin):
def get_field_queryset(self, db, db_field, request):
queryset = super().get_field_queryset(db, db_field, request)
if db_field.name == 'organization':
queryset = queryset.order_by('country','organisation_english','organisation_2','organisation_3', 'acronym')
queryset = queryset.order_by(
'country', 'organisation_english', 'organisation_2', 'organisation_3', 'acronym')
return queryset
class ResourceKeywordInline(admin.TabularInline):
autocomplete_fields = ['keyword']
model = ResourceKeyword
@ -139,10 +146,6 @@ class ResourcePeopleInline(admin.TabularInline):
}
class ResourceAdmin(admin.ModelAdmin):
inlines = [
ResourceKeywordInline,
@ -182,8 +185,32 @@ class ResourceAdmin(admin.ModelAdmin):
}),
)
list_per_page = settings.ADMIN_LIST_PER_PAGE
actions = [download_csv]
def get_actions(self, request):
actions = super().get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
def delete_resource(self, request, obj):
for o in obj.all():
resource_keyword = ResourceKeyword.objects.filter(resource=o.id)
for i in resource_keyword:
i.delete()
resource_range = ResourceRange.objects.filter(resource_title=o.id)
for i in resource_range:
i.delete()
resource_people = ResourcesPeople.objects.filter(resource=o.id)
for i in resource_people:
i.delete()
resource_people_resource = PeopleResource.objects.filter(
resource=o.id)
for i in resource_people_resource:
i.delete()
o.delete()
delete_resource.short_description = 'Delete Resource & Related Fields'
actions = [download_csv, delete_resource]
class RangeNameTranslationInline(admin.TabularInline):
@ -241,6 +268,7 @@ class RangeAdmin(admin.ModelAdmin):
actions = [download_csv]
class PeopleOrganizationInline(admin.TabularInline):
model = Person
exclude = ['source_id', 'organisation', 'field_indexer']
@ -252,8 +280,10 @@ class OrganizationAdmin(admin.ModelAdmin):
PeopleOrganizationInline,
]
readonly_fields = ('org_num1',)
search_fields = ['organisation_search', 'org_alpha_search', 'organisation_2', 'organisation_3', 'subject','organisation_english']
list_display = ['org_num1', 'organisation_english', 'organisation_2', 'country']
search_fields = ['organisation_search', 'org_alpha_search',
'organisation_2', 'organisation_3', 'subject', 'organisation_english']
list_display = ['org_num1', 'organisation_english',
'organisation_2', 'country']
fieldsets = (
(None, {
'fields': (
@ -373,7 +403,8 @@ class SpeciesAdmin(admin.ModelAdmin):
class TaxonRangeAdmin(admin.ModelAdmin):
model = TaxonRange
search_fields = ['range__range_name', 'taxon', 'subrange_or_region', 'distribution', 'source', 'remarks']
search_fields = ['range__range_name', 'taxon',
'subrange_or_region', 'distribution', 'source', 'remarks']
list_display = ['range_name', 'taxon']
def range_name(self, obj):
@ -447,6 +478,7 @@ admin.site.register(Keyword, KeywordAdmin)
# admin.site.register(Scale)
# admin.site.register(Taxon)
# admin.site.register(Field, FieldAdmin)
admin.site.disable_action('delete_selected')
# LU models