forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
51 lines (38 loc) · 1.49 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
dj-stripe Decorators.
"""
from functools import wraps
from django.core.exceptions import ImproperlyConfigured
from django.shortcuts import redirect
from .settings import SUBSCRIPTION_REDIRECT, subscriber_request_callback
from .utils import subscriber_has_active_subscription
def subscriber_passes_pay_test(test_func, plan=None, pay_page=SUBSCRIPTION_REDIRECT):
"""
Decorator for views that checks the subscriber passes the given test for a
"Paid Feature".
Redirects to `pay_page` if necessary. The test should be a callable
that takes the subscriber object and returns True if the subscriber passes.
"""
def decorator(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
if test_func(subscriber_request_callback(request), plan):
return view_func(request, *args, **kwargs)
if not pay_page:
raise ImproperlyConfigured("DJSTRIPE_SUBSCRIPTION_REDIRECT is not set.")
return redirect(pay_page)
return _wrapped_view
return decorator
def subscription_payment_required(
function=None, plan=None, pay_page=SUBSCRIPTION_REDIRECT
):
"""
Decorator for views that require subscription payment.
Redirects to `pay_page` if necessary.
"""
actual_decorator = subscriber_passes_pay_test(
subscriber_has_active_subscription, plan=plan, pay_page=pay_page
)
if function:
return actual_decorator(function)
return actual_decorator