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