mixins.py 773 B

123456789101112131415161718192021222324252627
  1. from typing import Annotated, List, TYPE_CHECKING
  2. import strawberry
  3. import strawberry_django
  4. from django.contrib.contenttypes.models import ContentType
  5. from core.models import ObjectChange
  6. if TYPE_CHECKING:
  7. from netbox.core.graphql.types import ObjectChangeType
  8. __all__ = (
  9. 'ChangelogMixin',
  10. )
  11. @strawberry.type
  12. class ChangelogMixin:
  13. @strawberry_django.field
  14. def changelog(self, info) -> List[Annotated["ObjectChangeType", strawberry.lazy('.types')]]: # noqa: F821
  15. content_type = ContentType.objects.get_for_model(self)
  16. object_changes = ObjectChange.objects.filter(
  17. changed_object_type=content_type,
  18. changed_object_id=self.pk
  19. )
  20. return object_changes.restrict(info.context.request.user, 'view')