Enable CSV export for the 4 main tables Organisations, Persons, Resources, Mountain ranges
This commit is contained in:
parent
cc66cfd4d8
commit
0136386ad0
1 changed files with 28 additions and 0 deletions
28
app/admin.py
28
app/admin.py
|
@ -1,10 +1,28 @@
|
||||||
|
from re import A
|
||||||
|
from unicodedata import name
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
from .models import *
|
from .models import *
|
||||||
from django.forms import TextInput
|
from django.forms import TextInput
|
||||||
from django.conf import settings
|
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):
|
class PeopleResourceInline(admin.TabularInline):
|
||||||
autocomplete_fields = ['resource']
|
autocomplete_fields = ['resource']
|
||||||
model = PeopleResource
|
model = PeopleResource
|
||||||
|
@ -67,6 +85,8 @@ class PersonAdmin(admin.ModelAdmin):
|
||||||
autocomplete_fields = ['organization']
|
autocomplete_fields = ['organization']
|
||||||
list_per_page = settings.ADMIN_LIST_PER_PAGE
|
list_per_page = settings.ADMIN_LIST_PER_PAGE
|
||||||
ordering = ['full_name']
|
ordering = ['full_name']
|
||||||
|
actions = [download_csv]
|
||||||
|
|
||||||
# list_display_links = ['id']
|
# list_display_links = ['id']
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,6 +111,10 @@ class ResourcePeopleInline(admin.TabularInline):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ResourceAdmin(admin.ModelAdmin):
|
class ResourceAdmin(admin.ModelAdmin):
|
||||||
inlines = [
|
inlines = [
|
||||||
ResourceKeywordInline,
|
ResourceKeywordInline,
|
||||||
|
@ -130,6 +154,8 @@ class ResourceAdmin(admin.ModelAdmin):
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
list_per_page = settings.ADMIN_LIST_PER_PAGE
|
list_per_page = settings.ADMIN_LIST_PER_PAGE
|
||||||
|
actions = [download_csv]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class RangeNameTranslationInline(admin.TabularInline):
|
class RangeNameTranslationInline(admin.TabularInline):
|
||||||
|
@ -185,6 +211,7 @@ class RangeAdmin(admin.ModelAdmin):
|
||||||
ordering = ['range_name']
|
ordering = ['range_name']
|
||||||
list_per_page = settings.ADMIN_LIST_PER_PAGE
|
list_per_page = settings.ADMIN_LIST_PER_PAGE
|
||||||
|
|
||||||
|
actions = [download_csv]
|
||||||
|
|
||||||
class PeopleOrganizationInline(admin.TabularInline):
|
class PeopleOrganizationInline(admin.TabularInline):
|
||||||
model = Person
|
model = Person
|
||||||
|
@ -225,6 +252,7 @@ class OrganizationAdmin(admin.ModelAdmin):
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
ordering = ['organisation_english']
|
ordering = ['organisation_english']
|
||||||
|
actions = [download_csv]
|
||||||
|
|
||||||
def org_url(self, instance):
|
def org_url(self, instance):
|
||||||
return format_html('<a href="{0}" target="_blank">{1}</a>',
|
return format_html('<a href="{0}" target="_blank">{1}</a>',
|
||||||
|
|
Loading…
Reference in a new issue