Django: DRY principle and UserPassesTestMixin
<p>I have a model named <code>Post</code> and have a field there called <code>owner</code> (foreign key to <code>User</code>). Of course, only owners can <code>update</code> or <code>delete</code> their own posts.</p>

<p>That being said, I use <code>login_required</code> decorator in the views to make sure the user is logged in but then, I also need to make sure the user trying to <code>update</code>/<code>delete</code> the question is the <code>owner</code>. </p>

<p>As I'm using <a href="https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-editing/" rel="noreferrer">Django: Generic Editing Views</a> the documentation says I need to use <a href="https://docs.djangoproject.com/en/1.9/topics/auth/default/#django.contrib.auth.mixins.UserPassesTestMixin" rel="noreferrer">Django: UserPassesTestMixin</a>. </p>

<p>This validation will be done for the <code>update</code> and <code>delete</code> views. DRY, what is the way to go about this? should I create a class named <code>TestUserOwnerOfPost</code> and create a <code>test_func()</code> and then make the <code>update</code> and <code>delete</code> views inherit from it? </p>

<p>Cause that's what I have tried and didn't work, code below: </p>

<pre><code>from django.views.generic.edit import UpdateView
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import UserPassesTestMixin

class TestUserOwnerOfPost(UserPassesTestMixin):                         
    def test_func(self):                                                       
        return self.request.user == self.post.owner

class EditPost(UpdateView, TestUserOwnerOfPost):                         
    model = Post                                                                         
    @method_decorator(login_required)                                          
    def dispatch(self, *args, **kwargs):                                       
        return super(EditPost, self).dispatch(*args, **kwargs)
</code></pre>

<p>With the code above, every logged-in user in the system can <code>edit</code>/<code>delete</code> any post. What am I doing wrong? am I missing something? thanks.</p>