Skip to content

Commit

Permalink
feat: update dataset and table admin
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna committed Feb 6, 2024
1 parent 11d2a00 commit 4310507
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 58 deletions.
73 changes: 46 additions & 27 deletions bd_api/apps/api/v1/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from ordered_model.admin import OrderedInlineModelAdminMixin, OrderedStackedInline

from bd_api.apps.api.v1.filters import (
OrganizationImageFilter,
TableCoverageFilter,
TableObservationFilter,
OrganizationImageListFilter,
TableCoverageListFilter,
TableObservationListFilter,
DatasetOrganizationListFilter,
TableOrganizationListFilter,
)
from bd_api.apps.api.v1.forms import (
CloudTableInlineForm,
Expand Down Expand Up @@ -414,7 +416,7 @@ class OrganizationAdmin(TabbedTranslationAdmin):
readonly_fields = ["id", "full_slug", "created_at", "updated_at"]
list_display = ["name", "full_slug", "has_picture"]
search_fields = ["name", "slug"]
list_filter = [OrganizationImageFilter, "created_at", "updated_at"]
list_filter = [OrganizationImageListFilter, "created_at", "updated_at"]
autocomplete_fields = [
"area",
]
Expand Down Expand Up @@ -452,19 +454,11 @@ class DatasetAdmin(OrderedInlineModelAdminMixin, TabbedTranslationAdmin):
update_search_index,
rebuild_search_index,
]

def related_objects(self, obj):
return format_html(
"<a class='related-widget-wrapper-link add-related' href='/admin/v1/table/add/?dataset={0}&_to_field=id&_popup=1'>{1} {2}</a>",
obj.id,
obj.tables.count(),
" ".join(
["tables" if obj.tables.count() > 1 else "table", "(click to add)"]
),
)

related_objects.short_description = "Tables"

inlines = [
TableInline,
RawDataSourceInline,
InformationRequestInline,
]
readonly_fields = [
"id",
"full_slug",
Expand All @@ -478,20 +472,34 @@ def related_objects(self, obj):
"updated_at",
"related_objects",
]
list_display = ["name", "full_slug", "coverage", "organization", "related_objects"]
search_fields = ["name", "slug", "organization__name"]
inlines = [
TableInline,
RawDataSourceInline,
InformationRequestInline,
]
filter_horizontal = [
"tags",
"themes",
]
list_filter = [
"organization__name",
DatasetOrganizationListFilter,
]
list_display = [
"name",
"full_slug",
"coverage",
"organization",
"related_objects",
]
ordering = ["-updated_at"]

def related_objects(self, obj):
return format_html(
"<a class='related-widget-wrapper-link add-related' href='/admin/v1/table/add/?dataset={0}&_to_field=id&_popup=1'>{1} {2}</a>",
obj.id,
obj.tables.count(),
" ".join(
["tables" if obj.tables.count() > 1 else "table", "(click to add)"]
),
)

related_objects.short_description = "Tables"


class TableAdmin(OrderedInlineModelAdminMixin, TabbedTranslationAdmin):
Expand Down Expand Up @@ -524,11 +532,22 @@ class TableAdmin(OrderedInlineModelAdminMixin, TabbedTranslationAdmin):
"published_by",
"data_cleaned_by",
]
list_display = [
"name",
"dataset",
"number_columns",
"number_rows",
"uncompressed_file_size",
"page_views",
"created_at",
"updated_at",
]
list_filter = [
"dataset__organization__name",
TableCoverageFilter,
TableObservationFilter,
TableOrganizationListFilter,
TableCoverageListFilter,
TableObservationListFilter,
]
ordering = ["-updated_at"]
change_form_template = "admin/table_change_form.html"

def changeform_view(self, request, object_id=None, form_url="", extra_context=None):
Expand Down
68 changes: 37 additions & 31 deletions bd_api/apps/api/v1/filters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
from django.contrib import admin

from bd_api.apps.api.v1.models import Column, Coverage, ObservationLevel
from bd_api.apps.api.v1.models import Coverage, ObservationLevel, Organization


class OrganizationImageFilter(admin.SimpleListFilter):
title = "has_picture"
class OrganizationImageListFilter(admin.SimpleListFilter):
title = "Picture"
parameter_name = "has_picture"

def lookups(self, request, model_admin):
Expand All @@ -21,26 +21,51 @@ def queryset(self, request, queryset):
return queryset.filter(picture="")


class TableCoverageFilter(admin.SimpleListFilter):
class DatasetOrganizationListFilter(admin.SimpleListFilter):
title = "Organization"
parameter_name = "organization"

def lookups(self, request, model_admin):
values = Organization.objects.order_by("name").distinct().values("name", "pk")
return [(v.get("pk"), v.get("name")) for v in values]

def queryset(self, request, queryset):
if self.value():
return queryset.filter(organization=self.value())


class TableOrganizationListFilter(admin.SimpleListFilter):
title = "Organization"
parameter_name = "organization"

def lookups(self, request, model_admin):
values = Organization.objects.order_by("name").distinct().values("name", "pk")
return [(v.get("pk"), v.get("name")) for v in values]

def queryset(self, request, queryset):
if self.value():
return queryset.filter(dataset__organization=self.value())


class TableCoverageListFilter(admin.SimpleListFilter):
title = "Coverage"
parameter_name = "table_coverage"

def lookups(self, request, model_admin):
distinct_values = (
values = (
Coverage.objects.filter(table__id__isnull=False)
.order_by("area__name")
.distinct()
.values("area__name", "area__slug")
)
# Create a tuple of tuples with the format (value, label).
return [(value.get("area__slug"), value.get("area__name")) for value in distinct_values]
return [(v.get("area__slug"), v.get("area__name")) for v in values]

def queryset(self, request, queryset):
if self.value():
return queryset.filter(coverages__area__slug=self.value())


class TableObservationFilter(admin.SimpleListFilter):
class TableObservationListFilter(admin.SimpleListFilter):
title = "Observation Level"
parameter_name = "table_observation"

Expand All @@ -51,30 +76,11 @@ def lookups(self, request, model_admin):
.distinct()
.values("entity__id", "entity__name")
)
# Create a tuple of tuples with the format (value, label).
return [(value.get("entity__id"), value.get("entity__name")) for value in distinct_values]
return [
(value.get("entity__id"), value.get("entity__name"))
for value in distinct_values
]

def queryset(self, request, queryset):
if self.value():
return queryset.filter(observation_levels__entity=self.value())


class DirectoryPrimaryKeyAdminFilter(admin.SimpleListFilter):
title = "directory_primary_key"
parameter_name = "directory_primary_key"

def lookups(self, request, model_admin):
distinct_values = (
Column.objects.filter(directory_primary_key__id__isnull=False)
.order_by("directory_primary_key__name")
.distinct()
.values(
"directory_primary_key__name",
)
)
# Create a tuple of tuples with the format (value, label).
return [(value.get("directory_primary_key__name"),) for value in distinct_values]

def queryset(self, request, queryset):
if self.value():
return queryset.filter(directory_primary_key__name=self.value())

0 comments on commit 4310507

Please sign in to comment.