• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python session.Session类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: 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:trac-ja,项目名称:trac-ja,代码行数:15,代码来源:session.py


示例17: 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:trac-ja,项目名称:trac-ja,代码行数:15,代码来源:session.py


示例18: test_new_session_promotion

    def test_new_session_promotion(self):
        """
        Verifies that even without a preexisting anonymous session,
        an authenticated session will be created when the user logs in.
        (same test as above without the initial INSERT)
        """
        with self.env.db_transaction as db:
            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.save()

        self.assertEqual([("john", 1)], self.env.db_query("SELECT sid, authenticated FROM session"))
开发者ID:dafrito,项目名称:trac-mirror,代码行数:16,代码来源:session.py


示例19: test_session_promotion

    def test_session_promotion(self):
        """
        Verifies that an existing anonymous session gets promoted to an
        authenticated session when the user logs in.
        """
        with self.env.db_transaction as db:
            db("INSERT INTO session VALUES ('123456', 0, 0)")
            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.save()

        self.assertEqual([("john", 1)], self.env.db_query("SELECT sid, authenticated FROM session"))
开发者ID:dafrito,项目名称:trac-mirror,代码行数:16,代码来源:session.py


示例20: 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:dafrito,项目名称:trac-mirror,代码行数:46,代码来源:session.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python wiki.wiki_to_html函数代码示例发布时间:2022-05-27
下一篇:
Python chrome.Chrome类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap