如何为会员模型编写自定义装饰器,以根据其会员类型(基于功能和类的视图)对用户进行身份验证

时间:2019-03-27 18:30:08

标签: django-models django-views

我想设置自定义装饰器,以根据用户的成员身份对用户进行身份验证

这是针对django2模型的。我尝试使用user_passes_test来限制对未经身份验证的用户的访问,但它不会根据不同的成员资格类型授予访问权限

models.py

MEMBERSHIP_CHOICES = (
    ('Platinum', 'plt'),
    ('Professional', 'pro'),
    ('Educational', 'edu')
)

class Membership(models.Model):
    slug = models.SlugField()
    membership_type = models.CharField(
            choices=MEMBERSHIP_CHOICES,
            default='edu',
            max_length=30)
    price = models.IntegerField(default=15)
    stripe_plan_id = models.CharField(max_length=40)

    def __str__(self):
        return self.membership_type


class UserMembership(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    stripe_customer_id = models.CharField(max_length=40)
    membership = models.ForeignKey(Membership, on_delete=models.SET_NULL, null=True)

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

def post_save_usermembership_create(sender, instance, created, *args, **kwargs):
    if created:
        UserMembership.objects.get_or_create(user=instance)

    user_membership, created = UserMembership.objects.get_or_create(user=instance)

    if user_membership.stripe_customer_id is None or user_membership.stripe_customer_id == '':
        new_customer_id = stripe.Customer.create(email=instance.email)
        user_membership.stripe_customer_id = new_customer_id['id']
        user_membership.save()

post_save.connect(post_save_usermembership_create, sender=settings.AUTH_USER_MODEL)


class Subscription(models.Model):
    user_membership = models.ForeignKey(UserMembership, on_delete=models.CASCADE)
    stripe_subscription_id = models.CharField(max_length=40)
    active = models.BooleanField(default=True)

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

    @property
    def get_created_date(self):
        subscription = stripe.Subscription.retrieve(self.stripe_subscription_id)
        return datetime.fromtimestamp(subscription.created)

    @property
    def get_next_billing_date(self):
        subscription = stripe.Subscription.retrieve(self.stripe_subscription_id)
        return datetime.fromtimestamp(subscription.current_period_end)


decorators.py

def professional_membership_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='account_login'):


    actual_decorator = user_passes_test(
        lambda u: u.is_active and Membership.membership_type == 'Professional',
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if function:
        return actual_decorator(function)
    return actual_decorator

views.py

@method_decorator([login_required, professional_membership_required], name='dispatch')
class DashboardPage(TemplateView):
    template_name = 'dashboard.html'

我希望根据我的会员资格类型对该页面进行身份验证或重定向到登录页面,但是允许所有注册用户访问该页面。

0 个答案:

没有答案