permission.utils: 30 total statements, 26.3% covered

Generated: Thu 2012-03-01 14:22 CST

Source file: /home/alisue/Dropbox/Codes/django-permission/permission/utils.py

Stats: 5 executed, 14 missed, 11 excluded, 61 ignored

  1. # vim: set fileencoding=utf-8 :
  2. """
  3. Utilities
  4. AUTHOR:
  5. lambdalisue[Ali su ae] (lambdalisue@hashnote.net)
  6. License:
  7. The MIT License (MIT)
  8. Copyright (c) 2012 Alisue allright reserved.
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to
  11. deal in the Software without restriction, including without limitation the
  12. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  13. sell copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. IN THE SOFTWARE.
  24. """
  25. from __future__ import with_statement
  26. __all__ = ('get_permission_codename', 'autodiscover',)
  27. def get_permission_codename(perm):
  28. """get permission codename from permission string
  29. Usage:
  30. >>> get_permission_codename(u"app_label.codename_model")
  31. u"codename_model"
  32. >>> get_permission_codename(u"app_label.codename")
  33. u"codename"
  34. >>> get_permission_codename(u"codename_model")
  35. u"codename_model"
  36. >>> get_permission_codename(u"codename")
  37. u"codename"
  38. >>> get_permission_codename(u"app_label.app_label.codename_model")
  39. u"app_label.codename_model"
  40. """
  41. try:
  42. perm = perm.split('.', 1)[1]
  43. except IndexError:
  44. pass
  45. return perm
  46. def autodiscover(module_name=None):
  47. """
  48. Auto-discover INSTALLED_APPS permissions.py modules and fail silently when
  49. not present. This forces an import on them to register any permissions bits
  50. they may want.
  51. """
  52. import copy
  53. from django.conf import settings
  54. from django.utils.importlib import import_module
  55. from django.utils.module_loading import module_has_submodule
  56. from permission.handlers import registry
  57. module_name = module_name or settings.PERMISSION_MODULE_NAME
  58. for app in settings.INSTALLED_APPS:
  59. mod = import_module(app)
  60. # Attempt to import the app's permissions module
  61. try:
  62. before_import_registry = copy.copy(registry._registry)
  63. import_module('%s.%s' % (app, module_name))
  64. except:
  65. # Reset the model registry to the state before tha last import as
  66. # this import will have to reoccur on the next request and this
  67. # could raise NotRegistered and AlreadyRegistered exceptions
  68. # (see #8254)
  69. registry._registry = before_import_registry
  70. # Decide whether to bubble up this error. If the app just
  71. # doesn't have an permissions module, we can ignore the error
  72. # attempting to import it, otherwise we want it to bubble up.
  73. if module_has_submodule(mod, module_name):
  74. raise