Skip to content

Commit

Permalink
Add new columns (configuration path, service environment) to operation (
Browse files Browse the repository at this point in the history
#3204)

Add new columns (configuration path, service environment) to operation
  • Loading branch information
ar4s committed Jan 4, 2018
1 parent 4130ebd commit afe1b5f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/ralph/admin/filters.py
Expand Up @@ -379,8 +379,8 @@ def queryset(self, request, queryset):
except ValueError:
_add_incorrect_value_message(request, self.title)
raise IncorrectLookupParameters()

return queryset
# distinct for m2m
return queryset.distinct()

def get_related_url(self):
return reverse(
Expand Down
62 changes: 58 additions & 4 deletions src/ralph/operations/admin.py
@@ -1,9 +1,13 @@
# -*- coding: utf-8 -*-
from collections import Counter

from django.db.models import Prefetch
from django.utils.translation import ugettext_lazy as _

from ralph.admin import RalphAdmin, RalphMPTTAdmin, register
from ralph.admin.mixins import RalphAdminForm
from ralph.admin.views.main import RalphChangeList
from ralph.assets.models import BaseObject
from ralph.attachments.admin import AttachmentsMixin
from ralph.data_importer import resources
from ralph.operations.filters import StatusFilter
Expand Down Expand Up @@ -64,12 +68,62 @@ def __init__(self, *args, **kwargs):
)


class ServiceEnvironmentAndConfigurationPathMixin(object):
def get_list_display(self, request):
list_display = super().get_list_display(request)
return list_display + ['get_services', 'get_configuration_path']

def _get_related_objects(self, obj, field):
if not obj.base_objects:
return '-'
objects = Counter([
str(getattr(base_object, field))
for base_object in obj.base_objects.all()
])
return '<br>'.join([
'{}: {}'.format(name, count)
for name, count in objects.most_common()
])

def get_services(self, obj):
return self._get_related_objects(obj, 'service_env')
get_services.allow_tags = True
get_services.short_description = _('services')

def get_configuration_path(self, obj):
return self._get_related_objects(obj=obj, field='configuration_path')
get_configuration_path.allow_tags = True
get_configuration_path.short_description = _('configuration path')

def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.prefetch_related(
Prefetch(
'base_objects',
queryset=BaseObject.objects.distinct().select_related(
'configuration_path',
'configuration_path__module',
'service_env',
'service_env__service',
'service_env__environment',
)
)
)
return qs


@register(Operation)
class OperationAdmin(AttachmentsMixin, RalphAdmin):
class OperationAdmin(
AttachmentsMixin,
ServiceEnvironmentAndConfigurationPathMixin,
RalphAdmin
):
search_fields = ['title', 'description', 'ticket_id']
list_filter = ['type', ('status', StatusFilter), 'reporter', 'assignee',
'created_date', 'update_date', 'resolved_date',
'base_objects']
list_filter = [
'type', ('status', StatusFilter), 'reporter', 'assignee',
'created_date', 'update_date', 'resolved_date', 'base_objects',
'base_objects__service_env', 'base_objects__configuration_path'
]
list_display = ['title', 'type', 'created_date', 'status', 'reporter',
'get_ticket_url']
list_select_related = ('reporter', 'type', 'status')
Expand Down
11 changes: 11 additions & 0 deletions src/ralph/operations/tests/factories.py
Expand Up @@ -3,6 +3,7 @@
from factory.django import DjangoModelFactory

from ralph.accounts.tests.factories import UserFactory
from ralph.data_center.tests.factories import DataCenterAssetFactory
from ralph.operations.models import (
Change,
Failure,
Expand Down Expand Up @@ -50,6 +51,16 @@ class OperationFactory(DjangoModelFactory):
class Meta:
model = Operation

@factory.post_generation
def base_objects(self, create, extracted, **kwargs):
if not create:
return
if not extracted:
extracted = [DataCenterAssetFactory() for i in range(2)]
if extracted:
for obj in extracted:
self.base_objects.add(obj)


class ChangeFactory(OperationFactory):
class Meta:
Expand Down
16 changes: 16 additions & 0 deletions src/ralph/operations/tests/test_admin.py
@@ -0,0 +1,16 @@
from django.core.urlresolvers import reverse
from django.test import TestCase

from ralph.operations.models import OperationType
from ralph.operations.tests.factories import OperationFactory

from ralph.tests.mixins import ClientMixin


class OperationAdminViewTest(ClientMixin, TestCase):
def test_operation_changelist_should_run_2_queries(self):
OperationFactory.create_batch(5)
with self.assertNumQueries(2):
self.client.get(
reverse('admin:operations_operation_changelist'),
)

0 comments on commit afe1b5f

Please sign in to comment.