Search code examples
pythondjangodjango-taggit

Restrict certain users from adding new tags , django-taggit?


i'm quite new to django framework, i'm creating a blog application from scratch , and i am integrating django-taggit, to tag the articles. What im trying to accomplish is that , i want only certain users to be able to add new tags and the rest of them can only use the existing ones. It something like what stackoverflow implements, it allows users with certain amount of reputations to add new tags.

How do i achieve this ?


Solution

  • You could do a couple things. First I would definitely implement a UserProfile model of some kind, i.e. that has a reputation attribute, and then you have a bunch of options to accomplish your task, i.e.

    Use the @user_passes_test decorator, where you create your own function to pass to the decorator.

    def at_least_fifty_rep(user): 
        my_profile = ... # get the user profile
        return my_profile.reputation > 50
    
    @user_passes_test(at_least_fifty_rep)
    def my_custom_view(request):
        ...
    

    Alternatively, you could implement controls in the template as well.