Enable CSV export for the 4 main tables Organisations, Persons, Resources, Mountain ranges

This commit is contained in:
asamihassan 2022-03-25 23:08:02 +05:00
parent cc66cfd4d8
commit 0136386ad0

View file

@ -1,10 +1,28 @@
from re import A
from unicodedata import name
from django.contrib import admin
from django.utils.html import format_html
from .models import *
from django.forms import TextInput
from django.conf import settings
from django.http import HttpResponse
import csv
def download_csv(modeladmin, request, queryset):
name = queryset[0].__class__.__name__
response = HttpResponse()
response['Content-Disposition'] = "attachment; filename="+name+"_table.csv"
first_row = []
for i in queryset[0]._meta.__dict__.get("fields"):
temp = str(i).split('.')
first_row.append(temp[-1])
writer = csv.writer(response)
writer.writerow(first_row)
for dic in queryset.values():
temp_list = (list(dic.values()))
writer.writerow(temp_list)
return response
class PeopleResourceInline(admin.TabularInline):
autocomplete_fields = ['resource']
model = PeopleResource
@ -67,6 +85,8 @@ class PersonAdmin(admin.ModelAdmin):
autocomplete_fields = ['organization']
list_per_page = settings.ADMIN_LIST_PER_PAGE
ordering = ['full_name']
actions = [download_csv]
# list_display_links = ['id']
@ -91,6 +111,10 @@ class ResourcePeopleInline(admin.TabularInline):
}
class ResourceAdmin(admin.ModelAdmin):
inlines = [
ResourceKeywordInline,
@ -130,6 +154,8 @@ class ResourceAdmin(admin.ModelAdmin):
}),
)
list_per_page = settings.ADMIN_LIST_PER_PAGE
actions = [download_csv]
class RangeNameTranslationInline(admin.TabularInline):
@ -185,6 +211,7 @@ class RangeAdmin(admin.ModelAdmin):
ordering = ['range_name']
list_per_page = settings.ADMIN_LIST_PER_PAGE
actions = [download_csv]
class PeopleOrganizationInline(admin.TabularInline):
model = Person
@ -225,6 +252,7 @@ class OrganizationAdmin(admin.ModelAdmin):
})
)
ordering = ['organisation_english']
actions = [download_csv]
def org_url(self, instance):
return format_html('<a href="{0}" target="_blank">{1}</a>',