add real checking for subscription
authorJan Szejko <janek37@gmail.com>
Thu, 30 Aug 2018 09:20:13 +0000 (11:20 +0200)
committerJan Szejko <janek37@gmail.com>
Thu, 30 Aug 2018 09:20:13 +0000 (11:20 +0200)
src/catalogue/utils.py
src/paypal/models.py
src/paypal/rest.py

index 2145312..9878a70 100644 (file)
@@ -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)
index 527e804..80aa357 100644 (file)
@@ -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)
index 1e0811b..9d9f45a 100644 (file)
@@ -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)