These are chat archives for django/django

Jun 2016
Vasily Alexandrov
Jun 18 2016 04:58 UTC

Hi everyone!
Need some help with CBV and UserPassesTestMixin. I was going to use it to check if user is viewing his own page, using a simple test:

class DocBaseView(LoginRequiredMixin, UserPassesTestMixin, View):
    def test_func(self):
        return request.user.username == re.findall(r'/(w+)', request.path)[0]

class DocView(DocBaseView):
    # actual view code

The problem is that the test function doesn't have a request parameter and I don't understand if there is a pythonic way to make this check done. I have many views with this same check, so I wanted to define it in a parent class and keep the code from repeats. Anyone got any ideas of how it can be done?

Filip Figiel
Jun 18 2016 05:43 UTC
You can override the dispatch method and raise 403 / 404 on test failure
Oh wait I'm dumb
You can access request via self
Filip Figiel
Jun 18 2016 05:48 UTC
That's exactly what they do in the docs, have you looked at them?