Wednesday, May 28, 2014

Re: Custom user model backend

In your settings.py you might have to set the SESSION_COOKIE_AGE

https://docs.djangoproject.com/en/1.4/topics/http/sessions/#session-cookie-age

K


On Wednesday, May 28, 2014 5:49:01 AM UTC-7, Domagoj Kovač wrote:
Hi guys,

I extended base user model with certain fields. My code is as follows:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.utils import timezone
from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_model
 
 
class CustomUserModelBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        try:
            user = self.user_class.objects.get(username=username)
            if user.login_duration is None or user.login_duration >= timezone.now():
                if user.check_password(password):
                    return user
            else:
                return None
        except self.user_class.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return self.user_class.objects.get(pk=user_id)
        except self.user_class.DoesNotExist:
            return None
    @property
    def user_class(self):
        if not hasattr(self, '_user_class'):
            self._user_class = get_model(*settings.CUSTOM_USER_MODEL.split('.', 2))
            if not self._user_class:
                raise ImproperlyConfigured('Could not get custom user model')
        return self._user_class

Everything works as it should but if i am logged in system logs me out after certain time even i am using application. Is there something i missed that controls how login persistence is handled?

Best,
Domagoj 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/755ef023-fc6a-46fb-8dc2-f4960e2e39db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment