3

I was stumbling on invitation feature to understand how it works. I found the blog post on http://www.pythondiary.com/tutorials/create-invite-only-website.html. There the author says (It checks the path as this will cause an infinite loop if not checked). How to use reverse function to check the path?

class Invite(models.Model):
    INVITE_CHOICES = (
    ('G', 'General'),
    ('I', 'Invitational'),
    )
    user = models.OneToOneField(User)
    cookie = models.UUIDField(default=uuid.uuid4)
    token = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)

    def __str__(self):
        return self.user.username

    # def get_absolute_url(self):
    #     return reverse('user_mgmt:confirm_invite', kwargs={'token': self.token})

class InvitedMiddleware(object):
    def process_request(self, req):
        if req.path == '/i.auth': # what path is this?
            return None
        if not req.user.is_authenticated():
            if 'token' in req.COOKIES:
                return redirect('user_mgmt.views.login_user')
        return None

    def process_response(self, req, resp):
        if req.user.is_authenticated():
            if req.user.is_staff:
                return resp
            if 'token' in req.COOKIES:
                token = req.COOKIES['token']
            else:
                invite = Invite.objects.get(user=req.user)
                token = invite.token
            resp.set_cookie('token', token, MAX_AGE=1209600)
        return resp
pythonBeginner
  • 781
  • 2
  • 12
  • 27
  • You must share your `urls.py` to help you with – nik_m Mar 14 '17 at 16:33
  • I have not understand the req.path so i have not created the urls yet. Is it the url/path for login? – pythonBeginner Mar 14 '17 at 16:42
  • In order to understand the `reverse` function (that's what you are asking, right?) you must implement the `urls.py` – nik_m Mar 14 '17 at 16:55
  • The problem is i did not understand the req.path part. What is he checking there? Which path he is refering? Can you show me an example what url you will create based on the condition he has used? Will it be different when using django-allauth? – pythonBeginner Mar 14 '17 at 16:58
  • In models.py i have used the reverse function and there i know why i have used reverse function and also what path to give but unaware on req.path part. What do you think it will be? Can you please make me the concept clear? – pythonBeginner Mar 14 '17 at 17:00

1 Answers1

0

OK. But first notice that this example uses the old Middleware declaration and syntax. When you are comfortable, switch to the new Middleware syntax. In addition it was written 5 years ago (!), where a lot has changed in Django since then!

Recalling from the docs:

You can think of it (the middleware) like an onion: each middleware class is a “layer” that wraps the view, which is in the core of the onion. If the request passes through all the layers of the onion [...], all the way to the view at the core, the response will then pass through every layer (in reverse order) on the way back out.

So, what the author does here is this:

Depending on the order of appearance of the InvitedMiddleware inside MIDDLEWARE setting, the InvitedMiddleware will be called before each view call. It checks if the request.path (i.e the url that is about to call a view, that is about to render a template - or not) is '/i.auth' (i.e 127.0.0.1:8000/i.auth/. If it is, then he returns None (the view is never called). Now, he does that, its unknown. He says that we'll get to what this path does later but he never does (!). Maybe there is a second series of this tutorial, who knows?

Anyway, if the requested path is not '/i.auth' then another check is performed. If the user is not authenticated. But I think its pretty straightforward from here on. Remember, all these happen before the view is called.

If, now you used the reverse method it should look like this:

if req.path == reverse('url_name', ...):

where ... are any potential positional args or/and kwargs.

But the author of that post, does not define somewhere this URL. Nor he is explain the structure of it. I think it is safe to pass that and continue with the rest.

nik_m
  • 11,825
  • 4
  • 43
  • 57