permission.decorators.utils: 94 total statements, 84.1% covered

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

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

Stats: 69 executed, 13 missed, 12 excluded, 73 ignored

  1. #!/usr/bin/env python
  2. # vim: set fileencoding=utf-8 :
  3. """
  4. Utils used in decorators
  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. import time
  28. import datetime
  29. import urlparse
  30. from django.conf import settings
  31. from django.http import Http404
  32. from django.db.models.fields import DateTimeField
  33. from django.shortcuts import get_object_or_404
  34. from django.contrib.auth import REDIRECT_FIELD_NAME
  35. from permission.exceptions import ValidationError
  36. try:
  37. from django.utils import timezone
  38. datetime_now = timezone.now
  39. except ImportError:
  40. datetime_now = datetime.datetime.now
  41. def redirect_to_login(request, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
  42. """redirect to login"""
  43. path = request.build_absolute_uri()
  44. # if the login url is the same scheme and net location then just
  45. # use the path as the "next" url.
  46. login_scheme, login_netloc = urlparse.urlparse(login_url or settings.LOGIN_URL)[:2]
  47. current_scheme, current_netloc = urlparse.urlparse(path)[:2]
  48. if ((not login_scheme or login_scheme == current_scheme) and
  49. (not login_netloc or login_netloc == current_netloc)):
  50. path = request.get_full_path()
  51. from django.contrib.auth.views import redirect_to_login as auth_redirect_to_login
  52. return auth_redirect_to_login(path, login_url, redirect_field_name)
  53. def get_object_from_classbased_instance(instance, queryset, request, *args, **kwargs):
  54. """get object from an instance of classbased generic view"""
  55. # initialize request, args, kwargs of classbased_instance
  56. # most of methods of classbased view assumed these attributes
  57. # but these attributes is initialized in ``dispatch`` method.
  58. instance.request = request
  59. instance.args = args
  60. instance.kwargs = kwargs
  61. # get queryset from class if ``queryset_or_model`` is not specified
  62. if hasattr(instance, 'get_queryset') and not queryset:
  63. queryset = instance.get_queryset()
  64. elif hasattr(instance, 'queryset') and not queryset:
  65. queryset = instance.queryset
  66. elif hasattr(instance, 'model') and not queryset:
  67. queryset = instance.model._default_manager.all()
  68. # get object
  69. if hasattr(instance, 'get_object'):
  70. obj = instance.get_object(queryset)
  71. elif hasattr(instance, 'object'):
  72. obj = instance.object
  73. else:
  74. obj = None
  75. return obj
  76. def get_object_from_list_detail_view(request, *args, **kwargs):
  77. """get object from generic list_detail.detail view"""
  78. queryset = kwargs['queryset']
  79. object_id = kwargs.get('object_id', None)
  80. slug = kwargs.get('slug', None)
  81. slug_field = kwargs.get('slug_field', 'slug')
  82. if object_id:
  83. obj = get_object_or_404(queryset, pk=object_id)
  84. elif slug and slug_field:
  85. obj = get_object_or_404(queryset, **{slug_field: slug})
  86. else:
  87. raise AttributeError(
  88. "Generic detail view must be called with either an "
  89. "object_id or a slug/slug_field."
  90. )
  91. return obj
  92. def _get_object_from_list_detail_view_validation(request, *args, **kwargs):
  93. if 'queryset' not in kwargs:
  94. return False
  95. elif 'object_id' not in kwargs and 'slug' not in kwargs:
  96. return False
  97. return True
  98. get_object_from_list_detail_view.validate = _get_object_from_list_detail_view_validation
  99. def get_object_from_date_based_view(request, *args, **kwargs):
  100. """get object from generic date_based.detail view"""
  101. year, month, day = kwargs['year'], kwargs['month'], kwargs['day']
  102. month_format = kwargs.get('month_format', '%b')
  103. day_format = kwargs.get('day_format', '%d')
  104. date_field = kwargs['date_field']
  105. queryset = kwargs['queryset']
  106. object_id = kwargs.get('object_id', None)
  107. slug = kwargs.get('slug', None)
  108. slug_field = kwargs.get('slug_field', 'slug')
  109. try:
  110. tt = time.strptime(
  111. '%s-%s-%s' % (year, month, day),
  112. '%s-%s-%s' % ('%Y', month_format, day_format)
  113. )
  114. date = datetime.date(*tt[:3])
  115. except ValueError:
  116. raise Http404
  117. model = queryset.model
  118. if isinstance(model._meta.get_field(date_field), DateTimeField):
  119. lookup_kwargs = {
  120. '%s__range' % date_field: (
  121. datetime.datetime.combine(date, datetime.time.min),
  122. datetime.datetime.combine(date, datetime.time.max),
  123. )}
  124. else:
  125. lookup_kwargs = {date_field: date}
  126. now = datetime_now()
  127. if date >= now.date() and not kwargs.get('allow_future', False):
  128. lookup_kwargs['%s__lte' % date_field] = now
  129. if object_id:
  130. lookup_kwargs['pk'] = object_id
  131. elif slug and slug_field:
  132. lookup_kwargs['%s__exact' % slug_field] = slug
  133. else:
  134. raise AttributeError(
  135. "Generic detail view must be called with either an "
  136. "object_id or a slug/slug_field."
  137. )
  138. return get_object_or_404(queryset, **lookup_kwargs)
  139. def _get_object_from_date_based_view_validation(request, *args, **kwargs):
  140. if 'queryset' not in kwargs:
  141. return False
  142. elif 'year' not in kwargs or 'month' not in kwargs or 'day' not in kwargs:
  143. return False
  144. elif 'object_id' not in kwargs and 'slug' not in kwargs:
  145. return False
  146. return True
  147. get_object_from_date_based_view.validate = _get_object_from_date_based_view_validation