def test_migrate_from_login(self): from django.contrib.sessions.models import Session as DjangoSession from django.contrib.sessions.backends.db import SessionStore as DjangoSessionStore try: call_command('migrate', 'sessions') call_command('clearsessions') user = User.objects.create_user('bouke', '', 'secret') session = DjangoSessionStore() session['_auth_user_id'] = user.id session.save() self.assertEqual(Session.objects.count(), 0) self.assertEqual(DjangoSession.objects.count(), 1) call_command('migratesessions') new_sessions = list(Session.objects.all()) self.assertEqual(len(new_sessions), 1) self.assertEqual(new_sessions[0].user, user) self.assertEqual(new_sessions[0].ip, '127.0.0.1') finally: call_command('migrate', 'sessions', 'zero')
def get_session(self, sid, authenticated=False): self.env.log.debug('Retrieving session for ID %r', sid) #db = self.env.get_db_cnx() #cursor = db.cursor() ss = DjangoSessionStore(session_key=sid) if not ss.exists(sid): ss.load() ss.save() sid = ss.session_key self.env.log.warning('create new django session') self.sid = sid self.authenticated = authenticated try: ds = DjangoSession.objects.get(pk=sid) if not ds: return session_data = ds.get_decoded() if session_data.has_key('_auth_user_id'): mb = DjangoModelBackend() self.django_user_data = mb.get_user(session_data['_auth_user_id']) self.authenticated = self.django_user_data.is_authenticated() self.last_visit = int(mktime(self.django_user_data.last_login.timetuple())) self.env.log.debug('authenticated as %s',self.django_user_data.username) else: self.django_user_data = DjangoAnonymousUser() self.last_visit = int(mktime(gmtime())) self.env.log.debug('anonymous request') except DjangoSession.objects.model.DoesNotExist: self.env.log.error('session does not exist') self._new = False