mixins.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import graphene
  2. from django.contrib.contenttypes.models import ContentType
  3. from graphene.types.generic import GenericScalar
  4. from extras.models import ObjectChange
  5. __all__ = (
  6. 'ChangelogMixin',
  7. 'ConfigContextMixin',
  8. 'CustomFieldsMixin',
  9. 'ImageAttachmentsMixin',
  10. 'JournalEntriesMixin',
  11. 'TagsMixin',
  12. )
  13. class ChangelogMixin:
  14. changelog = graphene.List('extras.graphql.types.ObjectChangeType')
  15. def resolve_changelog(self, info):
  16. content_type = ContentType.objects.get_for_model(self)
  17. object_changes = ObjectChange.objects.filter(
  18. changed_object_type=content_type,
  19. changed_object_id=self.pk
  20. )
  21. return object_changes.restrict(info.context.user, 'view')
  22. class ConfigContextMixin:
  23. config_context = GenericScalar()
  24. def resolve_config_context(self, info):
  25. return self.get_config_context()
  26. class CustomFieldsMixin:
  27. custom_fields = GenericScalar()
  28. def resolve_custom_fields(self, info):
  29. return self.custom_field_data
  30. class ImageAttachmentsMixin:
  31. image_attachments = graphene.List('extras.graphql.types.ImageAttachmentType')
  32. def resolve_image_attachments(self, info):
  33. return self.images.restrict(info.context.user, 'view')
  34. class JournalEntriesMixin:
  35. journal_entries = graphene.List('extras.graphql.types.JournalEntryType')
  36. def resolve_journal_entries(self, info):
  37. return self.journal_entries.restrict(info.context.user, 'view')
  38. class TagsMixin:
  39. tags = graphene.List('extras.graphql.types.TagType')
  40. def resolve_tags(self, info):
  41. return self.tags.all()