简体   繁体   English

如何在 django 中基于用户订阅提供优质内容

[英]How to serve premium content base on user subscription in django

Hello guys i hope the way i ask this questions meets the standard way of stackoverflow.大家好,我希望我提出这个问题的方式符合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.在这个项目上,您需要订阅一个计划(每月、3 个月或 6 个月)才能访问高级博客文章内容,如果您没有订阅但已注册,您将只能访问常规博客文章内容。 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.我已经能够集成付款 api 并且付款正在运行,我还能够将付款详细信息保存在数据库中的表格中。 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.所以我需要帮助的是如何根据注册和订阅计划的用户提供高级帖子,显示在 SubscriptionHistory 表中,并仅为未订阅但注册的用户提供常规帖子,谢谢。

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)

On my subscription based site I just have a boolean field in my user model 'paid' with the default of false.在我的基于订阅的网站上,我的用户 model 'paid' 中只有一个 boolean 字段,默认值为 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.在订阅 function 中,它设置为 true,我只是在为付费用户保留的任何内容之前放置一个简单的 if 语句。

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

So i finally found the solution based on the sample code @aina adesina provided and the code and this is what i did which makes it work as expected所以我终于根据@aina adesina 提供的示例代码和代码找到了解决方案,这就是我所做的,这使它按预期工作

def blog(request):
    template_name = 'cms/blog.html'
    blogs = Blog.objects.all()
    user = request.user
    if SubscriptionHistory.objects.filter(user=user, active=True).exists():
        featured_story = Blog.objects.filter(featured_stories=True, premium=True)
        latest_new = Blog.objects.filter(latest_news=True, premium=True)
        latest_article = Blog.objects.filter(latest_articles=True, premium=True)

        context = {
            'blogs':blogs,
            'featured_story':featured_story,
            'latest_new':latest_new,
            'latest_article':latest_article,
        }
        return render(request, template_name, context)
    else:
        featured_story = Blog.objects.filter(featured_stories=True, premium=False)
        latest_new = Blog.objects.filter(latest_news=True, premium=False)
        latest_article =Blog.objects.filter(latest_articles=True, premium=False)
    
        context = {
            'blogs':blogs,
            'featured_story':featured_story,
            'latest_new':latest_new,
            'latest_article':latest_article,
        }
        return render(request, template_name, context)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM