I have an application that uses django.contrib.auth but does not use the Django built-in permission system. Instead, the views have the @login_required decoder and then check which group the user belongs to and follow the different branches of code execution in the view depending on the group.
A user can belong to only one group.
Checking a user group at any time seems too big, so I'm trying to write Django middleware that will tell me a user group in a session.
Looking at the code below, will my middleware work the way I want?
class SetGroupMiddleware(object): def process_request(self, request): check_if_already_set = request.session.get('thegroup', 'notset') if check_if_already_set == 'notset': if request.user.id:
Then I intend to check request.session ['thegroup'] where necessary.
You need your suggestions and opinions. Is the session safe in this way? Will it even work? I am new to Django, Python, and general programming.
Thanks.
source share