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,25 +9,26 @@ from django.http import HttpResponse
import csv import csv
from django import forms from django import forms
from django.contrib.admin.widgets import AutocompleteSelect from django.contrib.admin.widgets import AutocompleteSelect
class autocomplete_fields_form(forms.ModelForm): class autocomplete_fields_form(forms.ModelForm):
"""This form overrides autocomplete """This form overrides autocomplete
organization field for Person class organization field for Person class
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(autocomplete_fields_form, self).__init__(*args, **kwargs) super(autocomplete_fields_form, self).__init__(*args, **kwargs)
# self.fields['organization'].label = 'My new label' # self.fields['organization'].label = 'My new label'
class Meta: class Meta:
model = Person model = Person
fields = '__all__' fields = '__all__'
widgets={ widgets = {
'organization': forms.Select(attrs={'style': 'width:750px'}) 'organization': forms.Select(attrs={'style': 'width:750px'})
} }
def download_csv(modeladmin, request, queryset): def download_csv(modeladmin, request, queryset):
name = queryset[0].__class__.__name__ name = queryset[0].__class__.__name__
response = HttpResponse() response = HttpResponse()
@ -44,8 +45,10 @@ def download_csv(modeladmin, request, queryset):
writer.writerow(temp_list) writer.writerow(temp_list)
return response return response
download_csv.short_description = 'export (.csv)' download_csv.short_description = 'export (.csv)'
class PeopleResourceInline(admin.TabularInline): class PeopleResourceInline(admin.TabularInline):
autocomplete_fields = ['resource'] autocomplete_fields = ['resource']
model = PeopleResource model = PeopleResource
@ -64,12 +67,13 @@ class PersonAdmin(admin.ModelAdmin):
PeopleRangeInline PeopleRangeInline
] ]
readonly_fields = ['id'] 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 = ( fieldsets = (
(None, { (None, {
'fields': ( 'fields': (
('id', 'profile_on_web', 'news_letter'), ('id', 'profile_on_web', 'news_letter'),
('mr_mrs', 'title','full_name', 'position'), ('mr_mrs', 'title', 'full_name', 'position'),
('first_name', 'last_name', 'search_name', 'status'), ('first_name', 'last_name', 'search_name', 'status'),
('contact_email', 'email_2', 'skype', 'professional_phone'), ('contact_email', 'email_2', 'skype', 'professional_phone'),
'organization', 'organization',
@ -116,8 +120,11 @@ class PersonAdmin(admin.ModelAdmin):
def get_field_queryset(self, db, db_field, request): def get_field_queryset(self, db, db_field, request):
queryset = super().get_field_queryset(db, db_field, request) queryset = super().get_field_queryset(db, db_field, request)
if db_field.name == 'organization': 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 return queryset
class ResourceKeywordInline(admin.TabularInline): class ResourceKeywordInline(admin.TabularInline):
autocomplete_fields = ['keyword'] autocomplete_fields = ['keyword']
model = ResourceKeyword model = ResourceKeyword
@ -139,10 +146,6 @@ class ResourcePeopleInline(admin.TabularInline):
} }
class ResourceAdmin(admin.ModelAdmin): class ResourceAdmin(admin.ModelAdmin):
inlines = [ inlines = [
ResourceKeywordInline, ResourceKeywordInline,
@ -182,8 +185,32 @@ class ResourceAdmin(admin.ModelAdmin):
}), }),
) )
list_per_page = settings.ADMIN_LIST_PER_PAGE 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): class RangeNameTranslationInline(admin.TabularInline):
@ -241,6 +268,7 @@ class RangeAdmin(admin.ModelAdmin):
actions = [download_csv] actions = [download_csv]
class PeopleOrganizationInline(admin.TabularInline): class PeopleOrganizationInline(admin.TabularInline):
model = Person model = Person
exclude = ['source_id', 'organisation', 'field_indexer'] exclude = ['source_id', 'organisation', 'field_indexer']
@ -252,15 +280,17 @@ class OrganizationAdmin(admin.ModelAdmin):
PeopleOrganizationInline, PeopleOrganizationInline,
] ]
readonly_fields = ('org_num1',) readonly_fields = ('org_num1',)
search_fields = ['organisation_search', 'org_alpha_search', 'organisation_2', 'organisation_3', 'subject','organisation_english'] search_fields = ['organisation_search', 'org_alpha_search',
list_display = ['org_num1', 'organisation_english', 'organisation_2', 'country'] 'organisation_2', 'organisation_3', 'subject', 'organisation_english']
list_display = ['org_num1', 'organisation_english',
'organisation_2', 'country']
fieldsets = ( fieldsets = (
(None, { (None, {
'fields': ( 'fields': (
'org_num1', 'org_num1',
('acronym', 'category', 'subject'), ('acronym', 'category', 'subject'),
'country', 'country',
('organisation_english','tel', 'email', 'url',), ('organisation_english', 'tel', 'email', 'url',),
('organisation_2', 'organisation_3'), ('organisation_2', 'organisation_3'),
'organisation_original', 'organisation_original',
) )
@ -373,7 +403,8 @@ class SpeciesAdmin(admin.ModelAdmin):
class TaxonRangeAdmin(admin.ModelAdmin): class TaxonRangeAdmin(admin.ModelAdmin):
model = TaxonRange 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'] list_display = ['range_name', 'taxon']
def range_name(self, obj): def range_name(self, obj):
@ -447,6 +478,7 @@ admin.site.register(Keyword, KeywordAdmin)
# admin.site.register(Scale) # admin.site.register(Scale)
# admin.site.register(Taxon) # admin.site.register(Taxon)
# admin.site.register(Field, FieldAdmin) # admin.site.register(Field, FieldAdmin)
admin.site.disable_action('delete_selected')
# LU models # LU models