1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
Files
netbox-community-netbox/netbox/extras/graphql/mixins.py

82 lines
2.2 KiB
Python
Raw Normal View History

2023-08-24 14:54:24 -07:00
import strawberry
2024-01-03 17:20:13 -08:00
import strawberry_django
2023-08-25 13:31:10 -07:00
from typing import TYPE_CHECKING, Annotated, List
from django.contrib.contenttypes.models import ContentType
from extras.models import ObjectChange
__all__ = (
'ChangelogMixin',
'ConfigContextMixin',
2021-08-03 13:49:12 -04:00
'CustomFieldsMixin',
'ImageAttachmentsMixin',
'JournalEntriesMixin',
2021-08-03 13:49:12 -04:00
'TagsMixin',
)
2023-08-25 13:31:10 -07:00
if TYPE_CHECKING:
from .types import ImageAttachmentType, JournalEntryType, ObjectChangeType, TagType
from tenancy.graphql.types import ContactAssignmentType
2023-08-25 13:31:10 -07:00
@strawberry.type
class ChangelogMixin:
2024-01-03 17:20:13 -08:00
@strawberry_django.field
2024-03-05 08:30:34 -08:00
def changelog(self, info) -> List[Annotated["ObjectChangeType", strawberry.lazy('.types')]]:
content_type = ContentType.objects.get_for_model(self)
object_changes = ObjectChange.objects.filter(
changed_object_type=content_type,
changed_object_id=self.pk
)
2024-03-05 08:30:34 -08:00
return object_changes.restrict(info.context.request.user, 'view')
2023-08-25 13:31:10 -07:00
@strawberry.type
class ConfigContextMixin:
2024-01-03 17:20:13 -08:00
@strawberry_django.field
2023-08-25 13:31:10 -07:00
def config_context(self) -> strawberry.scalars.JSON:
return self.get_config_context()
2023-08-25 13:31:10 -07:00
@strawberry.type
2021-08-03 13:49:12 -04:00
class CustomFieldsMixin:
2024-01-03 17:20:13 -08:00
@strawberry_django.field
2023-08-25 13:31:10 -07:00
def custom_fields(self) -> strawberry.scalars.JSON:
2021-08-03 13:49:12 -04:00
return self.custom_field_data
2023-08-25 13:31:10 -07:00
@strawberry.type
class ImageAttachmentsMixin:
2024-01-03 17:20:13 -08:00
@strawberry_django.field
2024-03-05 08:30:34 -08:00
def image_attachments(self, info) -> List[Annotated["ImageAttachmentType", strawberry.lazy('.types')]]:
return self.images.restrict(info.context.request.user, 'view')
2021-08-03 13:49:12 -04:00
2023-08-25 13:31:10 -07:00
@strawberry.type
class JournalEntriesMixin:
2024-01-03 17:20:13 -08:00
@strawberry_django.field
2024-03-05 08:30:34 -08:00
def journal_entries(self, info) -> List[Annotated["JournalEntryType", strawberry.lazy('.types')]]:
return self.journal_entries.restrict(info.context.request.user, 'view')
2023-08-25 13:31:10 -07:00
@strawberry.type
2021-08-03 13:49:12 -04:00
class TagsMixin:
2024-01-03 17:20:13 -08:00
@strawberry_django.field
2023-08-25 13:31:10 -07:00
def tags(self) -> List[Annotated["TagType", strawberry.lazy('.types')]]:
2021-08-03 13:49:12 -04:00
return self.tags.all()
2023-08-25 13:31:10 -07:00
@strawberry.type
class ContactsMixin:
2024-01-03 17:20:13 -08:00
@strawberry_django.field
2023-08-25 13:31:10 -07:00
def contacts(self) -> List[Annotated["ContactAssignmentType", strawberry.lazy('tenancy.graphql.types')]]:
return list(self.contacts.all())