-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add optional Front Image, Rear Image, and Image Count columns to Device Type and Module Type tables #22706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add optional Front Image, Rear Image, and Image Count columns to Device Type and Module Type tables #22706
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,3 +69,6 @@ yarn-error.log* | |
| /dist/ | ||
| /build/ | ||
| *.egg-info/ | ||
|
|
||
| # Ruff's locally generated cache | ||
| .ruff_cache | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -722,6 +722,10 @@ class DeviceTypeFilterSet(PrimaryModelFilterSet): | |
| method='_inventory_items', | ||
| label=_('Has inventory items'), | ||
| ) | ||
| has_images = django_filters.BooleanFilter( | ||
| method='_has_images', | ||
| label=('Has images'), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need _( internationalization here |
||
| ) | ||
|
|
||
| class Meta: | ||
| model = DeviceType | ||
|
|
@@ -794,6 +798,9 @@ def _device_bays(self, queryset, name, value): | |
| def _inventory_items(self, queryset, name, value): | ||
| return queryset.exclude(inventoryitemtemplates__isnull=value) | ||
|
|
||
| def _has_images(self, queryset, name, value): | ||
| return queryset.exclude(images__isnull=value) | ||
|
|
||
|
|
||
| @register_filterset | ||
| class ModuleTypeProfileFilterSet(PrimaryModelFilterSet): | ||
|
|
@@ -866,6 +873,10 @@ class ModuleTypeFilterSet(AttributeFiltersMixin, PrimaryModelFilterSet): | |
| method='_module_bays', | ||
| label=_('Has module bays'), | ||
| ) | ||
| has_images = django_filters.BooleanFilter( | ||
| method='_has_images', | ||
| label=_('Has images'), | ||
| ) | ||
|
|
||
| class Meta: | ||
| model = ModuleType | ||
|
|
@@ -919,6 +930,9 @@ def _pass_through_ports(self, queryset, name, value): | |
| def _module_bays(self, queryset, name, value): | ||
| return queryset.exclude(modulebaytemplates__isnull=value) | ||
|
|
||
| def _has_images(self, queryset, name, value): | ||
| return queryset.exclude(images__isnull=value) | ||
|
|
||
|
|
||
| class DeviceTypeComponentFilterSet(django_filters.FilterSet): | ||
| q = django_filters.CharFilter( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| from django.contrib.contenttypes.models import ContentType | ||
| from django.core.paginator import EmptyPage, PageNotAnInteger | ||
| from django.db import router, transaction | ||
| from django.db.models import Func, IntegerField, Prefetch | ||
| from django.db.models import Count, Func, IntegerField, Prefetch | ||
| from django.forms import ModelMultipleChoiceField, MultipleHiddenInput, modelformset_factory | ||
| from django.shortcuts import get_object_or_404, redirect, render | ||
| from django.urls import reverse | ||
|
|
@@ -1409,6 +1409,22 @@ class DeviceTypeListView(generic.ObjectListView): | |
| filterset_form = forms.DeviceTypeFilterForm | ||
| table = tables.DeviceTypeTable | ||
|
|
||
| def get_table(self, data, request, bulk_actions=True): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is duplicated below, can we extract this out into a common place, maybe an ImageCountAnnotationMixin to keep it DRY |
||
| # Figures out which columns the user has picked before building the tables, | ||
| # because configure() tries to apply an existing sort, that may include image_count | ||
| # and it needs the existing annotation from the queryset. | ||
| if request.user.is_authenticated: | ||
| selected_columns = request.user.config.get(f'tables.{self.table.__name__}.columns') | ||
| else: | ||
| selected_columns = None | ||
| if selected_columns is None: | ||
| selected_columns = self.table.Meta.default_columns | ||
|
|
||
| if 'image_count' in selected_columns: | ||
| data = data.annotate(image_count=Count('images')) | ||
|
|
||
| return super().get_table(data, request, bulk_actions) | ||
|
|
||
|
|
||
| @register_model_view(DeviceType) | ||
| class DeviceTypeView(GetRelatedModelsMixin, generic.ObjectView): | ||
|
|
@@ -1759,6 +1775,22 @@ class ModuleTypeListView(generic.ObjectListView): | |
| filterset_form = forms.ModuleTypeFilterForm | ||
| table = tables.ModuleTypeTable | ||
|
|
||
| def get_table(self, data, request, bulk_actions=True): | ||
| # Figures out which columns the user has picked before building the tables, | ||
| # because configure() tries to apply an existing sort, that may include image_count | ||
| # and it needs the existing annotation from the queryset. | ||
| if request.user.is_authenticated: | ||
| selected_columns = request.user.config.get(f'tables.{self.table.__name__}.columns') | ||
| else: | ||
| selected_columns = None | ||
| if selected_columns is None: | ||
| selected_columns = self.table.Meta.default_columns | ||
|
|
||
| if 'image_count' in selected_columns: | ||
| data = data.annotate(image_count=Count('images')) | ||
|
|
||
| return super().get_table(data, request, bulk_actions) | ||
|
|
||
|
|
||
| @register_model_view(ModuleType) | ||
| class ModuleTypeView(GetRelatedModelsMixin, generic.ObjectView): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is out of scope for the PR, can we remove this