permission.validation: 15 total statements, 80.0% covered

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

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

Stats: 8 executed, 2 missed, 5 excluded, 55 ignored

  1. #!/usr/bin/env python
  2. # vim: set fileencoding=utf-8 :
  3. """
  4. Validation method called in ``registry.register`` method
  5. AUTHOR:
  6. lambdalisue[Ali su ae] (lambdalisue@hashnote.net)
  7. License:
  8. The MIT License (MIT)
  9. Copyright (c) 2012 Alisue allright reserved.
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to
  12. deal in the Software without restriction, including without limitation the
  13. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  14. sell copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. IN THE SOFTWARE.
  25. """
  26. from __future__ import with_statement
  27. from django.db import models
  28. from django.db.models.base import ModelBase
  29. from permission.handlers import PermissionHandler
  30. from permission.exceptions import ValidationError
  31. __all__ = ('validate',)
  32. def validate(cls, model):
  33. """
  34. Does basic PermissionHandler option validation. Calls custom validation
  35. classmethod in the end if it is provided in cls. The signature of the
  36. custom validation classmethod should be: def validate(cls, model).
  37. """
  38. # Before we can introspect models, they need to be fully loaded so that
  39. # inter-relations are set up correctly. We force that here.
  40. models.get_apps()
  41. # validate model class
  42. if not isinstance(model, ModelBase):
  43. # this mean the model is not subclass of models.Model
  44. raise ValidationError(
  45. 'The model "%s" must be a subclass of ``models.Model``.' % model.__class__.__name__
  46. )
  47. # validate handler class
  48. if not issubclass(cls, PermissionHandler):
  49. raise ValidationError(
  50. 'The handler "%s" must be a subclass of '
  51. '``fluidpermission.handlers.PermissionHandler``.' % cls.__class__.__name__
  52. )
  53. # call custom validation classmethod
  54. if hasattr(cls, 'validate'):
  55. cls.validate(model)