Skip to content

Commit

Permalink
Merge pull request #360 from salopensource/install_count_fix
Browse files Browse the repository at this point in the history
Annotate the install counts onto the queryset vs. count method.
  • Loading branch information
sheagcraig committed Dec 6, 2019
2 parents 62697eb + c4b8408 commit 3ba01cf
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions inventory/views.py
Expand Up @@ -7,7 +7,7 @@
from urllib.parse import quote

# Django
from django.db.models import Q
from django.db.models import Q, Count
from django.http import HttpResponse, HttpResponseNotFound
from django.shortcuts import get_object_or_404
from django.urls import reverse
Expand Down Expand Up @@ -260,13 +260,8 @@ def get_context_data(self, **kwargs):

class ApplicationList(Datatable):

# Specifying no source means we cannot sort on this column; however
# the source value would be the total number of inventoryitem
# records, NOT the number returned by the get_install_count
# processor which filters by group_type. Without greatly increasing
# the processing time for the view, we therefore cannot sort by
# install count.
install_count = DisplayColumn("Install Count", source=None, processor='get_install_count')
install_count = DisplayColumn(
"Install Count", source='install_count', processor='get_install_count')

class Meta:
columns = ['name', 'bundleid', 'bundlename', 'install_count']
Expand All @@ -283,16 +278,14 @@ def link_to_detail(self, instance, **kwargs):

def get_install_count(self, instance, **kwargs):
"""Get the number of app installs filtered by access group"""
queryset = kwargs['view'].filter_queryset_by_group(instance.inventoryitem_set)
count = queryset.count()

# Build a link to InventoryListView for install count badge.
link_kwargs = copy.copy(kwargs['view'].kwargs)
link_kwargs['application_id'] = instance.pk
url = reverse("inventory_list", kwargs=link_kwargs)

# Build the link.
anchor = '<a href="{}"><span class="badge">{}</span></a>'.format(url, count)
anchor = '<a href="{}"><span class="badge">{}</span></a>'.format(
url, instance.install_count)
return anchor


Expand Down Expand Up @@ -338,7 +331,8 @@ def get_queryset(self):
if crufty_pattern:
queryset = queryset.exclude(bundleid__regex=crufty_pattern)

return queryset
# Annotate the install counts in.
return queryset.annotate(install_count=Count('inventoryitem'))

def get_context_data(self, **kwargs):
context = super(ApplicationListView, self).get_context_data(**kwargs)
Expand Down

0 comments on commit 3ba01cf

Please sign in to comment.