From 431050747e54a857c8bdd2811ceb5f629834a50c Mon Sep 17 00:00:00 2001 From: Vinicius Date: Mon, 5 Feb 2024 22:19:06 -0300 Subject: [PATCH] feat: update dataset and table admin --- bd_api/apps/api/v1/admin.py | 73 ++++++++++++++++++++++------------- bd_api/apps/api/v1/filters.py | 68 +++++++++++++++++--------------- 2 files changed, 83 insertions(+), 58 deletions(-) diff --git a/bd_api/apps/api/v1/admin.py b/bd_api/apps/api/v1/admin.py index 533da1d9..a2f5e837 100644 --- a/bd_api/apps/api/v1/admin.py +++ b/bd_api/apps/api/v1/admin.py @@ -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, @@ -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", ] @@ -452,19 +454,11 @@ class DatasetAdmin(OrderedInlineModelAdminMixin, TabbedTranslationAdmin): update_search_index, rebuild_search_index, ] - - def related_objects(self, obj): - return format_html( - "{1} {2}", - 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", @@ -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( + "{1} {2}", + 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): @@ -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): diff --git a/bd_api/apps/api/v1/filters.py b/bd_api/apps/api/v1/filters.py index 178aa428..a47d3f54 100644 --- a/bd_api/apps/api/v1/filters.py +++ b/bd_api/apps/api/v1/filters.py @@ -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): @@ -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" @@ -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())