M
M
Mikkkch2021-09-29 19:42:08
Django
Mikkkch, 2021-09-29 19:42:08

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


It is possible that my decision is correct, but I would still like to implement such a system in a better way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2021-09-29
@Mikkkch

In a separate application, there is a UserPlan model

In my opinion, it makes sense to take out all the logic of working with the user (authorization / registration / models) in a separate application.
the restriction logic is located in __init__ (or another file).

middleware to help.
def check_can_user_create_post_now(user):
    if Post.objects.filter(created_at=today).count() == user.plan.posts_number_per_day:
        return False

Specifically, in this form, the code will break the whole logic, since there is a selection from the posts of all users.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question