Beispiel #1
0
 def get_context_data(self, **kwargs):
     ret = super(Catalog, self).get_context_data(**kwargs)
     try:
         model_type_id = int(self.kwargs.get('type', ''))
     except ValueError:
         model_type_id = None
     kind = self.kwargs.get('kind')
     sidebar_items = (
         [
             MenuHeader('Component groups'),
         ] + [
             MenuItem(
                 label=t.raw.title(),
                 name='component-%d' % t.id,
                 fugue_icon=COMPONENT_ICONS.get(t.id),
                 view_name='catalog',
                 view_args=('component', t.id),
             ) for t in ComponentType(item=lambda t: t)
         ] + [
             MenuHeader('Device groups'),
         ] + [
             MenuItem(
                 label=t.raw.title(),
                 name='device-%d' % t.id,
                 fugue_icon=DEVICE_ICONS.get(t.id),
                 view_name='catalog',
                 view_args=('device', t.id),
             ) for t in DeviceType(item=lambda t: t)
         ] + [
             MenuHeader('Tools'),
             MenuItem(
                 'History',
                 name='history',
                 fugue_icon='fugue-hourglass',
                 view_name='catalog_history',
             ),
             MenuItem(
                 'Pricing groups',
                 name='pricing',
                 fugue_icon='fugue-shopping-basket',
                 view_name='catalog_pricing',
                 view_args=('pricing',),
             ),
         ]
     )
     selected = '%s-%d' % (kind, model_type_id) if model_type_id else ''
     ret.update({
         'component_model_types': ComponentType(item=lambda a: a),
         'details': self.kwargs.get('details', 'info'),
         'device_model_types': DeviceType(item=lambda a: a),
         'editable': True,
         'kind': kind,
         'model_type_id': model_type_id,
         'sidebar_items': sidebar_items,
         'sidebar_selected': selected,
         'subsection': model_type_id,
     })
     return ret
Beispiel #2
0
 def Field(self, *args, **kwargs):
     choices = [(t.raw, t.raw.title())
                for t in DeviceType(item=lambda t: t)]
     choices.pop()  # remove "Unknown" from the list
     choices.insert(0, ('', '-----'))
     kwargs['choices'] = choices
     return forms.ChoiceField(*args, **kwargs)
Beispiel #3
0
 def Field(self, *args, **kwargs):
     choices = [(t.raw, t.raw.title())
                for t in DeviceType(item=lambda t: t)]
     # Make "Unknown" the first choice
     choices.insert(0, choices.pop())
     kwargs['choices'] = choices
     return forms.ChoiceField(*args, **kwargs)
Beispiel #4
0
def get_model(device):
    if not device.model:
        return ''
    model = '[%s] %s' % (DeviceType.name_from_id(device.model.type),
                         device.model.name)
    if device.model.group:
        model += ' {%s}' % device.model.group.name
    return model
Beispiel #5
0
def get_model(device):
    if not device.model:
        return ''
    model = '[%s] %s' % (DeviceType.name_from_id(
        device.model.type), device.model.name)
    if device.model.group:
        model += ' {%s}' % device.model.group.name
    return model
Beispiel #6
0
def _update_batch(device_ids, rack, dc):
    for d in Device.objects.filter(id__in=device_ids):
        name = d.get_name()
        if name != 'unknown':
            d.name = name
        try:
            model_type = DeviceType.raw_from_id(rack.model.type)
        except AttributeError:
            model_type = None
        if model_type == 'rack':
            d.rack = rack.sn
        else:
            d.rack = None
        d.dc = dc.name.upper() if dc else None
        d.save()
Beispiel #7
0
class SearchForm(forms.Form):
    name = forms.CharField(required=False,
                           widget=forms.TextInput(attrs={'class': 'span12'}))
    address = forms.CharField(required=False,
                              widget=forms.TextInput(attrs={
                                  'class': 'span12',
                                  'title': TooltipContent.empty_field,
                              }),
                              label="Address or network")
    remarks = forms.CharField(required=False,
                              widget=forms.TextInput(attrs={
                                  'class': 'span12',
                                  'title': TooltipContent.empty_field
                              }))
    role = forms.CharField(required=False,
                           widget=forms.TextInput(attrs={
                               'class': 'span12',
                               'title': TooltipContent.empty_field,
                           }),
                           label="Venture or role")
    # This search field cannot be named 'service', because 'Info' tab already
    # has ajax field with the same id, and such collision results in unwanted
    # data bindings between those two.
    service_catalog = forms.CharField(required=False,
                                      widget=forms.TextInput(attrs={
                                          'class': 'span12',
                                          'title': TooltipContent.empty_field,
                                      }),
                                      label="Service catalog")
    device_environment = forms.CharField(required=False,
                                         widget=forms.TextInput(attrs={
                                             'class': 'span12',
                                             'title': TooltipContent.empty_field,
                                         }),
                                         label="Device environment")
    model = forms.CharField(required=False,
                            widget=forms.TextInput(attrs={
                                'class': 'span12',
                                'title': TooltipContent.empty_field_venture,
                            }))
    component = forms.CharField(required=False,
                                widget=forms.TextInput(
                                    attrs={'class': 'span12'}),
                                label="Component")
    software = forms.CharField(required=False,
                               widget=forms.TextInput(attrs={
                                   'class': 'span12',
                                   'title': TooltipContent.software_field,
                               }),
                               label="Software")
    serial = forms.CharField(required=False,
                             widget=forms.TextInput(attrs={
                                 'class': 'span12',
                                 'title': TooltipContent.empty_field,
                             }),
                             label="Serial number, MAC or WWN")
    barcode = forms.CharField(required=False,
                              widget=forms.TextInput(attrs={
                                  'class': 'span12',
                                  'title': TooltipContent.empty_field,
                              }))
    department = forms.CharField(
        required=False, widget=forms.TextInput(attrs={
            'class': 'span12',
        }))
    position = forms.CharField(required=False,
                               widget=forms.TextInput(attrs={
                                   'class': 'span12',
                                   'title': TooltipContent.empty_field,
                               }),
                               label="Datacenter, rack or position")
    history = forms.CharField(required=False,
                              widget=forms.TextInput(attrs={
                                  'class': 'span12'
                              }))
    device_type = forms.MultipleChoiceField(required=False,
                                            widget=forms.SelectMultiple(
                                                attrs={'class': 'span12'}),
                                            choices=DeviceType(
                                                item=lambda e: (e.id, e.raw)),
                                            )
    purchase_date_start = forms.DateField(required=False,
                                          widget=DateWidget(attrs={
                                              'class': 'span12',
                                              'placeholder': 'Start YYYY-MM-DD',
                                              'data-collapsed': True,
                                          }),
                                          label='Purchase date', input_formats=['%Y-%m-%d'])
    purchase_date_end = forms.DateField(required=False,
                                        widget=DateWidget(attrs={
                                            'class': 'span12 end-date-field',
                                            'placeholder': 'End YYYY-MM-DD',
                                            'data-collapsed': True,
                                        }),
                                        label='', input_formats=['%Y-%m-%d'])
    no_purchase_date = forms.BooleanField(required=False,
                                          label="Empty purchase date",
                                          widget=forms.CheckboxInput(attrs={
                                              'data-collapsed': True,
                                          }))
    deprecation_date_start = forms.DateField(required=False,
                                             widget=DateWidget(attrs={
                                                 'class': 'span12',
                                                 'placeholder': 'Start YYYY-MM-DD',
                                                 'data-collapsed': True,
                                             }),
                                             label='Deprecation date', input_formats=['%Y-%m-%d'])
    deprecation_date_end = forms.DateField(required=False,
                                           widget=DateWidget(attrs={
                                               'class': 'span12 end-date-field',
                                               'placeholder': 'End YYYY-MM-DD',
                                               'data-collapsed': True,
                                           }),
                                           label='', input_formats=['%Y-%m-%d'])
    no_deprecation_date = forms.BooleanField(required=False,
                                             label="Empty deprecation date",
                                             widget=forms.CheckboxInput(attrs={
                                                 'data-collapsed': True,
                                             }))
    deprecation_kind = forms.MultipleChoiceField(
        required=False,
        widget=forms.SelectMultiple(
            attrs={'class': 'span12',
                   'data-collapsed': True},),
        label="Deprecation",
        choices=[('None', '-----')] + [(kind.id, kind.name)
                                       for kind in DeprecationKind.objects.all()],
    )
    warranty_expiration_date_start = forms.DateField(required=False,
                                                     widget=DateWidget(attrs={
                                                         'class': 'span12',
                                                         'placeholder': 'Start YYYY-MM-DD',
                                                         'data-collapsed': True,
                                                     }),
                                                     label='Warranty expiration date', input_formats=['%Y-%m-%d'])
    warranty_expiration_date_end = forms.DateField(required=False,
                                                   widget=DateWidget(attrs={
                                                       'class': 'span12 end-date-field',
                                                       'placeholder': 'End YYYY-MM-DD',
                                                       'data-collapsed': True,
                                                   }),
                                                   label='', input_formats=['%Y-%m-%d'])
    no_warranty_expiration_date = forms.BooleanField(required=False,
                                                     label="Empty warranty expiration date",
                                                     widget=forms.CheckboxInput(attrs={
                                                         'data-collapsed': True,
                                                     }))
    support_expiration_date_start = forms.DateField(required=False,
                                                    widget=DateWidget(attrs={
                                                        'class': 'span12',
                                                        'placeholder': 'Start YYYY-MM-DD',
                                                        'data-collapsed': True,
                                                    }),
                                                    label='Support expiration date', input_formats=['%Y-%m-%d'])
    support_expiration_date_end = forms.DateField(required=False,
                                                  widget=DateWidget(attrs={
                                                      'class': 'span12 end-date-field ',
                                                      'placeholder': 'End YYYY-MM-DD',
                                                      'data-collapsed': True,
                                                  }),
                                                  label='', input_formats=['%Y-%m-%d'])
    no_support_expiration_date = forms.BooleanField(required=False,
                                                    label="Empty support expiration date",
                                                    widget=forms.CheckboxInput(attrs={
                                                        'data-collapsed': True,
                                                    }))
    with_changes = forms.BooleanField(
        required=False,
        label="Only with Scan changes",
    )
    deleted = forms.BooleanField(required=False, label="Include deleted")

    def clean_purchase_date_end(self):
        validate_start_end_date_date(self.cleaned_data['purchase_date_start'],
                                     self.cleaned_data['purchase_date_end'])
        return self.cleaned_data['purchase_date_end']

    def clean_deprecation_date_end(self):
        validate_start_end_date_date(
            self.cleaned_data['deprecation_date_start'],
            self.cleaned_data['deprecation_date_end'])
        return self.cleaned_data['deprecation_date_end']

    def clean_warranty_expiration_date_end(self):
        validate_start_end_date_date(
            self.cleaned_data['warranty_expiration_date_start'],
            self.cleaned_data['warranty_expiration_date_end'])
        return self.cleaned_data['warranty_expiration_date_end']

    def clean_support_expiration_date_end(self):
        validate_start_end_date_date(
            self.cleaned_data['support_expiration_date_start'],
            self.cleaned_data['support_expiration_date_end'])
        return self.cleaned_data['support_expiration_date_end']

    def clean_address(self):
        data = self.cleaned_data['address']
        if data:
            if '/' in data:
                try:
                    ipaddr.IPNetwork(data)
                except ValueError:
                    raise forms.ValidationError("Invalid network")
            else:
                try:
                    ipaddr.IPv4Address(data)
                except ValueError:
                    raise forms.ValidationError("Invalid address")
        return data
Beispiel #8
0
class SearchForm(forms.Form):
    name = forms.CharField(required=False,
                           widget=forms.TextInput(attrs={'class': 'span12'}))
    address = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'span12',
            'title': TooltipContent.empty_field,
        }),
        label="Address or network")
    remarks = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'span12',
            'title': TooltipContent.empty_field
        }))
    role = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'span12',
            'title': TooltipContent.empty_field,
        }),
        label="Venture or role")
    model = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'span12',
            'title': TooltipContent.empty_field_venture,
        }))
    component = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={'class': 'span12'}),
        label="Component or software")
    serial = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'span12',
            'title': TooltipContent.empty_field,
        }),
        label="Serial number, MAC or WWN")
    barcode = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'span12',
            'title': TooltipContent.empty_field,
        }))
    position = forms.CharField(
        required=False,
        widget=forms.TextInput(attrs={
            'class': 'span12',
            'title': TooltipContent.empty_field,
        }),
        label="Datacenter, rack or position")
    history = forms.CharField(
        required=False, widget=forms.TextInput(attrs={'class': 'span12'}))
    device_type = forms.MultipleChoiceField(
        required=False,
        widget=forms.SelectMultiple(attrs={'class': 'span12'}),
        choices=DeviceType(item=lambda e: (e.id, e.raw)),
    )
    device_group = forms.IntegerField(required=False,
                                      widget=DeviceGroupWidget,
                                      label="")
    component_group = forms.IntegerField(required=False,
                                         widget=ComponentGroupWidget,
                                         label="")
    purchase_date_start = forms.DateField(
        required=False,
        widget=DateWidget(
            attrs={
                'class': 'span12',
                'placeholder': 'Start YYYY-MM-DD',
                'data-collapsed': True,
            }),
        label='Purchase date',
        input_formats=['%Y-%m-%d'])
    purchase_date_end = forms.DateField(
        required=False,
        widget=DateWidget(
            attrs={
                'class': 'span12 end-date-field',
                'placeholder': 'End YYYY-MM-DD',
                'data-collapsed': True,
            }),
        label='',
        input_formats=['%Y-%m-%d'])
    no_purchase_date = forms.BooleanField(
        required=False,
        label="Empty purchase date",
        widget=forms.CheckboxInput(attrs={
            'data-collapsed': True,
        }))
    deprecation_date_start = forms.DateField(
        required=False,
        widget=DateWidget(
            attrs={
                'class': 'span12',
                'placeholder': 'Start YYYY-MM-DD',
                'data-collapsed': True,
            }),
        label='Deprecation date',
        input_formats=['%Y-%m-%d'])
    deprecation_date_end = forms.DateField(
        required=False,
        widget=DateWidget(
            attrs={
                'class': 'span12 end-date-field',
                'placeholder': 'End YYYY-MM-DD',
                'data-collapsed': True,
            }),
        label='',
        input_formats=['%Y-%m-%d'])
    no_deprecation_date = forms.BooleanField(
        required=False,
        label="Empty deprecation date",
        widget=forms.CheckboxInput(attrs={
            'data-collapsed': True,
        }))
    warranty_expiration_date_start = forms.DateField(
        required=False,
        widget=DateWidget(
            attrs={
                'class': 'span12',
                'placeholder': 'Start YYYY-MM-DD',
                'data-collapsed': True,
            }),
        label='Warranty expiration date',
        input_formats=['%Y-%m-%d'])
    warranty_expiration_date_end = forms.DateField(
        required=False,
        widget=DateWidget(
            attrs={
                'class': 'span12 end-date-field',
                'placeholder': 'End YYYY-MM-DD',
                'data-collapsed': True,
            }),
        label='',
        input_formats=['%Y-%m-%d'])
    no_warranty_expiration_date = forms.BooleanField(
        required=False,
        label="Empty warranty expiration date",
        widget=forms.CheckboxInput(attrs={
            'data-collapsed': True,
        }))
    support_expiration_date_start = forms.DateField(
        required=False,
        widget=DateWidget(
            attrs={
                'class': 'span12',
                'placeholder': 'Start YYYY-MM-DD',
                'data-collapsed': True,
            }),
        label='Support expiration date',
        input_formats=['%Y-%m-%d'])
    support_expiration_date_end = forms.DateField(
        required=False,
        widget=DateWidget(
            attrs={
                'class': 'span12 end-date-field ',
                'placeholder': 'End YYYY-MM-DD',
                'data-collapsed': True,
            }),
        label='',
        input_formats=['%Y-%m-%d'])
    no_support_expiration_date = forms.BooleanField(
        required=False,
        label="Empty support expiration date",
        widget=forms.CheckboxInput(attrs={
            'data-collapsed': True,
        }))
    deleted = forms.BooleanField(required=False, label="Include deleted")

    def clean_purchase_date_end(self):
        validate_start_end_date_date(self.cleaned_data['purchase_date_start'],
                                     self.cleaned_data['purchase_date_end'])
        return self.cleaned_data['purchase_date_end']

    def clean_deprecation_date_end(self):
        validate_start_end_date_date(
            self.cleaned_data['deprecation_date_start'],
            self.cleaned_data['deprecation_date_end'])
        return self.cleaned_data['deprecation_date_end']

    def clean_warranty_expiration_date_end(self):
        validate_start_end_date_date(
            self.cleaned_data['warranty_expiration_date_start'],
            self.cleaned_data['warranty_expiration_date_end'])
        return self.cleaned_data['warranty_expiration_date_end']

    def clean_support_expiration_date_end(self):
        validate_start_end_date_date(
            self.cleaned_data['support_expiration_date_start'],
            self.cleaned_data['support_expiration_date_end'])
        return self.cleaned_data['support_expiration_date_end']

    def clean_address(self):
        data = self.cleaned_data['address']
        if data:
            if '/' in data:
                try:
                    ipaddr.IPNetwork(data)
                except ValueError:
                    raise forms.ValidationError("Invalid network")
            else:
                try:
                    ipaddr.IPv4Address(data)
                except ValueError:
                    raise forms.ValidationError("Invalid address")
        return data
Beispiel #9
0
def device_type(dev):
    return DeviceType.DescFromID((dev.model.type if dev.model else None)
                                 or DeviceType.unknown.id).title()
Beispiel #10
0
# -*- coding: utf-8 -*-
"""
Set of functions to make diff from Scan results.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from django.utils.encoding import force_unicode

from ralph.discovery.models import DeviceType
from ralph.scan.data import UNIQUE_FIELDS_FOR_MERGER

RAW_DEVICE_TYPES = [choice_name for _, choice_name in DeviceType()]


def _sort_dict_by_multiple_fields_values(keynames):
    def getit(adict):
        composite = []
        for key in keynames:
            if key in adict:
                composite.append(adict[key])
        return composite

    return getit


def sort_results(data, ignored_fields=set(['device'])):
    """