permissions.py 957 B

123456789101112131415161718192021222324252627282930313233
  1. """
  2. .. module:: dj-stripe.contrib.rest_framework.permissions.
  3. :synopsis: dj-stripe - Permissions to be used with the dj-stripe REST API.
  4. """
  5. from rest_framework.permissions import BasePermission
  6. from ...settings import subscriber_request_callback
  7. from ...utils import subscriber_has_active_subscription
  8. class DJStripeSubscriptionPermission(BasePermission):
  9. """
  10. A permission to be used when wanting to permit users with active subscriptions.
  11. """
  12. def has_permission(self, request, view) -> bool:
  13. """
  14. Check if the subscriber has an active subscription.
  15. Returns false if:
  16. * a subscriber isn't passed through the request
  17. See ``utils.subscriber_has_active_subscription`` for more rules.
  18. """
  19. try:
  20. return subscriber_has_active_subscription(
  21. subscriber_request_callback(request)
  22. )
  23. except AttributeError:
  24. return False