Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.conf import settings
- from django.http import HttpResponsePermanentRedirect
- class UserCheckMiddleware:
- def process_request(self, request):
- #Paths
- inactive_path = settings.INACTIVE_USER_URL
- #First check if the user is logged in
- if request.user.is_authenticated():
- #Check if the user has a valid membership
- if not request.user.profile.is_valid_member:
- #Check if the path is the inactive_path to prevent endless redirectloop
- if not request.path in (inactive_path):
- #Redirect to inactive_path
- return HttpResponsePermanentRedirect(inactive_path)
- class Profile(models.Model):
- user = models.OneToOneField(User)
- memberend = models.DateTimeField(blank=True, null=True)
- def is_valid_member(self):
- if self.memberend:
- #Return True if memberend is in the future and False if the date is in the past
- return self.memberend >= timezone.now()
- else:
- #Memberend is not set, the user is valid
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement