9856 GFK working

This commit is contained in:
Arthur 2024-02-15 14:54:04 -08:00
parent d37414d69a
commit 1aa5b0d5a1
3 changed files with 76 additions and 105 deletions

View File

@ -37,78 +37,6 @@ from dcim.models import (
) )
class LinkPeerType(graphene.Union):
class Meta:
types = (
CircuitTerminationType,
ConsolePortType,
ConsoleServerPortType,
FrontPortType,
InterfaceType,
PowerFeedType,
PowerOutletType,
PowerPortType,
RearPortType,
)
@classmethod
def resolve_type(cls, instance, info):
if type(instance) is CircuitTermination:
return CircuitTerminationType
if type(instance) is ConsolePortType:
return ConsolePortType
if type(instance) is ConsoleServerPort:
return ConsoleServerPortType
if type(instance) is FrontPort:
return FrontPortType
if type(instance) is Interface:
return InterfaceType
if type(instance) is PowerFeed:
return PowerFeedType
if type(instance) is PowerOutlet:
return PowerOutletType
if type(instance) is PowerPort:
return PowerPortType
if type(instance) is RearPort:
return RearPortType
class CableTerminationTerminationType(graphene.Union):
class Meta:
types = (
CircuitTerminationType,
ConsolePortType,
ConsoleServerPortType,
FrontPortType,
InterfaceType,
PowerFeedType,
PowerOutletType,
PowerPortType,
RearPortType,
)
@classmethod
def resolve_type(cls, instance, info):
if type(instance) is CircuitTermination:
return CircuitTerminationType
if type(instance) is ConsolePortType:
return ConsolePortType
if type(instance) is ConsoleServerPort:
return ConsoleServerPortType
if type(instance) is FrontPort:
return FrontPortType
if type(instance) is Interface:
return InterfaceType
if type(instance) is PowerFeed:
return PowerFeedType
if type(instance) is PowerOutlet:
return PowerOutletType
if type(instance) is PowerPort:
return PowerPortType
if type(instance) is RearPort:
return RearPortType
class InventoryItemTemplateComponentType(graphene.Union): class InventoryItemTemplateComponentType(graphene.Union):
class Meta: class Meta:
types = ( types = (

View File

@ -1,20 +1,40 @@
import graphene import strawberry
import strawberry_django
from typing import TYPE_CHECKING, Annotated, List, Union
__all__ = (
'CabledObjectMixin',
'PathEndpointMixin',
)
@strawberry.type
class CabledObjectMixin: class CabledObjectMixin:
link_peers = graphene.List('dcim.graphql.gfk_mixins.LinkPeerType')
def resolve_cable_end(self, info): # @strawberry_django.field
# Handle empty values # def cable_end(self) -> List[Annotated["ObjectChangeType", strawberry.lazy('.types')]]:
return self.cable_end or None # # Handle empty values
# return self.cable_end or None
def resolve_link_peers(self, info): @strawberry_django.field
def link_peers(self) -> List[Annotated[Union[
Annotated["CircuitTerminationType", strawberry.lazy('circuits.graphql.types')],
Annotated["ConsolePortType", strawberry.lazy('dcim.graphql.types')],
Annotated["ConsoleServerPortType", strawberry.lazy('dcim.graphql.types')],
Annotated["FrontPortType", strawberry.lazy('dcim.graphql.types')],
Annotated["InterfaceType", strawberry.lazy('dcim.graphql.types')],
Annotated["PowerFeedType", strawberry.lazy('dcim.graphql.types')],
Annotated["PowerOutletType", strawberry.lazy('dcim.graphql.types')],
Annotated["PowerPortType", strawberry.lazy('dcim.graphql.types')],
Annotated["RearPortType", strawberry.lazy('dcim.graphql.types')],
], strawberry.union("LinkPeerType")]]:
return self.link_peers return self.link_peers
@strawberry.type
class PathEndpointMixin: class PathEndpointMixin:
connected_endpoints = graphene.List('dcim.graphql.gfk_mixins.ConnectedEndpointType') pass
# @strawberry_django.field
def resolve_connected_endpoints(self, info): # def connected_endpoints(self) -> List[Annotated["ObjectChangeType", strawberry.lazy('.types')]]:
# Handle empty values # # Handle empty values
return self.connected_endpoints or None # return self.connected_endpoints or None

View File

@ -1,12 +1,24 @@
from typing import Annotated, List, Union
import strawberry import strawberry
import strawberry_django import strawberry_django
from dcim import models from dcim import models
from extras.graphql.mixins import ( from extras.graphql.mixins import (
ChangelogMixin, ConfigContextMixin, ContactsMixin, CustomFieldsMixin, ImageAttachmentsMixin, TagsMixin, ChangelogMixin,
ConfigContextMixin,
ContactsMixin,
CustomFieldsMixin,
ImageAttachmentsMixin,
TagsMixin,
) )
from ipam.graphql.mixins import IPAddressesMixin, VLANGroupsMixin from ipam.graphql.mixins import IPAddressesMixin, VLANGroupsMixin
from netbox.graphql.types import BaseObjectType, OrganizationalObjectType, NetBoxObjectType
from netbox.graphql.types import (
BaseObjectType,
NetBoxObjectType,
OrganizationalObjectType,
)
from .filters import * from .filters import *
from .mixins import CabledObjectMixin, PathEndpointMixin from .mixins import CabledObjectMixin, PathEndpointMixin
@ -88,6 +100,29 @@ class ComponentTemplateObjectType(
# Model types # Model types
# #
@strawberry_django.type(
models.CableTermination,
exclude=('termination_type', 'termination_id'),
filters=CableTerminationFilter
)
class CableTerminationType(NetBoxObjectType):
@strawberry_django.field
def termination(self) -> List[Annotated[Union[
Annotated["CircuitTerminationType", strawberry.lazy('circuits.graphql.types')],
Annotated["ConsolePortType", strawberry.lazy('dcim.graphql.types')],
Annotated["ConsoleServerPortType", strawberry.lazy('dcim.graphql.types')],
Annotated["FrontPortType", strawberry.lazy('dcim.graphql.types')],
Annotated["InterfaceType", strawberry.lazy('dcim.graphql.types')],
Annotated["PowerFeedType", strawberry.lazy('dcim.graphql.types')],
Annotated["PowerOutletType", strawberry.lazy('dcim.graphql.types')],
Annotated["PowerPortType", strawberry.lazy('dcim.graphql.types')],
Annotated["RearPortType", strawberry.lazy('dcim.graphql.types')],
], strawberry.union("CableTerminationTerminationType")]]:
return self.termination
@strawberry_django.type( @strawberry_django.type(
models.Cable, models.Cable,
# fields='__all__', # fields='__all__',
@ -95,32 +130,20 @@ class ComponentTemplateObjectType(
filters=CableFilter filters=CableFilter
) )
class CableType(NetBoxObjectType): class CableType(NetBoxObjectType):
# a_terminations = graphene.List('dcim.graphql.gfk_mixins.CableTerminationTerminationType')
# b_terminations = graphene.List('dcim.graphql.gfk_mixins.CableTerminationTerminationType')
def resolve_type(self, info): @strawberry_django.field
return self.type or None def terminations(self) -> List[CableTerminationType]:
return self.terminations
def resolve_length_unit(self, info): @strawberry_django.field
return self.length_unit or None def a_terminations(self) -> List[CableTerminationType]:
def resolve_a_terminations(self, info):
return self.a_terminations return self.a_terminations
def resolve_b_terminations(self, info): @strawberry_django.field
def b_terminations(self) -> List[CableTerminationType]:
return self.b_terminations return self.b_terminations
@strawberry_django.type(
models.CableTermination,
exclude=('termination_type', 'termination_id'),
filters=CableTerminationFilter
)
class CableTerminationType(NetBoxObjectType):
# termination = graphene.Field('dcim.graphql.gfk_mixins.CableTerminationTerminationType')
pass
@strawberry_django.type( @strawberry_django.type(
models.ConsolePort, models.ConsolePort,
# exclude=('_path',), # exclude=('_path',),