Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

guardian.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435
  1. from django.contrib.contenttypes.models import ContentType
  2. def get_polymorphic_base_content_type(obj):
  3. """
  4. Helper function to return the base polymorphic content type id. This should used with django-guardian and the
  5. GUARDIAN_GET_CONTENT_TYPE option.
  6. See the django-guardian documentation for more information:
  7. https://django-guardian.readthedocs.io/en/latest/configuration.html#guardian-get-content-type
  8. """
  9. if hasattr(obj, "polymorphic_model_marker"):
  10. try:
  11. superclasses = list(obj.__class__.mro())
  12. except TypeError:
  13. # obj is an object so mro() need to be called with the obj.
  14. superclasses = list(obj.__class__.mro(obj))
  15. polymorphic_superclasses = list()
  16. for sclass in superclasses:
  17. if hasattr(sclass, "polymorphic_model_marker"):
  18. polymorphic_superclasses.append(sclass)
  19. # PolymorphicMPTT adds an additional class between polymorphic and base class.
  20. if hasattr(obj, "can_have_children"):
  21. root_polymorphic_class = polymorphic_superclasses[-3]
  22. else:
  23. root_polymorphic_class = polymorphic_superclasses[-2]
  24. ctype = ContentType.objects.get_for_model(root_polymorphic_class)
  25. else:
  26. ctype = ContentType.objects.get_for_model(obj)
  27. return ctype