Home > Software engineering >  How to serve premium content base on user subscription in django
How to serve premium content base on user subscription in django

Time:12-07

Hello guys i hope the way i ask this questions meets the standard way of stackoverflow. I have a projects i'm working on. On this project you need to subscribe a plan (monthly, 3 months or 6 months) to gain access to premium blog post contents and if you are not subscribed but registered, you will only get access to regular blog post contents. I've been able to integrate payment api and also the payment is working, i've also been able to save the payment details on a table in my database. What i need now is how do i filter the user based on their subscription status to serve them the premium or regular contents.Below is my Models and Views.

# In models.py
class User(AbstractBaseUser, PermissionsMixin):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
    username = models.CharField(max_length=200, unique=True, blank=True)
    email = models.EmailField(max_length=200, unique=True, blank=False)
    first_name = models.CharField(max_length=200, blank=True)
    last_name = models.CharField(max_length=200, blank=True)
    phone_no = models.CharField(max_length=11,validators=[MaxLengthValidator(11)])
    profile_picture = models.ImageField(upload_to="images/profile_image",default="default_user_image.jpeg", blank=True, null=True)
    is_active = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    date_joined = models.DateTimeField(auto_now_add=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    objects = UserManager()
    EMAIL_FIELD = 'email'
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email',]

    def __str__(self):
        return self.username

    def get_absolute_url(self):
        return reverse("admin-update-user", kwargs={"pk": self.pk})


class Blog(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(blank=False, null=False)
    author = models.ForeignKey(User, on_delete=models.CASCADE, default=True)
    post = RichTextUploadingField()
    featured_stories = models.BooleanField(default=False)
    latest_news = models.BooleanField(default=False)
    latest_articles = models.BooleanField(default=False)
    featured_image = models.ImageField(null=True, blank=True, default="default.png", upload_to="blog_images/")
    premium = models.BooleanField(default=False)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):
        return self.title

    def snippet(self):
        return self.post[:150]

    def get_absolute_url(self):
        return reverse("blog-detail", kwargs={"pk": self.pk})

#subscription plan for premium blog views 
class Plan(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    desc = models.TextField()
    price = models.IntegerField(default=0)
    discount_price = models.IntegerField(default=0)
    discount= models.IntegerField(default=0)
    created_on = models.DateTimeField(auto_now_add=True)
    class Meta:
        ordering = ("created_on",)

    def __str__(self):
        return self.title


# this saves the subscription record which will be used to filter premium blog from the blog model and assoicate it to users
class SubscriptionHistory(models.Model):
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    email = models.EmailField(max_length=200, unique=True, blank=False)
    full_name = models.CharField(max_length=200, blank=False)
    phone_no = models.CharField(max_length=11, unique=True, blank=False)
    plan = models.ForeignKey(Plan, on_delete=models.CASCADE, default='monthly plan')
    amount_paid = models.IntegerField(default=0)
    reference = models.CharField(max_length=200, unique=True, blank=False)
    transaction_id = models.CharField(max_length=200)
    status = models.CharField(max_length=200)
    start_date = models.DateTimeField(auto_now_add=True)
    expiry_date = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=False)

    def __str__(self):
        return self.user.username


# in views.py
# blog list
def blog(request):
    template_name = 'cms/blog.html'
    blogs = Blog.objects.all()
    user = request.user
    subscribed_user = SubscriptionHistory.objects.filter(user=user, active=True)
    featured_story =Blog.objects.filter(featured_stories=True)
    latest_new =Blog.objects.filter(latest_news=True)
    latest_article =Blog.objects.filter(latest_articles=True)
    premium_blogs = Blog.objects.filter(premium=True)
    context = {
        'blogs':blogs,
        'featured_story':featured_story,
        'latest_new':latest_new,
        'latest_article':latest_article,
        "premium_blog":premium_blogs
    }
    return render(request, template_name, context)

So what i need help with is how to serve premium posts based on the users who are registered and subscribed to a plan, present inside the SubscriptionHistory table and serve just regular posts to users who are not subscribed but registered, Thanks.

CodePudding user response:

def blog(request):
template_name = 'cms/blog.html'
blogs = Blog.objects.all()
user = request.user
subscribed_user = SubscriptionHistory.objects.filter(user=user, active=True)
featured_story = Blog.objects.filter(featured_stories=True)
latest_new = Blog.objects.filter(latest_news=True)
latest_article = Blog.objects.filter(latest_articles=True)
premium_blogs = Blog.objects.filter(premium=True)
context_regular = {
    'blogs': blogs,
    'featured_story': featured_story,
    'latest_new': latest_new,
    'latest_article': latest_article,
    "premium_blog": premium_blogs
}
context_premium = {
    'blogs': blogs,
    'featured_story': featured_story,
    'latest_new': latest_new,
    'latest_article': latest_article,
    "premium_blog": premium_blogs
}
if subscribed_user:
    return render(request, template_name, context_premium)
else:
    return render(request, template_name, context_regular)

CodePudding user response:

On my subscription based site I just have a boolean field in my user model 'paid' with the default of false.

In the subscription function it gets set to true and I just put a simple if statement before any content that is reserved for paid users.

    {% if user.paid %}
        *Paid user content*
    {% else %}
        Please subscribe HERE to see this content
    {% endif %}
  • Related