9856 fix counter fields and merge feature

This commit is contained in:
Arthur 2024-03-13 11:37:13 -07:00
parent 2b7c1d1845
commit 1da5219563
2 changed files with 19 additions and 10 deletions

View File

@ -5,7 +5,8 @@ import django_filters
import strawberry import strawberry
import strawberry_django import strawberry_django
from strawberry import auto from strawberry import auto
from utilities.fields import ColorField from netbox.graphql.scalars import BigInt
from utilities.fields import ColorField, CounterCacheField
from utilities.filters import * from utilities.filters import *
@ -17,18 +18,31 @@ def autotype_decorator(filterset):
print(field_type) print(field_type)
print("") print("")
def create_attribute_and_function(cls, fieldname, attr_type, create_function):
if fieldname not in cls.__annotations__ and attr_type:
cls.__annotations__[fieldname] = attr_type
fname = f"filter_{fieldname}"
if create_function and not hasattr(cls, fname):
filter_by_filterset = getattr(cls, 'filter_by_filterset')
setattr(cls, fname, partialmethod(filter_by_filterset, key=fieldname))
def wrapper(cls): def wrapper(cls):
cls.filterset = filterset cls.filterset = filterset
fields = filterset.get_fields() fields = filterset.get_fields()
model = filterset._meta.model model = filterset._meta.model
for fieldname in fields.keys(): for fieldname in fields.keys():
create_function = False
attr_type = auto attr_type = auto
if fieldname not in cls.__annotations__: if fieldname not in cls.__annotations__:
field = model._meta.get_field(fieldname) field = model._meta.get_field(fieldname)
if isinstance(field, ColorField): if isinstance(field, CounterCacheField):
create_function = True
attr_type = BigInt
elif isinstance(field, ColorField):
attr_type = List[str] | None attr_type = List[str] | None
cls.__annotations__[fieldname] = attr_type create_attribute_and_function(cls, fieldname, attr_type, create_function)
declared_filters = filterset.declared_filters declared_filters = filterset.declared_filters
for fieldname, v in declared_filters.items(): for fieldname, v in declared_filters.items():
@ -145,13 +159,7 @@ def autotype_decorator(filterset):
else: else:
show_field("unknown type!", fieldname, v, cls) show_field("unknown type!", fieldname, v, cls)
if fieldname not in cls.__annotations__ and attr_type: create_attribute_and_function(cls, fieldname, attr_type, create_function)
cls.__annotations__[fieldname] = attr_type
fname = f"filter_{fieldname}"
if create_function and not hasattr(cls, fname):
filter_by_filterset = getattr(cls, 'filter_by_filterset')
setattr(cls, fname, partialmethod(filter_by_filterset, key=fieldname))
return cls return cls

View File

@ -82,6 +82,7 @@ class VirtualMachineType(ConfigContextMixin, NetBoxObjectType):
_name: str _name: str
interface_count: BigInt interface_count: BigInt
virtual_disk_count: BigInt virtual_disk_count: BigInt
interface_count: BigInt
config_template: Annotated["ConfigTemplateType", strawberry.lazy('extras.graphql.types')] | None config_template: Annotated["ConfigTemplateType", strawberry.lazy('extras.graphql.types')] | None
site: Annotated["SiteType", strawberry.lazy('dcim.graphql.types')] | None site: Annotated["SiteType", strawberry.lazy('dcim.graphql.types')] | None
cluster: Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')] | None cluster: Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')] | None