Permalink
Browse files

Merge pull request #191 from wmatyskiewicz/dev

fixed github issue #183
  • Loading branch information...
2 parents 57df837 + fc2442a commit 0b38d62ebe9bfbfbca6bcfd27e3af25955fa9199 @ambv ambv committed Oct 29, 2012
Showing with 109 additions and 40 deletions.
  1. +17 −16 src/ralph/ui/tests.py
  2. +2 −0 src/ralph/ui/urls.py
  3. +2 −2 src/ralph/ui/views/catalog.py
  4. +88 −22 src/ralph/ui/views/racks.py
View
@@ -58,6 +58,7 @@
'OS': '__Linux*'
}
+
class TestSearch(TestCase):
"""
TODO:
@@ -209,32 +210,32 @@ def test_access_to_device(self):
def test_name_field_old(self):
#Search objects in response.context
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
self.assertEqual(context.name, self.device.name)
def test_address_or_network_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
#test ip
self.assertEqual(context.name, self.device.name)
ip = context.ipaddress_set.filter(address=DEVICE['ip']).count()
- self.assertTrue(ip>0)
+ self.assertTrue(ip > 0)
#test network
"""
FIXME - i can`t tests network, i need more info !
"""
def test_remarks_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
self.assertEqual(context.remarks, DEVICE['remarks'])
def test_venture_or_role_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
self.assertEqual(self.device.venture.name, DEVICE['venture'])
self.assertEqual(context.venture.name, DEVICE['venture'])
@@ -243,41 +244,41 @@ def test_venture_or_role_field(self):
def test_model_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
self.assertEqual(self.device.model.type, DeviceType.unknown)
self.assertEqual(context.model.type, DeviceType.unknown)
self.assertEqual(context.model.name, DEVICE['model_name'])
def test_component_or_software_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
processor = context.processor_set.filter(device=self.device.id).count()
- self.assertTrue(processor>0)
+ self.assertTrue(processor > 0)
def test_serial_number_mac_or_wwn_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
self.assertEqual(self.device.sn, context.sn)
mac = context.ethernet_set.filter(mac=DEVICE['mac']).count()
- self.assertTrue(mac>0)
+ self.assertTrue(mac > 0)
diskshare = context.diskshare_set.filter(device=self.device.id).count()
- self.assertTrue(diskshare>0)
+ self.assertTrue(diskshare > 0)
dsm = context.disksharemount_set.filter(device=self.device.id).count()
- self.assertTrue(dsm>0)
+ self.assertTrue(dsm > 0)
def test_barcode_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
self.assertEqual(self.device.barcode, DEVICE['barcode'])
self.assertEqual(context.barcode, DEVICE['barcode'])
def test_datacenter_rack_or_position_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
self.assertEqual(self.device.position, DEVICE['position'])
self.assertEqual(context.position, DEVICE['position'])
@@ -288,7 +289,7 @@ def test_datacenter_rack_or_position_field(self):
def test_history_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
db_query = HistoryChange.objects.get(
device=self.device, new_value=DEVICE['name'],
@@ -303,7 +304,7 @@ def test_history_field(self):
def test_device_type_field(self):
url = '/ui/search/info/%s' % self.device.id
- device_search = self.client.get(url,follow=True)
+ device_search = self.client.get(url, follow=True)
context = device_search.context['object']
self.assertEqual(self.device.model.type, DeviceType.unknown.id)
self.assertEqual(context.model.type, DeviceType.unknown)
View
@@ -6,6 +6,7 @@
from django.conf.urls.defaults import patterns, url
from django.contrib.auth.decorators import login_required
+from django.views.generic.simple import redirect_to
from ralph.cmdb.views import Search as SearchCmdb
@@ -116,6 +117,7 @@
url(r'^racks/$',
login_required(RacksDeviceList.as_view()), {}, 'racks'),
+ url(r'^racks/-/rack/$', redirect_to, {'url': '/ui/racks/-/info/'}),
url(r'^racks/(?P<rack>[-\w]*)/(?P<details>add_device)/(?P<device>)$',
login_required(RacksAddDevice.as_view()), {}, 'racks'),
url(r'^racks/(?P<rack>[-\w]*)/(?P<details>rack)/(?P<device>)$',
@@ -224,13 +224,13 @@ def get(self, *args, **kwargs):
else:
self.query = DeviceModel.objects.filter(
type=self.model_type_id).filter(group=None)
- unassigned_device = DeviceModel.objects.filter(
+ unassigned_devices = DeviceModel.objects.filter(
type=self.model_type_id
).filter(
group=None
)
unassigned_count = 0
- for u in unassigned_device:
+ for u in unassigned_devices:
unassigned_count = unassigned_count + u.get_count()
self.unassigned_count = unassigned_count
self.groups = list(DeviceModelGroup.objects.filter(
View
@@ -36,7 +36,7 @@ def set_rack(self):
self.rack = None
return
rack_name = rack_name.replace('-', ' ')
- if rack_name and rack_name != 'rack none':
+ if rack_name and rack_name != 'rack none' and rack_name != ' ':
self.rack = get_object_or_404(
Device,
sn=rack_name,
@@ -54,7 +54,7 @@ def slug(sn):
return sn.replace(' ', '-').lower()
sidebar_items = [
MenuItem("Unknown", name='', fugue_icon='fugue-prohibition',
- view_name='racks', view_args=['', ret['details'], ''])
+ view_name='racks', view_args=['-', ret['details'], ''])
]
for dc in Device.objects.filter(
model__type=DeviceType.data_center.id).order_by('name'):
@@ -194,8 +194,11 @@ def get_context_data(self, **kwargs):
profile = self.request.user.get_profile()
has_perm = profile.has_perm
tab_items = ret['tab_items']
- tab_items.append(MenuItem('Rack', fugue_icon='fugue-media-player-phone',
- href='../rack/?%s' % self.request.GET.urlencode()))
+ if ret['subsection'] is not '':
+ tab_items.append(
+ MenuItem('Rack', fugue_icon='fugue-media-player-phone',
+ href='../rack/?%s' % self.request.GET.urlencode())
+ )
if has_perm(Perm.create_device, self.rack.venture if
self.rack else None):
tab_items.append(MenuItem('Add Device',
@@ -262,8 +265,14 @@ def get_context_data(self, **kwargs):
ret = super(RacksRack, self).get_context_data(**kwargs)
self.set_rack()
tab_items = ret['tab_items']
- tab_items.append(MenuItem('Rack', fugue_icon='fugue-media-player-phone',
- href='../rack/?%s' % self.request.GET.urlencode()))
+
+ tab_items.append(
+ MenuItem('Rack', fugue_icon='fugue-media-player-phone',
+ href='../rack/?%s' % self.request.GET.urlencode())
+ )
+ tab_items.append(MenuItem('Add device',
+ fugue_icon='fugue-wooden-box--plus',
+ href='../add_device/?%s' % self.request.GET.urlencode()))
if self.rack.model.type == DeviceType.rack.id:
slots_set = [
(self.rack, self.get_slots(self.rack))
@@ -278,6 +287,7 @@ def get_context_data(self, **kwargs):
})
return ret
+
class DeviceCreateView(CreateView):
model = Device
slug_field = 'id'
@@ -289,45 +299,101 @@ def get_success_url(self):
def get_template_names(self):
return [self.template_name]
+ def set_rack(self):
+ rack_name = self.kwargs.get('rack')
+ if rack_name is None:
+ self.rack = None
+ return
+ rack_name = rack_name.replace('-', ' ')
+ if rack_name and rack_name != 'rack none' and rack_name != ' ':
+ self.rack = get_object_or_404(
+ Device,
+ sn=rack_name,
+ model__type__in=(DeviceType.rack.id,
+ DeviceType.data_center.id)
+ )
+ else:
+ self.rack = ''
+
def form_valid(self, form):
self.set_rack()
- model = form.save(commit=False)
macs = [('', mac, 0) for mac in form.cleaned_data['macs'].split()]
- dev = Device.create(ethernets=macs, sn=form.cleaned_data['sn'],
- model=form.cleaned_data['model'], priority=1)
- form.instance = dev
- model = form.save(commit=False)
- model.parent = self.rack
- model.dc = self.rack.dc
- model.rack = self.rack.rack
- model.save(priority=1, user=self.request.user)
+ try:
+ dc = self.rack.dc
+ except AttributeError:
+ dc = None
+ try:
+ rack = self.rack.rack
+ except AttributeError:
+ rack = None
+ if self.rack == '':
+ self.rack = None
+ wed = form.cleaned_data['warranty_expiration_date']
+ sed = form.cleaned_data['support_expiration_date']
+ dev = Device.create(
+ ethernets=macs,
+ barcode=form.cleaned_data['barcode'],
+ remarks=form.cleaned_data['remarks'],
+ sn=form.cleaned_data['sn'],
+ model=form.cleaned_data['model'],
+ venture=form.cleaned_data['venture'],
+ purchase_date=form.cleaned_data['purchase_date'],
+ priority=1,
+ position=form.cleaned_data['position'],
+ chassis_position=form.cleaned_data['chassis_position'],
+ margin_kind=form.cleaned_data['margin_kind'],
+ deprecation_kind=form.cleaned_data['deprecation_kind'],
+ price=form.cleaned_data['price'],
+ warranty_expiration_date=wed,
+ support_expiration_date=sed,
+ support_kind=form.cleaned_data['support_kind'],
+ venture_role=form.cleaned_data['venture_role'],
+ parent=self.rack,
+ dc=dc,
+ rack=rack,
+ user=self.request.user,
+ )
+ dev.name=form.cleaned_data['name']
+ dev.save()
messages.success(self.request, "Device created.")
- return HttpResponseRedirect(self.request.path + '../info/%d' % model.id)
+ return HttpResponseRedirect(self.request.path + '../info/%d' % dev.id)
def get(self, *args, **kwargs):
self.set_rack()
has_perm = self.request.user.get_profile().has_perm
- if not has_perm(Perm.create_device, self.rack.venture):
+ try:
+ venture = self.rack.venture
+ except AttributeError:
+ venture = None
+ if not has_perm(Perm.create_device, venture):
return HttpResponseForbidden(
- "You don't have permission to create devices here.")
+ "You don't have permission to create devices here.")
return super(DeviceCreateView, self).get(*args, **kwargs)
def post(self, *args, **kwargs):
self.set_rack()
has_perm = self.request.user.get_profile().has_perm
- if not has_perm(Perm.create_device, self.rack.venture):
+ try:
+ venture = self.rack.venture
+ except AttributeError:
+ venture = None
+ if not has_perm(Perm.create_device, venture):
return HttpResponseForbidden(
- "You don't have permission to create devices here.")
+ "You don't have permission to create devices here.")
return super(DeviceCreateView, self).post(*args, **kwargs)
-
-
class RacksAddDevice(Racks, DeviceCreateView):
template_name = 'ui/racks-add-device.html'
form_class = DeviceCreateForm
def get_context_data(self, **kwargs):
ret = super(RacksAddDevice, self).get_context_data(**kwargs)
tab_items = ret['tab_items']
+ if ret['subsection'] is not '':
+ tab_items.append(
+ MenuItem('Rack', fugue_icon='fugue-media-player-phone',
+ href='../rack/?%s' % self.request.GET.urlencode())
+ )
+
tab_items.append(MenuItem('Add Device', name='add_device',
fugue_icon='fugue-wooden-box--plus',
href='../add_device/?%s' % (

0 comments on commit 0b38d62

Please sign in to comment.