Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. from django.conf import settings
  2. from django.http import HttpResponsePermanentRedirect
  3.  
  4.  
  5. class UserCheckMiddleware:
  6.  
  7. def process_request(self, request):
  8.  
  9. #Paths
  10. inactive_path = settings.INACTIVE_USER_URL
  11.  
  12. #First check if the user is logged in
  13. if request.user.is_authenticated():
  14.  
  15. #Check if the user has a valid membership
  16. if not request.user.profile.is_valid_member:
  17.  
  18. #Check if the path is the inactive_path to prevent endless redirectloop
  19. if not request.path in (inactive_path):
  20. #Redirect to inactive_path
  21. return HttpResponsePermanentRedirect(inactive_path)
  22.  
  23. class Profile(models.Model):
  24. user = models.OneToOneField(User)
  25. memberend = models.DateTimeField(blank=True, null=True)
  26.  
  27.  
  28.  
  29. def is_valid_member(self):
  30. if self.memberend:
  31. #Return True if memberend is in the future and False if the date is in the past
  32. return self.memberend >= timezone.now()
  33. else:
  34. #Memberend is not set, the user is valid
  35. return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement