Answer the question
In order to leave comments, you need to log in
How to implement a system of restrictions depending on the user's plan?
About the problem:
There is a set of plans, one of which the user chooses himself. The plan may be free.
Depending on the plan, certain restrictions are imposed on the user. These restrictions are related to the number of created objects of a particular model, let it be a model called Post.
For example, we need to limit the number of Post objects a user creates per day. I will also add that the ability to edit restrictions dynamically is extremely necessary.
Here's how I envision the solution:
In a separate application, there is a UserPlan model that lists the name of the plan, whether it's free or premium, and lists the restrictions imposed on a user with this plan regarding the Post model.
There are no views in this application, all the logic of restrictions is located in __init__ (or other file).
In the user model, of course, the connection with UserPlan is a foreign key.
From the application services responsible for working with the Post model (creation views, etc.), functions are pulled from the application with the logic of restrictions.
Example:
def check_can_user_create_post_now(user):
if Post.objects.filter(created_at=today).count() == user.plan.posts_number_per_day:
return False
Answer the question
In order to leave comments, you need to log in
In a separate application, there is a UserPlan model
the restriction logic is located in __init__ (or another file).
def check_can_user_create_post_now(user): if Post.objects.filter(created_at=today).count() == user.plan.posts_number_per_day: return False
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question