Skip to content

Commit

Permalink
Merge pull request #1306 from zefciu/RAS-271
Browse files Browse the repository at this point in the history
Moved property setting to a separate function
  • Loading branch information
ar4s committed Feb 9, 2015
2 parents 0c0f4d3 + b0bb9eb commit 3fb2c03
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/ralph/cmdb/api.py
Expand Up @@ -17,7 +17,6 @@
from ralph.cmdb.monkey import method_check
import tastypie
from tastypie.resources import Resource
Resource.method_check = method_check

from django.conf import settings
from django.contrib.auth.models import User
Expand Down Expand Up @@ -51,6 +50,7 @@
from ralph.cmdb.models_ci import CIOwner, CIOwnershipType
from ralph.cmdb.util import breadth_first_search_ci, can_change_ci_state

Resource.method_check = method_check

THROTTLE_AT = settings.API_THROTTLING['throttle_at']
TIMEFRAME = settings.API_THROTTLING['timeframe']
Expand Down
6 changes: 4 additions & 2 deletions src/ralph/cmdb/importer.py
Expand Up @@ -42,10 +42,8 @@
from __future__ import unicode_literals

import os
os.environ['DJANGO_SETTINGS_MODULE'] = "ralph.settings"

import logging
logger = logging.getLogger(__name__)

from django.contrib.contenttypes.models import ContentType

Expand All @@ -57,6 +55,10 @@
from lck.django.common import nested_commit_on_success


os.environ['DJANGO_SETTINGS_MODULE'] = "ralph.settings"
logger = logging.getLogger(__name__)


def get_layers_for_ci_type(ci_type_id):
try:
ci_type = cdb.CIType.objects.get(pk=ci_type_id)
Expand Down
4 changes: 3 additions & 1 deletion src/ralph/cmdb/integration/lib/jira.py
Expand Up @@ -6,11 +6,13 @@
from django.utils import simplejson as json

import logging
logger = logging.getLogger(__name__)

from ralph.cmdb.integration.exceptions import IssueTrackerException


logger = logging.getLogger(__name__)


class Jira(object):

def __init__(self):
Expand Down
24 changes: 24 additions & 0 deletions src/ralph/discovery/models_device.py
Expand Up @@ -925,6 +925,30 @@ def save(self, sync_fields=True, *args, **kwargs):
# the reason (for more, see the source code of SavePrioritized).
return super(Device, self).save(*args, **kwargs)

def set_property(self, symbol, value, user):
from ralph.business.models import RoleProperty, RolePropertyValue
try:
p = self.venture_role.roleproperty_set.get(symbol=symbol)
except RoleProperty.DoesNotExist:
p = self.venture.roleproperty_set.get(symbol=symbol)
if value != p.default and not {value, p.default} == {None, ''}:
pv, created = RolePropertyValue.concurrent_get_or_create(
property=p,
device=self,
)
pv.value = value
pv.save(user=user)
else:
try:
pv = RolePropertyValue.objects.get(
property=p,
device=self,
)
except RolePropertyValue.DoesNotExist:
pass
else:
pv.delete()

def get_asset(self):
asset = None
if self.id and 'ralph_assets' in settings.INSTALLED_APPS:
Expand Down
30 changes: 29 additions & 1 deletion src/ralph/discovery/tests/test_models.py
Expand Up @@ -19,8 +19,12 @@
from ralph.discovery.tests.util import DeviceFactory, IPAddressFactory
from ralph.discovery.models import DeviceType, Device, UptimeSupport
from ralph.discovery.models_history import HistoryChange
from ralph.ui.tests.global_utils import UserFactory
from ralph.util.models import fields_synced_signal, ChangeTuple
from ralph.util.tests.utils import UserFactory
from ralph.util.tests.utils import (
UserFactory, RolePropertyTypeFactory, RolePropertyFactory,
VentureRoleFactory,
)
from ralph_assets.models import Orientation
from ralph_assets.tests.utils.assets import DCAssetFactory, DeviceInfoFactory

Expand Down Expand Up @@ -184,3 +188,27 @@ def test_orientation_property(self):
self.assertEqual(dev_2.orientation, 'middle')
with self.assertRaises(AttributeError):
dev_2.orientation = Orientation.back.id


class DevicePropertiesTest(TestCase):

def setUp(self):
sample_role = VentureRoleFactory()
RolePropertyFactory(
symbol='my_custom_property_1',
type=RolePropertyTypeFactory(symbol='STRING'),
role=sample_role,
)
self.sample_device = DeviceFactory(
venture=sample_role.venture, venture_role=sample_role)
self.sample_user = UserFactory()

def test_successful_save(self):
self.sample_device.set_property(
symbol='my_custom_property_1', value='Test 123',
user=self.sample_user,
)
self.assertEqual(
self.sample_device.get_property_set(),
{'my_custom_property_1': 'Test 123'}
)
24 changes: 1 addition & 23 deletions src/ralph/ui/views/common.py
Expand Up @@ -55,8 +55,6 @@
from ralph.scan.models import ScanSummary
from ralph.scan.util import get_pending_changes, update_scan_summary
from ralph.business.models import (
RoleProperty,
RolePropertyValue,
Venture,
VentureRole,
)
Expand Down Expand Up @@ -659,27 +657,7 @@ def get(self, *args, **kwargs):

def save_properties(self, device, properties):
for symbol, value in properties.iteritems():
try:
p = device.venture_role.roleproperty_set.get(symbol=symbol)
except RoleProperty.DoesNotExist:
p = device.venture.roleproperty_set.get(symbol=symbol)
if value != p.default and not {value, p.default} == {None, ''}:
pv, created = RolePropertyValue.concurrent_get_or_create(
property=p,
device=device,
)
pv.value = value
pv.save(user=self.request.user)
else:
try:
pv = RolePropertyValue.objects.get(
property=p,
device=device,
)
except RolePropertyValue.DoesNotExist:
pass
else:
pv.delete()
device.set_property(symbol, value, self.request.user)

def get_property_form(self, data=None):
if not self.object.venture_role:
Expand Down
10 changes: 7 additions & 3 deletions src/ralph/ui/views/racks.py
Expand Up @@ -201,11 +201,15 @@ def sort_tree(self, query, sort):
depth, item = top.pop()
c = children[item]
if sort in ('-position', 'position'):
key = lambda x: (x.get_position() or '').rjust(100)
def key(x):
return (x.get_position() or '').rjust(100)
c.sort(key=key, reverse=not sort.startswith('-'))
elif sort == '':
key = lambda x: (x.model.type if x.model else None,
(x.get_position() or '').rjust(100))
def key(x):
return (
x.model.type if x.model else None,
(x.get_position() or '').rjust(100)
)
c.sort(key=key, reverse=True)
top.extend((depth + 1, i) for i in c)
item.depth = depth
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/util/models.py
Expand Up @@ -18,6 +18,7 @@

from ralph.settings import SYNC_FIELD_MIXIN_NOTIFICATIONS_WHITELIST

from django.contrib.auth.tests import models as auth_test_models
ChangeTuple = namedtuple('ChangeTuple', ['field', 'old_value', 'new_value'])


Expand All @@ -34,7 +35,6 @@ def create_api_key_ignore_dberrors(*args, **kwargs):

# workaround for a unit test bug in Django 1.4.x

from django.contrib.auth.tests import models as auth_test_models
del auth_test_models.ProfileTestCase.test_site_profile_not_available

# signal used by SyncFieldMixin for sending notifications on changed fields
Expand Down
15 changes: 14 additions & 1 deletion src/ralph/util/tests/utils.py
Expand Up @@ -10,7 +10,9 @@
from factory.django import DjangoModelFactory

from ralph.account.models import Region
from ralph.business.models import Venture, VentureRole
from ralph.business.models import (
Venture, VentureRole, RolePropertyType, RoleProperty,
)
from ralph.cmdb import models_ci
from ralph.cmdb.tests.utils import CIFactory
from ralph.ui.tests.global_utils import UserFactory
Expand Down Expand Up @@ -95,3 +97,14 @@ class VentureRoleFactory(DjangoModelFactory):
FACTORY_FOR = VentureRole
name = factory.Sequence(lambda n: 'Venture role #{}'.format(n))
venture = factory.SubFactory(VentureFactory)


class RolePropertyTypeFactory(DjangoModelFactory):
FACTORY_FOR = RolePropertyType
symbol = factory.Sequence(lambda n: 'property_type_{}'.format(n))


class RolePropertyFactory(DjangoModelFactory):
FACTORY_FOR = RoleProperty
symbol = factory.Sequence(lambda n: 'property_{}'.format(n))
type = factory.SubFactory(RolePropertyTypeFactory)

0 comments on commit 3fb2c03

Please sign in to comment.