def get_queryset(self): combined = Asset.objects.filter( commissioned=Asset.Decommissioned.COMMISSIONED) version = ChangePlan.objects.get(id=get_version(self.request)) versioned = versioned_queryset(combined, version, Asset.IDENTITY_FIELDS) return versioned
def get_queryset(self): version = ChangePlan.objects.get(id=get_version(self.request)) queryset = Asset.objects.all() datacenter_id = self.request.query_params.get('site_id', None) if datacenter_id is not None: queryset = queryset.filter(site_id=datacenter_id) rack_id = self.request.query_params.get('rack_id', None) if rack_id is not None: rack = Rack.objects.filter(id=rack_id).first() if rack is not None: queryset = queryset.filter(rack__rack=rack.rack) if 'asset_id' in self.request.query_params: queryset = queryset.exclude( id=self.request.query_params['asset_id']) queryset = versioned_queryset(queryset, version, Asset.IDENTITY_FIELDS) return queryset
def export(self, queryset=None, version_id=0, *args, **kwargs): if queryset is None: queryset = self.get_queryset() for network_connection in queryset: src = network_connection dest = network_connection.connection if dest: if src.asset.itmodel.network_ports > dest.asset.itmodel.network_ports: queryset = queryset.exclude(id=src.id) elif src.asset.itmodel.network_ports == dest.asset.itmodel.network_ports: if src.asset.asset_number > dest.asset.asset_number: queryset = queryset.exclude(id=src.id) queryset = queryset.exclude(mac_address__isnull=True, connection__isnull=True) version = ChangePlan.objects.get(id=version_id) versioned = versioned_queryset(queryset, version, NetworkPort.IDENTITY_FIELDS) versioned = versioned.order_by('asset__hostname', 'label__name') return super(NetworkPortResource, self).export(versioned, *args, **kwargs)
def to_representation(self, instance): data = super(AssetSerializer, self).to_representation(instance) if not instance.site.offline and not instance.itmodel.type == ITModel.Type.BLADE: connections = Powered.objects.filter(asset=instance) ports = NetworkPort.objects.filter(asset=instance) if instance.version.id != 0: connections = versioned_queryset(connections, instance.version, Powered.IDENTITY_FIELDS) ports = versioned_queryset(ports, instance.version, NetworkPort.IDENTITY_FIELDS) data['power_connections'] = [ {'pdu_id': conn['pdu'], 'plug': conn['plug_number']} for conn in connections.values('pdu', 'plug_number')] data['network_ports'] = [ NetworkPortSerializer(port).data for port in ports.order_by('label') if port ] update_asset_power(instance) networked = instance.pdu_set.filter(networked=True) if networked.exists() and instance.version.id == 0: data['power_state'] = "Off" for pdu in networked: if pdu.powered_set.filter(asset=instance, on=True).exists(): data['power_state'] = "On" break else: data['power_state'] = None elif not instance.site.offline and instance.itmodel.type == ITModel.Type.BLADE: chassis_hostname = instance.blade_chassis.hostname vendor_bmi = instance.blade_chassis.itmodel.vendor == "BMI" if chassis_hostname and vendor_bmi: data['power_state'] = "On" if is_blade_power_on(chassis_hostname, instance.slot) else "Off" else: data['power_state'] = None else: data['power_connections'] = [] data['network_ports'] = [] data['power_state'] = None data['decommissioned'] = not instance.commissioned data['network_graph'] = net_graph(instance.id) location = {"site": instance.site.id} if instance.rack_position is not None: location['tag'] = 'rack-mount' location['rack'] = instance.rack.id location['rack_position'] = instance.rack_position elif instance.slot is not None: location['tag'] = 'chassis-mount' location['rack'] = instance.blade_chassis.rack.id if instance.blade_chassis.rack else None location['asset'] = instance.blade_chassis.id location['slot'] = instance.slot elif instance.site.offline: location['tag'] = 'offline' else: raise serializers.ValidationError( "Location of this asset is inconsistent." ) data['location'] = location del data['site'] del data['rack'] del data['rack_position'] del data['blade_chassis'] del data['slot'] return data
def filter_queryset(self, request, queryset, view): version = get_version(request) return versioned_queryset( queryset, ChangePlan.objects.get(id=version), view.serializer_class.Meta.model.IDENTITY_FIELDS)