Skip to content

Commit

Permalink
Merge pull request #1054 from mkurek/feature/profit-center
Browse files Browse the repository at this point in the history
Feature/profit center
  • Loading branch information
xor-xor committed Sep 8, 2014
2 parents 694549a + b621209 commit 73632a1
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 13 deletions.
14 changes: 13 additions & 1 deletion src/ralph/cmdb/fixtures/initial_data.yaml
Expand Up @@ -67,6 +67,12 @@
name: Environment
icon_class: fugue-tree

- model: cmdb.CIType
pk: 12
fields:
name: ProfitCenter
icon_class: fugue-money-coin

# Attribute

- model: cmdb.CIAttribute
Expand All @@ -90,14 +96,20 @@
attribute_type: 3
ci_types: [2]


- model: cmdb.CIAttribute
pk: 4
fields:
name: SLA value
attribute_type: 4
ci_types: [2,1]

- model: cmdb.CIAttribute
pk: 5
fields:
name: Description
attribute_type: 2
ci_types: [12]

# Layers

- model: cmdb.CILayer
Expand Down
8 changes: 7 additions & 1 deletion src/ralph/cmdb/migrations/0024_bumped_id_ci_type.py
Expand Up @@ -49,7 +49,13 @@ def forwards(self, orm):
db.commit_transaction()

def backwards(self, orm):
"""Backwards migrations not needed"""
"""Remove new ci types"""
db.start_transaction()
db.execute('DELETE FROM cmdb_cilayer_connected_types WHERE citype_id > 10 and citype_id < 1000;')
db.execute('DELETE FROM cmdb_ciattribute_ci_types WHERE citype_id > 10 and citype_id < 1000;')
db.execute('DELETE FROM cmdb_ci WHERE type_id > 10 and type_id < 1000;')
db.execute('DELETE FROM cmdb_citype WHERE id > 10 and id < 1000;')
db.commit_transaction()

models = {
'auth.group': {
Expand Down
487 changes: 487 additions & 0 deletions src/ralph/cmdb/migrations/0025_bumped_id_ci_attribute.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/ralph/cmdb/models_ci.py
Expand Up @@ -71,6 +71,7 @@ class CI_TYPES(Choices):
DATACENTER = _('Data Center')
NETWORKTERMINATOR = _('Network Terminator')
ENVIRONMENT = _('Environment')
PROFIT_CENTER = _('Profit Center')


contenttype_mappings = {
Expand Down Expand Up @@ -314,7 +315,7 @@ class CI(TimeTrackable):
)
barcode = models.CharField(
verbose_name=_("barcode"), max_length=255, unique=True, null=True,
default=None,
default=None, blank=True,
)
content_type = models.ForeignKey(
ContentType, verbose_name=_("content type"), null=True, blank=True,
Expand Down
34 changes: 29 additions & 5 deletions src/ralph/util/api_pricing.py
Expand Up @@ -11,7 +11,7 @@
from django.db import models as db

from ralph.business.models import Venture, VentureExtraCost
from ralph.cmdb.models import CI, CIOwner, CIType
from ralph.cmdb.models import CI, CIAttributeValue, CIOwner, CIType
from ralph.discovery.models import (
Device,
DeviceEnvironment,
Expand Down Expand Up @@ -338,6 +338,30 @@ def get_business_lines():
}


def get_profit_centers():
"""
Returns Profit Centers from CMDB (CIs with type Profit Center)
"""
profit_center_type = CIType.objects.get(name='ProfitCenter')
business_line_type = CIType.objects.get(name='BusinessLine')
for profit_center in CI.objects.filter(type=profit_center_type):
try:
description = profit_center.ciattributevalue_set.get(
attribute__name='description'
).value
except CIAttributeValue.DoesNotExist:
description = None
business_line = profit_center.child.filter(
parent__type=business_line_type
).values_list('parent__uid', flat=True)
yield {
'ci_uid': profit_center.uid,
'name': profit_center.name,
'description': description,
'business_line': business_line[0] if business_line else None,
}


def get_owners():
"""
Returns CIOwners from CMDB
Expand All @@ -358,17 +382,17 @@ def get_services():
owners, business line etc.
"""
service_type = CIType.objects.get(name='Service')
business_line_type = CIType.objects.get(name='BusinessLine')
profit_center_type = CIType.objects.get(name='ProfitCenter')
for service in CI.objects.filter(
type=service_type
).select_related('relations'):
business_line = service.child.filter(
parent__type=business_line_type
profit_center = service.child.filter(
parent__type=profit_center_type
).values_list('parent__uid', flat=True)
yield {
'ci_uid': service.uid,
'name': service.name,
'business_line': business_line[0] if business_line else None,
'profit_center': profit_center[0] if profit_center else None,
'business_owners': list(service.business_owners.values_list(
'id',
flat=True,
Expand Down
10 changes: 5 additions & 5 deletions src/ralph/util/tests/tests.py
Expand Up @@ -214,9 +214,9 @@ def test_get_owners(self):

def test_get_services(self):
service = utils.ServiceFactory()
business_line = utils.BusinessLineFactory()
profit_center = utils.ProfitCenterFactory()
utils.ServiceBusinessLineRelationFactory(
parent=business_line,
parent=profit_center,
child=service,
)
business_ownership = utils.ServiceOwnershipFactory.create_batch(
Expand All @@ -233,19 +233,19 @@ def test_get_services(self):
service_dict = {
'name': service.name,
'ci_uid': service.uid,
'business_line': business_line.uid,
'profit_center': profit_center.uid,
'business_owners': [bo.owner.id for bo in business_ownership],
'technical_owners': [to.owner.id for to in technical_ownership],
}
self.assertEquals(result, [service_dict])

def test_get_services_without_business_line(self):
def test_get_services_without_profit_center(self):
service = utils.ServiceFactory()
result = [a for a in api_pricing.get_services()]
service_dict = {
'name': service.name,
'ci_uid': service.uid,
'business_line': None,
'profit_center': None,
'technical_owners': [],
'business_owners': [],
}
Expand Down
6 changes: 6 additions & 0 deletions src/ralph/util/tests/utils.py
Expand Up @@ -19,6 +19,12 @@ def type(self):
return models_ci.CIType.objects.get(name='BusinessLine')


class ProfitCenterFactory(CIFactory):
@factory.lazy_attribute
def type(self):
return models_ci.CIType.objects.get(name='ProfitCenter')


class ServiceFactory(CIFactory):
@factory.lazy_attribute
def type(self):
Expand Down

0 comments on commit 73632a1

Please sign in to comment.