mixins.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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 strawberry.types import Info
  6. from core.models import ObjectChange
  7. if TYPE_CHECKING:
  8. from core.graphql.types import DataFileType, DataSourceType, ObjectChangeType
  9. __all__ = (
  10. 'ChangelogMixin',
  11. 'SyncedDataMixin',
  12. )
  13. @strawberry.type
  14. class ChangelogMixin:
  15. @strawberry_django.field
  16. def changelog(self, info: Info) -> List[Annotated['ObjectChangeType', strawberry.lazy('.types')]]: # noqa: F821
  17. content_type = ContentType.objects.get_for_model(self)
  18. object_changes = ObjectChange.objects.filter(
  19. changed_object_type=content_type,
  20. changed_object_id=self.pk
  21. )
  22. return object_changes.restrict(info.context.request.user, 'view')
  23. @strawberry.type
  24. class SyncedDataMixin:
  25. data_source: Annotated['DataSourceType', strawberry.lazy('core.graphql.types')] | None
  26. data_file: Annotated['DataFileType', strawberry.lazy('core.graphql.types')] | None