Esempio n. 1
0
 def create(self, data):
     data['orientation'] = RackOrientation.id_from_name(data['orientation'])
     data['server_room'] = ServerRoom.objects.get(
         pk=int(data['server_room'])
     )
     return Rack.objects.create(**data)
Esempio n. 2
0
 def get_orientation_desc(self):
     return RackOrientation.name_from_id(self.orientation)
Esempio n. 3
0
 def update(self, data):
     data['server_room'] = ServerRoom.objects.get(
         pk=data['server_room']
     )
     data['orientation'] = RackOrientation.id_from_name(data['orientation'])
     return super(RackBaseSerializer, self).update(self.instance, data)
Esempio n. 4
0
class Rack(AdminAbsoluteUrlMixin, NamedMixin.NonUnique, models.Model):
    _allow_in_dashboard = True

    server_room = models.ForeignKey(
        ServerRoom, verbose_name=_('server room'),
        null=True,
        blank=False,
    )
    server_room._autocomplete = False
    server_room._filter_title = _('server room')
    description = models.CharField(
        _('description'), max_length=250, blank=True
    )
    orientation = models.PositiveIntegerField(
        choices=RackOrientation(),
        default=RackOrientation.top.id,
    )
    max_u_height = models.IntegerField(default=48)

    visualization_col = models.PositiveIntegerField(
        verbose_name=_('column number on visualization grid'),
        default=0,
    )
    visualization_row = models.PositiveIntegerField(
        verbose_name=_('row number on visualization grid'),
        default=0,
    )
    accessories = models.ManyToManyField(Accessory, through='RackAccessory')
    require_position = models.BooleanField(
        default=True,
        help_text=_(
            'Uncheck if position is optional for this rack (ex. when rack '
            'has warehouse-kind role'
        )
    )
    reverse_ordering = models.BooleanField(
        default=settings.RACK_LISTING_NUMBERING_TOP_TO_BOTTOM,
        help_text=_(
            'Check if RU numbers count from top to bottom with position 1 '
            'starting at the top of the rack. If unchecked position 1 is '
            'at the bottom of the rack'
        ),
        verbose_name=_('RU order top to bottom'),
    )

    class Meta:
        unique_together = ('name', 'server_room')

    def __str__(self):
        if self.server_room:
            return "{} ({}/{})".format(
                self.name,
                self.server_room.data_center,
                self.server_room.name,
            )
        return self.name

    def get_orientation_desc(self):
        return RackOrientation.name_from_id(self.orientation)

    def get_root_assets(self, side=None):
        filter_kwargs = {
            'rack': self,
        }
        if side:
            filter_kwargs['orientation'] = side
        else:
            filter_kwargs['orientation__in'] = [
                Orientation.front, Orientation.back
            ]
        return DataCenterAsset.objects.select_related(
            'model', 'model__category'
        ).filter(
            Q(slot_no='') | Q(slot_no=None), **filter_kwargs
        ).exclude(model__has_parent=True)

    def get_free_u(self):
        u_list = [True] * self.max_u_height
        accessories = RackAccessory.objects.values_list(
            'position').filter(rack=self)
        dc_assets = self.get_root_assets().values_list(
            'position', 'model__height_of_device'
        )

        def fill_u_list(objects, height_of_device=lambda obj: 1):
            for obj in objects:
                # if position is None when objects simply does not have
                # (assigned) position and position 0 is for some
                # accessories (pdu) with left-right orientation and
                # should not be included in free/filled space.
                if obj[0] == 0 or obj[0] is None:
                    continue

                start = obj[0] - 1
                end = min(
                    self.max_u_height, obj[0] + int(height_of_device(obj)) - 1
                )
                height = end - start
                if height:
                    u_list[start:end] = [False] * height
        fill_u_list(accessories)
        fill_u_list(dc_assets, lambda obj: obj[1])
        return sum(u_list)

    def get_pdus(self):
        return DataCenterAsset.objects.select_related('model').filter(
            rack=self,
            orientation__in=(Orientation.left, Orientation.right),
            position=0,
        )