In my app, a subscription is required for 95% of the features. I'm wondering if it's better to add decorators to almost all functions requiring a subscription or to use middleware to manage access. The middleware approach could allow me to exclude the few pages that don't need a subscription, so I wouldn't have to worry about it when creating new views. Which method is more effective?
#middleware or decorators for access
12 messages · Page 1 of 1 (latest)
subscriptions => user + something else, or is subscription == active user ?
If it is just "must have an active account to view", you can use LoginRequiredMiddleware and login_not_required decorator
it is something else, like Stripe payments etc.
So they have to have a active user with "payment=up2date" ?
but yeah, i would probably do something like the LoginRequiredMiddleware from 5.1.
You could probably subclass it to fit your needs as well
Sorry, I am not clear -
I already have login needed set on views.
What i want to implement is for certain pages for which they need to do a payment (so they have access). This is stored in the user model.
And I thought it is probably better to write a middleware for it, since it is more maintainable. so everything is 'blocked' from non subscribed users be default, and i explicitly allow the url paths that are allowed to go through without a active subscription.
yes?
LoginRequiredMiddleware in django 5.1 does about what you want. Sublass it if you are on 5.1, backport if if you are on 4.2 and make it also check whatever attribute reflects a customer in good standing. https://github.com/django/django/blob/stable/5.1.x/django/contrib/auth/middleware.py#L41
thank you 🙂
looks like you should only have to add a few lines to process_view