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.

utils.py 2.1KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import functools
  2. from importlib import import_module
  3. from django.core.exceptions import ViewDoesNotExist
  4. from django.utils.module_loading import module_has_submodule
  5. @functools.lru_cache(maxsize=None)
  6. def get_callable(lookup_view):
  7. """
  8. Return a callable corresponding to lookup_view.
  9. * If lookup_view is already a callable, return it.
  10. * If lookup_view is a string import path that can be resolved to a callable,
  11. import that callable and return it, otherwise raise an exception
  12. (ImportError or ViewDoesNotExist).
  13. """
  14. if callable(lookup_view):
  15. return lookup_view
  16. if not isinstance(lookup_view, str):
  17. raise ViewDoesNotExist(
  18. "'%s' is not a callable or a dot-notation path" % lookup_view
  19. )
  20. mod_name, func_name = get_mod_func(lookup_view)
  21. if not func_name: # No '.' in lookup_view
  22. raise ImportError(
  23. "Could not import '%s'. The path must be fully qualified." % lookup_view
  24. )
  25. try:
  26. mod = import_module(mod_name)
  27. except ImportError:
  28. parentmod, submod = get_mod_func(mod_name)
  29. if submod and not module_has_submodule(import_module(parentmod), submod):
  30. raise ViewDoesNotExist(
  31. "Could not import '%s'. Parent module %s does not exist."
  32. % (lookup_view, mod_name)
  33. )
  34. else:
  35. raise
  36. else:
  37. try:
  38. view_func = getattr(mod, func_name)
  39. except AttributeError:
  40. raise ViewDoesNotExist(
  41. "Could not import '%s'. View does not exist in module %s."
  42. % (lookup_view, mod_name)
  43. )
  44. else:
  45. if not callable(view_func):
  46. raise ViewDoesNotExist(
  47. "Could not import '%s.%s'. View is not callable."
  48. % (mod_name, func_name)
  49. )
  50. return view_func
  51. def get_mod_func(callback):
  52. # Convert 'django.views.news.stories.story_detail' to
  53. # ['django.views.news.stories', 'story_detail']
  54. try:
  55. dot = callback.rindex(".")
  56. except ValueError:
  57. return callback, ""
  58. return callback[:dot], callback[dot + 1 :]