mixins.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from typing import TYPE_CHECKING, Annotated, List
  2. import strawberry
  3. import strawberry_django
  4. from strawberry.types import Info
  5. __all__ = (
  6. 'ConfigContextMixin',
  7. 'ContactsMixin',
  8. 'CustomFieldsMixin',
  9. 'ImageAttachmentsMixin',
  10. 'JournalEntriesMixin',
  11. 'TagsMixin',
  12. )
  13. if TYPE_CHECKING:
  14. from .types import ImageAttachmentType, JournalEntryType, TagType
  15. from tenancy.graphql.types import ContactAssignmentType
  16. @strawberry.type
  17. class ConfigContextMixin:
  18. @strawberry_django.field
  19. def config_context(self) -> strawberry.scalars.JSON:
  20. return self.get_config_context()
  21. @strawberry.type
  22. class CustomFieldsMixin:
  23. @strawberry_django.field
  24. def custom_fields(self) -> strawberry.scalars.JSON:
  25. return self.custom_field_data
  26. @strawberry.type
  27. class ImageAttachmentsMixin:
  28. @strawberry_django.field
  29. def image_attachments(self, info: Info) -> List[Annotated['ImageAttachmentType', strawberry.lazy('.types')]]:
  30. return self.images.restrict(info.context.request.user, 'view')
  31. @strawberry.type
  32. class JournalEntriesMixin:
  33. @strawberry_django.field
  34. def journal_entries(self, info: Info) -> List[Annotated['JournalEntryType', strawberry.lazy('.types')]]:
  35. return self.journal_entries.all()
  36. @strawberry.type
  37. class TagsMixin:
  38. tags: List[Annotated['TagType', strawberry.lazy('.types')]]
  39. @strawberry.type
  40. class ContactsMixin:
  41. contacts: List[Annotated['ContactAssignmentType', strawberry.lazy('tenancy.graphql.types')]]