From: Jan Szejko Date: Thu, 30 Aug 2018 09:20:13 +0000 (+0200) Subject: add real checking for subscription X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/d679452e8cede72656beb1e14b48a1b3e82e79c0 add real checking for subscription --- diff --git a/src/catalogue/utils.py b/src/catalogue/utils.py index 2145312ad..9878a70f2 100644 --- a/src/catalogue/utils.py +++ b/src/catalogue/utils.py @@ -20,6 +20,7 @@ from django.core.files.uploadedfile import UploadedFile from django.http import HttpResponse from django.utils.encoding import force_unicode +from paypal.rest import user_is_subscribed from reporting.utils import read_chunks # Use the system (hardware-based) random number generator if it exists. @@ -357,4 +358,4 @@ def gallery_url(slug): def is_subscribed(user): - return user.is_authenticated() # TEMPORARY + return user_is_subscribed(user) diff --git a/src/paypal/models.py b/src/paypal/models.py index 527e80458..80aa357a6 100644 --- a/src/paypal/models.py +++ b/src/paypal/models.py @@ -18,3 +18,11 @@ class BillingAgreement(models.Model): plan = models.ForeignKey(BillingPlan) active = models.BooleanField(max_length=32) token = models.CharField(max_length=32) + + def get_agreement(self): + from .rest import get_agreement + return get_agreement(self.agreement_id) + + def check_agreement(self): + from .rest import check_agreement + return check_agreement(self.agreement_id) diff --git a/src/paypal/rest.py b/src/paypal/rest.py index 1e0811b10..9d9f45ab9 100644 --- a/src/paypal/rest.py +++ b/src/paypal/rest.py @@ -11,7 +11,7 @@ from django.core.urlresolvers import reverse from django.utils import timezone from paypalrestsdk import BillingPlan, BillingAgreement, ResourceNotFound from django.conf import settings -from .models import BillingPlan as BillingPlanModel +from .models import BillingPlan as BillingPlanModel, BillingAgreement as BillingAgreementModel paypalrestsdk.configure(settings.PAYPAL_CONFIG) @@ -114,5 +114,13 @@ def check_agreement(agreement_id): return a.state == 'Active' +def user_is_subscribed(user): + try: + agreement = BillingAgreementModel.objects.get(user=user) + except BillingAgreementModel.DoesNotExist: + return False + return agreement.check_agreement() + + def execute_agreement(token): return BillingAgreement.execute(token)