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,27 +9,28 @@ 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__'
widgets={
widgets = {
'organization': forms.Select(attrs={'style': 'width:750px'})
}
}
def download_csv(modeladmin, request, queryset):
name = queryset[0].__class__.__name__
name = queryset[0].__class__.__name__
response = HttpResponse()
response['Content-Disposition'] = "attachment; filename="+name+"_table.csv"
first_row = []
@ -40,12 +41,14 @@ def download_csv(modeladmin, request, queryset):
writer = csv.writer(response)
writer.writerow(first_row)
for dic in queryset.values():
temp_list = (list(dic.values()))
temp_list = (list(dic.values()))
writer.writerow(temp_list)
return response
download_csv.short_description = 'export (.csv)'
class PeopleResourceInline(admin.TabularInline):
autocomplete_fields = ['resource']
model = PeopleResource
@ -64,12 +67,13 @@ 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': (
('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'),
('contact_email', 'email_2', 'skype', 'professional_phone'),
'organization',
@ -81,7 +85,7 @@ class PersonAdmin(admin.ModelAdmin):
('Research Scale', {
'classes': ('collapse',),
'fields': (('field_site', 'transect', 'mountain_top', 'mountain_range', 'landscape', 'regional', 'national',),
'_global'),
'_global'),
}),
('Methods and tools', {
'classes': ('collapse',),
@ -107,7 +111,7 @@ class PersonAdmin(admin.ModelAdmin):
list_display = ['id', 'full_name', 'organization']
#autocomplete_fields = ['organization']
form = autocomplete_fields_form # adjust width of autocomplete_fields organization
form = autocomplete_fields_form # adjust width of autocomplete_fields organization
list_per_page = settings.ADMIN_LIST_PER_PAGE
ordering = ['full_name']
@ -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,15 +280,17 @@ 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': (
'org_num1',
('acronym', 'category', 'subject'),
'country',
('organisation_english','tel', 'email', 'url',),
('organisation_english', 'tel', 'email', 'url',),
('organisation_2', 'organisation_3'),
'organisation_original',
)
@ -275,8 +305,8 @@ class OrganizationAdmin(admin.ModelAdmin):
('Other details', {
'classes': ('collapse',),
'fields': (
'tags',
),
'tags',
),
})
)
ordering = ['organisation_english']
@ -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