当前位置: 首页>>代码示例>>Python>>正文


Python session.Session类代码示例

本文整理汇总了Python中trac.web.session.Session的典型用法代码示例。如果您正苦于以下问题:Python Session类的具体用法?Python Session怎么用?Python Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Session类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_change_anonymous_session

    def test_change_anonymous_session(self):
        """
        Verify that changing from one anonymous session to an inexisting
        anonymous session creates the new session and doesn't carry over
        variables from the previous session.
        """

        cursor = self.db.cursor()
        cursor.execute("INSERT INTO session VALUES ('123456', 0, 0)")
        cursor.execute("INSERT INTO session_attribute VALUES "
                       "('123456', 0, 'foo', 'bar')")

        incookie = Cookie()
        incookie['trac_session'] = '123456'
        req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                   outcookie=Cookie())
        session = Session(self.env, req)
        self.assertEqual({'foo': 'bar'}, session)
        
        session.get_session('7890')
        session['baz'] = 'moo'
        session.save()
        self.assertEqual({'baz': 'moo'}, session)

        cursor.execute("SELECT COUNT(*) FROM session WHERE sid='7890' AND "
                       "authenticated=0")
        self.assertEqual(1, cursor.fetchone()[0])
        cursor.execute("SELECT name, value FROM session_attribute "
                       "WHERE sid='7890' AND authenticated=0")
        self.assertEqual([('baz', 'moo')], cursor.fetchall())
开发者ID:wiraqutra,项目名称:photrackjp,代码行数:30,代码来源:session.py

示例2: test_delete_empty_session

    def test_delete_empty_session(self):
        """
        Verify that a session gets deleted when it doesn't have any data except
        for the 'last_visit' timestamp.
        """
        now = time.time()

        # Make sure the session has data so that it doesn't get dropped
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO session "
                       "VALUES ('123456', 0, %s)",
                       (int(now - UPDATE_INTERVAL - 3600),))
        cursor.execute("INSERT INTO session_attribute VALUES "
                       "('123456', 0, 'foo', 'bar')")

        incookie = Cookie()
        incookie['trac_session'] = '123456'
        req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                   outcookie=Cookie())
        session = Session(self.env, req)
        del session['foo']
        session.save()

        cursor.execute("SELECT COUNT(*) FROM session WHERE sid='123456' AND "
                       "authenticated=0")
        self.assertEqual(0, cursor.fetchone()[0])
开发者ID:wiraqutra,项目名称:photrackjp,代码行数:26,代码来源:session.py

示例3: test_purge_anonymous_session

 def test_purge_anonymous_session(self):
     """
     Verify that old sessions get purged.
     """
     with self.env.db_transaction as db:
         db("INSERT INTO session VALUES ('123456', 0, %s)", (0,))
         db("INSERT INTO session VALUES ('987654', 0, %s)",
            (int(time.time() - PURGE_AGE - 3600),))
         db("""
             INSERT INTO session_attribute
             VALUES ('987654', 0, 'foo', 'bar')
             """)
         
         # We need to modify a different session to trigger the purging
         incookie = Cookie()
         incookie['trac_session'] = '123456'
         req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                    outcookie=Cookie())
         session = Session(self.env, req)
         session['foo'] = 'bar'
         session.save()
     
     self.assertEqual(0, self.env.db_query("""
         SELECT COUNT(*) FROM session WHERE sid='987654' AND authenticated=0
         """)[0][0])
开发者ID:trac-ja,项目名称:trac-ja,代码行数:25,代码来源:session.py

示例4: test_authenticated_session_independence_var

    def test_authenticated_session_independence_var(self):
        """
        Verify that an anonymous session with the same name as an authenticated
        session doesn't interfere with the latter.
        """
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO session VALUES ('john', 1, 0)")
        cursor.execute("INSERT INTO session_attribute VALUES "
                       "('john', 1, 'foo', 'bar')")

        cursor.execute("SELECT value FROM session_attribute "
                       "WHERE sid='john' AND authenticated=1 AND name='foo'")
        self.assertEqual('bar', cursor.fetchone()[0])

        incookie = Cookie()
        incookie['trac_session'] = 'john'
        req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                   outcookie=Cookie())
        session = Session(self.env, req)
        self.assert_('foo' not in session)
        session['foo'] = 'baz'
        session.save()

        cursor.execute("SELECT value FROM session_attribute "
                       "WHERE sid='john' AND authenticated=1 AND name='foo'")
        rows = cursor.fetchall()
        self.assertEqual(1, len(rows))
        self.assertEqual('bar', rows[0][0])
        cursor.execute("SELECT value FROM session_attribute "
                       "WHERE sid='john' AND authenticated=0 AND name='foo'")
        rows = cursor.fetchall()
        self.assertEqual(1, len(rows))
        self.assertEqual('baz', rows[0][0])
开发者ID:wiraqutra,项目名称:photrackjp,代码行数:33,代码来源:session.py

示例5: test_delete_anonymous_session_var

    def test_delete_anonymous_session_var(self):
        """
        Verify that modifying a variable updates the 'session' table accordingly
        for an anonymous session.
        """
        with self.env.db_transaction as db:
            db("INSERT INTO session VALUES ('123456', 0, 0)")
            db(
                """
                INSERT INTO session_attribute VALUES
                ('123456', 0, 'foo', 'bar')
                """
            )
            incookie = Cookie()
            incookie["trac_session"] = "123456"
            req = Mock(authname="anonymous", base_path="/", incookie=incookie, outcookie=Cookie())
            session = Session(self.env, req)
            self.assertEqual("bar", session["foo"])
            del session["foo"]
            session.save()

        self.assertEqual(
            0,
            self.env.db_query(
                """
            SELECT COUNT(*) FROM session_attribute
            WHERE sid='123456' AND name='foo'
            """
            )[0][0],
        )
开发者ID:dafrito,项目名称:trac-mirror,代码行数:30,代码来源:session.py

示例6: test_delete_empty_session

    def test_delete_empty_session(self):
        """
        Verify that a session gets deleted when it doesn't have any data except
        for the 'last_visit' timestamp.
        """
        now = time.time()

        # Make sure the session has data so that it doesn't get dropped
        with self.env.db_transaction as db:
            db("INSERT INTO session VALUES ('123456', 0, %s)",
               (int(now - UPDATE_INTERVAL - 3600),))
            db("""
                INSERT INTO session_attribute
                VALUES ('123456', 0, 'foo', 'bar')
                """)

            incookie = Cookie()
            incookie['trac_session'] = '123456'
            req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                       outcookie=Cookie())
            session = Session(self.env, req)
            del session['foo']
            session.save()

        self.assertEqual(0, self.env.db_query("""
            SELECT COUNT(*) FROM session WHERE sid='123456' AND authenticated=0
            """)[0][0])
开发者ID:trac-ja,项目名称:trac-ja,代码行数:27,代码来源:session.py

示例7: test_change_anonymous_session

    def test_change_anonymous_session(self):
        """
        Verify that changing from one anonymous session to an inexisting
        anonymous session creates the new session and doesn't carry over
        variables from the previous session.
        """

        with self.env.db_transaction as db:
            db("INSERT INTO session VALUES ('123456', 0, 0)")
            db("""
                INSERT INTO session_attribute
                VALUES ('123456', 0, 'foo', 'bar')
                """)

        incookie = Cookie()
        incookie['trac_session'] = '123456'
        req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                   outcookie=Cookie())
        session = Session(self.env, req)
        self.assertEqual({'foo': 'bar'}, session)
        
        session.get_session('7890')
        session['baz'] = 'moo'
        session.save()
        self.assertEqual({'baz': 'moo'}, session)

        with self.env.db_query as db:
            self.assertEqual(1, db("""
                SELECT COUNT(*) FROM session
                WHERE sid='7890' AND authenticated=0
                """)[0][0])
            self.assertEqual([('baz', 'moo')], db("""
                SELECT name, value FROM session_attribute
                WHERE sid='7890' AND authenticated=0
                """))
开发者ID:trac-ja,项目名称:trac-ja,代码行数:35,代码来源:session.py

示例8: test_authenticated_session_independence_var

    def test_authenticated_session_independence_var(self):
        """
        Verify that an anonymous session with the same name as an authenticated
        session doesn't interfere with the latter.
        """
        with self.env.db_transaction as db:
            db("INSERT INTO session VALUES ('john', 1, 0)")
            db("INSERT INTO session_attribute VALUES ('john',1,'foo','bar')")

        self.assertEqual('bar', self.env.db_query("""
            SELECT value FROM session_attribute
            WHERE sid='john' AND authenticated=1 AND name='foo'
            """)[0][0])

        incookie = Cookie()
        incookie['trac_session'] = 'john'
        req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                   outcookie=Cookie())
        session = Session(self.env, req)
        self.assert_('foo' not in session)
        session['foo'] = 'baz'
        session.save()

        rows = self.env.db_query("""
            SELECT value FROM session_attribute
            WHERE sid='john' AND authenticated=1 AND name='foo'
            """)
        self.assertEqual(1, len(rows))
        self.assertEqual('bar', rows[0][0])
        rows = self.env.db_query("""
            SELECT value FROM session_attribute
            WHERE sid='john' AND authenticated=0 AND name='foo'
            """)
        self.assertEqual(1, len(rows))
        self.assertEqual('baz', rows[0][0])
开发者ID:trac-ja,项目名称:trac-ja,代码行数:35,代码来源:session.py

示例9: test_update_session

    def test_update_session(self):
        """
        Verify that accessing a session after one day updates the sessions 
        'last_visit' variable so that the session doesn't get purged.
        """
        now = time.time()

        # Make sure the session has data so that it doesn't get dropped
        with self.env.db_transaction as db:
            db("INSERT INTO session VALUES ('123456', 0, 1)")
            db("""
                INSERT INTO session_attribute
                VALUES ('123456', 0, 'foo', 'bar')
                """)

            incookie = Cookie()
            incookie['trac_session'] = '123456'
            outcookie = Cookie()
            req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                       outcookie=outcookie)
            session = Session(self.env, req)
            session['modified'] = True
            session.save() # updating does require modifications

            self.assertEqual(PURGE_AGE, outcookie['trac_session']['expires'])

        self.assertAlmostEqual(now, int(self.env.db_query("""
            SELECT last_visit FROM session
            WHERE sid='123456' AND authenticated=0
            """)[0][0]), -1)
开发者ID:trac-ja,项目名称:trac-ja,代码行数:30,代码来源:session.py

示例10: test_update_session

    def test_update_session(self):
        """
        Verify that accessing a session after one day updates the sessions 
        'last_visit' variable so that the session doesn't get purged.
        """
        now = time.time()

        # Make sure the session has data so that it doesn't get dropped
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO session VALUES ('123456', 0, 1)")
        cursor.execute("INSERT INTO session_attribute VALUES "
                       "('123456', 0, 'foo', 'bar')")

        incookie = Cookie()
        incookie['trac_session'] = '123456'
        outcookie = Cookie()
        req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                   outcookie=outcookie)
        session = Session(self.env, req)
        session.save() # updating should not require modifications

        self.assertEqual(PURGE_AGE, outcookie['trac_session']['expires'])

        cursor.execute("SELECT last_visit FROM session WHERE sid='123456' AND "
                       "authenticated=0")
        self.assertAlmostEqual(now, int(cursor.fetchone()[0]), -1)
开发者ID:cyphactor,项目名称:lifecyclemanager,代码行数:26,代码来源:session.py

示例11: test_purge_anonymous_session

    def test_purge_anonymous_session(self):
        """
        Verify that old sessions get purged.
        """
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO session "
                       "VALUES ('123456', 0, %s)",
                       (0,))
        cursor.execute("INSERT INTO session "
                       "VALUES ('987654', 0, %s)",
                       (int(time.time() - PURGE_AGE - 3600),))
        cursor.execute("INSERT INTO session_attribute VALUES "
                       "('987654', 0, 'foo', 'bar')")
        
        # We need to modify a different session to trigger the purging
        incookie = Cookie()
        incookie['trac_session'] = '123456'
        req = Mock(authname='anonymous', base_path='/', incookie=incookie,
                   outcookie=Cookie())
        session = Session(self.env, req)
        session['foo'] = 'bar'
        session.save()

        cursor.execute("SELECT COUNT(*) FROM session WHERE sid='987654' AND "
                       "authenticated=0")
        self.assertEqual(0, cursor.fetchone()[0])
开发者ID:wiraqutra,项目名称:photrackjp,代码行数:26,代码来源:session.py

示例12: _test_authenticated_session

 def _test_authenticated_session(self, username, fullname, email):
     """
     Verifies that a session cookie does not get used if the user is logged
     in, and that Trac expires the cookie.
     """
     req = MockRequest(self.env, authname=username)
     req.incookie['trac_session'] = '123456'
     session = Session(self.env, req)
     self.assertEqual(username, session.sid)
     session['email'] = email
     session['name'] = fullname
     session.save()
开发者ID:aroth-arsoft,项目名称:arsoft-trac-commitupdater,代码行数:12,代码来源:test_commitupdater.py

示例13: test_add_authenticated_session_var

 def test_add_authenticated_session_var(self):
     """
     Verify that new variables are inserted into the 'session' table in the
     database for an authenticated session.
     """
     req = Mock(authname='john', base_path='/', incookie=Cookie())
     session = Session(self.env, req)
     session['foo'] = 'bar'
     session.save()
     
     self.assertEqual('bar', self.env.db_query("""
         SELECT value FROM session_attribute WHERE sid='john' AND name='foo'
         """)[0][0])
开发者ID:trac-ja,项目名称:trac-ja,代码行数:13,代码来源:session.py

示例14: test_add_anonymous_session_var

    def test_add_anonymous_session_var(self):
        """
        Verify that new variables are inserted into the 'session' table in the
        database for an anonymous session.
        """
        incookie = Cookie()
        incookie["trac_session"] = "123456"
        req = Mock(authname="anonymous", base_path="/", incookie=incookie, outcookie=Cookie())
        session = Session(self.env, req)
        session["foo"] = "bar"
        session.save()

        self.assertEqual("bar", self.env.db_query("SELECT value FROM session_attribute WHERE sid='123456'")[0][0])
开发者ID:dafrito,项目名称:trac-mirror,代码行数:13,代码来源:session.py

示例15: test_authenticated_session

 def test_authenticated_session(self):
     """
     Verifies that a session cookie does not get used if the user is logged
     in, and that Trac expires the cookie.
     """
     incookie = Cookie()
     incookie["trac_session"] = "123456"
     outcookie = Cookie()
     req = Mock(authname="john", base_path="/", incookie=incookie, outcookie=outcookie)
     session = Session(self.env, req)
     self.assertEqual("john", session.sid)
     session["foo"] = "bar"
     session.save()
     self.assertEquals(0, outcookie["trac_session"]["expires"])
开发者ID:dafrito,项目名称:trac-mirror,代码行数:14,代码来源:session.py


注:本文中的trac.web.session.Session类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。