mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-25 01:48:38 -06:00
9856 extras and mixins
This commit is contained in:
parent
700f015942
commit
46b0df43f9
149
netbox/extras/graphql/filters.py
Normal file
149
netbox/extras/graphql/filters.py
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
import strawberry
|
||||||
|
from strawberry import auto
|
||||||
|
from extras import models, filtersets
|
||||||
|
|
||||||
|
__all__ = (
|
||||||
|
'ConfigContextFilter',
|
||||||
|
'ConfigTemplateFilter',
|
||||||
|
'CustomFieldFilter',
|
||||||
|
'CustomFieldChoiceSetFilter',
|
||||||
|
'CustomLinkFilter',
|
||||||
|
'ExportTemplateFilter',
|
||||||
|
'ImageAttachmentFilter',
|
||||||
|
'JournalEntryFilter',
|
||||||
|
'ObjectChangeFilter',
|
||||||
|
'SavedFilterFilter',
|
||||||
|
'TagFilter',
|
||||||
|
'WebhookFilter',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.ConfigContext, lookups=True)
|
||||||
|
class ConfigContextFilter(filtersets.ConfigContextFilterSet):
|
||||||
|
id: auto
|
||||||
|
name: auto
|
||||||
|
is_active: auto
|
||||||
|
data_synced: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.ConfigTemplate, lookups=True)
|
||||||
|
class ConfigTemplateFilter(filtersets.ConfigTemplateFilterSet):
|
||||||
|
id: auto
|
||||||
|
name: auto
|
||||||
|
description: auto
|
||||||
|
data_synced: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.CustomField, lookups=True)
|
||||||
|
class CustomFieldFilter(filtersets.CustomFieldFilterSet):
|
||||||
|
id: auto
|
||||||
|
content_types: auto
|
||||||
|
name: auto
|
||||||
|
group_name: auto
|
||||||
|
required: auto
|
||||||
|
search_weight: auto
|
||||||
|
filter_logic: auto
|
||||||
|
ui_visibility: auto
|
||||||
|
weight: auto
|
||||||
|
is_cloneable: auto
|
||||||
|
description: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.CustomFieldChoiceSet, lookups=True)
|
||||||
|
class CustomFieldChoiceSetFilter(filtersets.CustomFieldChoiceSetFilterSet):
|
||||||
|
id: auto
|
||||||
|
name: auto
|
||||||
|
description: auto
|
||||||
|
base_choices: auto
|
||||||
|
order_alphabetically: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.CustomLink, lookups=True)
|
||||||
|
class CustomLinkFilter(filtersets.CustomLinkFilterSet):
|
||||||
|
id: auto
|
||||||
|
content_types: auto
|
||||||
|
name: auto
|
||||||
|
enabled: auto
|
||||||
|
link_text: auto
|
||||||
|
link_url: auto
|
||||||
|
weight: auto
|
||||||
|
group_name: auto
|
||||||
|
new_window: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.ExportTemplate, lookups=True)
|
||||||
|
class ExportTemplateFilter(filtersets.ExportTemplateFilterSet):
|
||||||
|
id: auto
|
||||||
|
content_types: auto
|
||||||
|
name: auto
|
||||||
|
description: auto
|
||||||
|
data_synced: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.ImageAttachment, lookups=True)
|
||||||
|
class ImageAttachmentFilter(filtersets.ImageAttachmentFilterSet):
|
||||||
|
id: auto
|
||||||
|
content_type_id: auto
|
||||||
|
object_id: auto
|
||||||
|
name: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.JournalEntry, lookups=True)
|
||||||
|
class JournalEntryFilter(filtersets.JournalEntryFilterSet):
|
||||||
|
id: auto
|
||||||
|
assigned_object_type_id: auto
|
||||||
|
assigned_object_id: auto
|
||||||
|
created: auto
|
||||||
|
kind: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.ObjectChange, lookups=True)
|
||||||
|
class ObjectChangeFilter(filtersets.ObjectChangeFilterSet):
|
||||||
|
id: auto
|
||||||
|
user: auto
|
||||||
|
user_name: auto
|
||||||
|
request_id: auto
|
||||||
|
action: auto
|
||||||
|
changed_object_type_id: auto
|
||||||
|
changed_object_id: auto
|
||||||
|
object_repr: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.SavedFilter, lookups=True)
|
||||||
|
class SavedFilterFilter(filtersets.SavedFilterFilterSet):
|
||||||
|
id: auto
|
||||||
|
content_types: auto
|
||||||
|
name: auto
|
||||||
|
slug: auto
|
||||||
|
description: auto
|
||||||
|
enabled: auto
|
||||||
|
shared: auto
|
||||||
|
weight: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.Tag, lookups=True)
|
||||||
|
class TagFilter(filtersets.TagFilterSet):
|
||||||
|
id: auto
|
||||||
|
name: auto
|
||||||
|
slug: auto
|
||||||
|
# color: auto
|
||||||
|
description: auto
|
||||||
|
object_types: auto
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.filter(models.Webhook, lookups=True)
|
||||||
|
class WebhookFilter(filtersets.WebhookFilterSet):
|
||||||
|
id: auto
|
||||||
|
name: auto
|
||||||
|
type_create: auto
|
||||||
|
type_update: auto
|
||||||
|
type_delete: auto
|
||||||
|
type_job_start: auto
|
||||||
|
type_job_end: auto
|
||||||
|
payload_url: auto
|
||||||
|
enabled: auto
|
||||||
|
http_method: auto
|
||||||
|
http_content_type: auto
|
||||||
|
secret: auto
|
||||||
|
ssl_verification: auto
|
||||||
|
ca_file_path: auto
|
@ -1,7 +1,7 @@
|
|||||||
import strawberry
|
import strawberry
|
||||||
import graphene
|
from typing import TYPE_CHECKING, Annotated, List
|
||||||
|
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from graphene.types.generic import GenericScalar
|
|
||||||
|
|
||||||
from extras.models import ObjectChange
|
from extras.models import ObjectChange
|
||||||
|
|
||||||
@ -14,11 +14,16 @@ __all__ = (
|
|||||||
'TagsMixin',
|
'TagsMixin',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .types import ImageAttachmentType, JournalEntryType, ObjectChangeType, TagType
|
||||||
|
from tenancy.graphql.types import ContactAssignmentType
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
class ChangelogMixin:
|
class ChangelogMixin:
|
||||||
changelog = graphene.List('extras.graphql.types.ObjectChangeType')
|
|
||||||
|
|
||||||
def resolve_changelog(self, info):
|
@strawberry.django.field
|
||||||
|
def changelog(self) -> List[Annotated["ObjectChangeType", strawberry.lazy('.types')]]:
|
||||||
content_type = ContentType.objects.get_for_model(self)
|
content_type = ContentType.objects.get_for_model(self)
|
||||||
object_changes = ObjectChange.objects.filter(
|
object_changes = ObjectChange.objects.filter(
|
||||||
changed_object_type=content_type,
|
changed_object_type=content_type,
|
||||||
@ -27,43 +32,49 @@ class ChangelogMixin:
|
|||||||
return object_changes.restrict(info.context.user, 'view')
|
return object_changes.restrict(info.context.user, 'view')
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
class ConfigContextMixin:
|
class ConfigContextMixin:
|
||||||
config_context = GenericScalar()
|
|
||||||
|
|
||||||
def resolve_config_context(self, info):
|
@strawberry.django.field
|
||||||
|
def config_context(self) -> strawberry.scalars.JSON:
|
||||||
return self.get_config_context()
|
return self.get_config_context()
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
class CustomFieldsMixin:
|
class CustomFieldsMixin:
|
||||||
custom_fields = GenericScalar()
|
|
||||||
|
|
||||||
def resolve_custom_fields(self, info):
|
@strawberry.django.field
|
||||||
|
def custom_fields(self) -> strawberry.scalars.JSON:
|
||||||
return self.custom_field_data
|
return self.custom_field_data
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
class ImageAttachmentsMixin:
|
class ImageAttachmentsMixin:
|
||||||
image_attachments = graphene.List('extras.graphql.types.ImageAttachmentType')
|
|
||||||
|
|
||||||
def resolve_image_attachments(self, info):
|
@strawberry.django.field
|
||||||
|
def image_attachments(self) -> List[Annotated["ImageAttachmentType", strawberry.lazy('.types')]]:
|
||||||
return self.images.restrict(info.context.user, 'view')
|
return self.images.restrict(info.context.user, 'view')
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
class JournalEntriesMixin:
|
class JournalEntriesMixin:
|
||||||
journal_entries = graphene.List('extras.graphql.types.JournalEntryType')
|
|
||||||
|
|
||||||
def resolve_journal_entries(self, info):
|
@strawberry.django.field
|
||||||
|
def journal_entries(self) -> List[Annotated["JournalEntryType", strawberry.lazy('.types')]]:
|
||||||
return self.journal_entries.restrict(info.context.user, 'view')
|
return self.journal_entries.restrict(info.context.user, 'view')
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
class TagsMixin:
|
class TagsMixin:
|
||||||
tags = graphene.List('extras.graphql.types.TagType')
|
|
||||||
|
|
||||||
def resolve_tags(self, info):
|
@strawberry.django.field
|
||||||
|
def tags(self) -> List[Annotated["TagType", strawberry.lazy('.types')]]:
|
||||||
return self.tags.all()
|
return self.tags.all()
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
class ContactsMixin:
|
class ContactsMixin:
|
||||||
contacts = graphene.List('tenancy.graphql.types.ContactAssignmentType')
|
|
||||||
|
|
||||||
def resolve_contacts(self, info):
|
@strawberry.django.field
|
||||||
|
def contacts(self) -> List[Annotated["ContactAssignmentType", strawberry.lazy('tenancy.graphql.types')]]:
|
||||||
return list(self.contacts.all())
|
return list(self.contacts.all())
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
|
import strawberry
|
||||||
|
from strawberry import auto
|
||||||
|
|
||||||
from extras import filtersets, models
|
from extras import filtersets, models
|
||||||
from extras.graphql.mixins import CustomFieldsMixin, TagsMixin
|
from extras.graphql.mixins import CustomFieldsMixin, TagsMixin
|
||||||
from netbox.graphql.types import BaseObjectType, ObjectType, OrganizationalObjectType
|
from netbox.graphql.types import BaseObjectType, ObjectType, OrganizationalObjectType
|
||||||
|
from .filters import *
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'ConfigContextType',
|
'ConfigContextType',
|
||||||
@ -18,97 +22,109 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.ConfigContext,
|
||||||
|
fields='__all__',
|
||||||
|
filters=ConfigContextFilter
|
||||||
|
)
|
||||||
class ConfigContextType(ObjectType):
|
class ConfigContextType(ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.ConfigContext
|
|
||||||
fields = '__all__'
|
|
||||||
filterset_class = filtersets.ConfigContextFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.ConfigTemplate,
|
||||||
|
fields='__all__',
|
||||||
|
filters=ConfigTemplateFilter
|
||||||
|
)
|
||||||
class ConfigTemplateType(TagsMixin, ObjectType):
|
class ConfigTemplateType(TagsMixin, ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.ConfigTemplate
|
|
||||||
fields = '__all__'
|
|
||||||
filterset_class = filtersets.ConfigTemplateFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.CustomField,
|
||||||
|
fields='__all__',
|
||||||
|
filters=CustomFieldFilter
|
||||||
|
)
|
||||||
class CustomFieldType(ObjectType):
|
class CustomFieldType(ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.CustomField
|
|
||||||
exclude = ('content_types', )
|
|
||||||
filterset_class = filtersets.CustomFieldFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.CustomFieldChoiceSet,
|
||||||
|
fields='__all__',
|
||||||
|
filters=CustomFieldChoiceSetFilter
|
||||||
|
)
|
||||||
class CustomFieldChoiceSetType(ObjectType):
|
class CustomFieldChoiceSetType(ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.CustomFieldChoiceSet
|
|
||||||
fields = '__all__'
|
|
||||||
filterset_class = filtersets.CustomFieldChoiceSetFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.CustomLink,
|
||||||
|
fields='__all__',
|
||||||
|
filters=CustomLinkFilter
|
||||||
|
)
|
||||||
class CustomLinkType(ObjectType):
|
class CustomLinkType(ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.CustomLink
|
|
||||||
exclude = ('content_types', )
|
|
||||||
filterset_class = filtersets.CustomLinkFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.ExportTemplate,
|
||||||
|
fields='__all__',
|
||||||
|
filters=ExportTemplateFilter
|
||||||
|
)
|
||||||
class ExportTemplateType(ObjectType):
|
class ExportTemplateType(ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.ExportTemplate
|
|
||||||
exclude = ('content_types', )
|
|
||||||
filterset_class = filtersets.ExportTemplateFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.ImageAttachment,
|
||||||
|
fields='__all__',
|
||||||
|
filters=ImageAttachmentFilter
|
||||||
|
)
|
||||||
class ImageAttachmentType(BaseObjectType):
|
class ImageAttachmentType(BaseObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.ImageAttachment
|
|
||||||
fields = '__all__'
|
|
||||||
filterset_class = filtersets.ImageAttachmentFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.JournalEntry,
|
||||||
|
fields='__all__',
|
||||||
|
filters=JournalEntryFilter
|
||||||
|
)
|
||||||
class JournalEntryType(CustomFieldsMixin, TagsMixin, ObjectType):
|
class JournalEntryType(CustomFieldsMixin, TagsMixin, ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.JournalEntry
|
|
||||||
fields = '__all__'
|
|
||||||
filterset_class = filtersets.JournalEntryFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.ObjectChange,
|
||||||
|
fields='__all__',
|
||||||
|
filters=ObjectChangeFilter
|
||||||
|
)
|
||||||
class ObjectChangeType(BaseObjectType):
|
class ObjectChangeType(BaseObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.ObjectChange
|
|
||||||
fields = '__all__'
|
|
||||||
filterset_class = filtersets.ObjectChangeFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.SavedFilter,
|
||||||
|
exclude=['content_types',],
|
||||||
|
filters=SavedFilterFilter
|
||||||
|
)
|
||||||
class SavedFilterType(ObjectType):
|
class SavedFilterType(ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.SavedFilter
|
|
||||||
exclude = ('content_types', )
|
|
||||||
filterset_class = filtersets.SavedFilterFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.Tag,
|
||||||
|
exclude=['extras_taggeditem_items', 'color'], # bug - remove color from exclude
|
||||||
|
filters=TagFilter
|
||||||
|
)
|
||||||
class TagType(ObjectType):
|
class TagType(ObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.Tag
|
|
||||||
exclude = ('extras_taggeditem_items',)
|
|
||||||
filterset_class = filtersets.TagFilterSet
|
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
models.Webhook,
|
||||||
|
exclude=['content_types',],
|
||||||
|
filters=WebhookFilter
|
||||||
|
)
|
||||||
class WebhookType(OrganizationalObjectType):
|
class WebhookType(OrganizationalObjectType):
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = models.Webhook
|
|
||||||
exclude = ('content_types', )
|
|
||||||
filterset_class = filtersets.WebhookFilterSet
|
|
||||||
|
@ -21,23 +21,24 @@ __all__ = (
|
|||||||
# Base types
|
# Base types
|
||||||
#
|
#
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
class BaseObjectType:
|
class BaseObjectType:
|
||||||
"""
|
"""
|
||||||
Base GraphQL object type for all NetBox objects. Restricts the model queryset to enforce object permissions.
|
Base GraphQL object type for all NetBox objects. Restricts the model queryset to enforce object permissions.
|
||||||
"""
|
"""
|
||||||
display: auto
|
|
||||||
class_type: auto
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_queryset(cls, queryset, info):
|
def get_queryset(cls, queryset, info):
|
||||||
# Enforce object permissions on the queryset
|
# Enforce object permissions on the queryset
|
||||||
return queryset.restrict(info.context.request.user, 'view')
|
return queryset.restrict(info.context.request.user, 'view')
|
||||||
|
|
||||||
def resolve_display(parent, info, **kwargs):
|
@strawberry.django.field
|
||||||
return str(parent)
|
def display(self) -> str:
|
||||||
|
return str(self)
|
||||||
|
|
||||||
def resolve_class_type(parent, info, **kwargs):
|
@strawberry.django.field
|
||||||
return parent.__class__.__name__
|
def class_type(self) -> str:
|
||||||
|
return self.__class__.__name__
|
||||||
|
|
||||||
|
|
||||||
class ObjectType(
|
class ObjectType(
|
||||||
@ -79,8 +80,9 @@ class NetBoxObjectType(
|
|||||||
# Miscellaneous types
|
# Miscellaneous types
|
||||||
#
|
#
|
||||||
|
|
||||||
|
@strawberry.django.type(
|
||||||
|
ContentType,
|
||||||
|
fields=['id', 'app_label', 'model'],
|
||||||
|
)
|
||||||
class ContentTypeType:
|
class ContentTypeType:
|
||||||
|
pass
|
||||||
class Meta:
|
|
||||||
model = ContentType
|
|
||||||
fields = ('id', 'app_label', 'model')
|
|
||||||
|
Loading…
Reference in New Issue
Block a user