Re: Custom user model backend

2014-05-29 Thread Kelvin Wong
Maybe try putting some logging in your CustomUserModelBackend.

Near the top, put in some logging boilerplate:

import logging
logger = logging.getLogger(__name__)

Then in your backend, something like this:

class CustomUserModelBackend(ModelBackend):
...
def get_user(self, user_id):
try:
return self.user_class.objects.get(pk=user_id)
except self.user_class.DoesNotExist:
logger.info('User.id={}: User not found, attaching 
AnonymousUser'.format(user_id))
return None

This backend method is checked by the AuthenticationMiddleware when it 
attaches the user to the request

https://github.com/django/django/blob/stable/1.6.x/django/contrib/auth/middleware.py#L18

https://github.com/django/django/blob/stable/1.6.x/django/contrib/auth/__init__.py#L133

When you make a backend like this it is also a good idea to make a test 
suite for it.

If these are all fruitless, maybe try looking at your session setup. I'm 
assuming you are running the db backend for the sessions. If you are using 
memcached or something else, check the usual settings (TIMEOUT, OPTIONS, 
etc)

K

On Wednesday, May 28, 2014 11:52:58 PM UTC-7, Domagoj Kovač wrote:
>
> I already know about SESSION_COOKIE_AGE. I set it to be 30 minutes, but 
> the problem is that even if i am doing something system logs me out, and 
> this should not happen. It work properly before i implemented custom auth 
> backend.
>

-- 
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+unsubscr...@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/3bade907-e3b1-4252-b7fd-6613d87a8cf8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Custom user model backend

2014-05-28 Thread Domagoj Kovač
I already know about SESSION_COOKIE_AGE. I set it to be 30 minutes, but the 
problem is that even if i am doing something system logs me out, and this 
should not happen. It work properly before i implemented custom auth 
backend.

-- 
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+unsubscr...@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/70f1aaa7-b578-41bb-a1eb-acc06a19d51a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Custom user model backend

2014-05-28 Thread Kelvin Wong
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+unsubscr...@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.


Custom user model backend

2014-05-28 Thread Domagoj Kovač
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+unsubscr...@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/07a5632b-62ff-4b41-b485-85f56f6cb21c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.