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


Python User.role方法代码示例

本文整理汇总了Python中app.userprofile.User.role方法的典型用法代码示例。如果您正苦于以下问题:Python User.role方法的具体用法?Python User.role怎么用?Python User.role使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在app.userprofile.User的用法示例。


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

示例1: test_delete_has_users_new_role

# 需要导入模块: from app.userprofile import User [as 别名]
# 或者: from app.userprofile.User import role [as 别名]
    def test_delete_has_users_new_role(self):
        """
            Test deleting a role if there are still users and a valid new role is given.

            Expected result: The role is deleted. The role is assigned to all users who had the old role (but not to
                             others).
        """
        # The role that will be deleted.
        name = 'Administrator'
        role = Role(name=name)
        user = User('[email protected]', 'Jane Doe')
        user.role = role
        db.session.add(role)
        db.session.add(user)

        # The new role for the user.
        new_role = Role(name='Guest')
        db.session.add(new_role)

        # Another role and user who will stay untouched.
        other_role = Role(name='User')
        other_user = User('[email protected]', 'John Doe')
        other_user.role = other_role
        db.session.add(other_role)
        db.session.add(other_user)

        db.session.commit()

        role.delete(new_role)
        loaded_role = Role.load_from_name(name)
        self.assertIsNone(loaded_role)
        self.assertEqual(new_role, user.role)
        self.assertEqual(other_role, other_user.role)
开发者ID:BMeu,项目名称:Aerarium,代码行数:35,代码来源:role_test.py

示例2: test_permission_required_one_of_has_permission

# 需要导入模块: from app.userprofile import User [as 别名]
# 或者: from app.userprofile.User import role [as 别名]
    def test_permission_required_one_of_has_permission(self):
        """
            Test the `permission_required` decorator if the user has one of the requested permission, but not all.

            Expected result: The decorated view function is returned.
        """

        email = '[email protected]'
        name = 'Jane Doe'
        password = '123456'
        user = User(email, name)
        user.set_password(password)
        user.role = Role('Administrator')
        user.role.permissions = Permission.EditRole

        db.session.add(user)
        db.session.commit()

        user.login(email, password)

        self.assertTrue(user.role.has_permission(Permission.EditRole))
        self.assertFalse(user.role.has_permission(Permission.EditUser))

        view_function = permission_required_one_of(Permission.EditRole, Permission.EditUser)(self.view_function)
        response = view_function()
        self.assertEqual(self.view_function(), response)
开发者ID:BMeu,项目名称:Aerarium,代码行数:28,代码来源:decorators_test.py

示例3: test_init_has_users

# 需要导入模块: from app.userprofile import User [as 别名]
# 或者: from app.userprofile.User import role [as 别名]
    def test_init_has_users(self):
        """
            Test that the form is correctly initialized if the role has users.

            Expected result: The new_role field exists and is initialized with all other roles.
        """
        role = Role(name='Administrator')
        user = User('[email protected]', 'Jane Doe')
        user.role = role

        other_role_1 = Role(name='Visitor')
        other_role_2 = Role(name='Guest')

        db.session.add(role)
        db.session.add(user)
        db.session.add(other_role_1)
        db.session.add(other_role_2)

        db.session.commit()

        # The role choices are ordered by name and skip the role to delete.
        choices = [
            (0, ''),
            (other_role_2.id, other_role_2.name),
            (other_role_1.id, other_role_1.name),
        ]

        self.assertLess(other_role_1.id, other_role_2.id)
        self.assertListEqual([user], role.users.all())

        form = RoleDeleteForm(role)
        self.assertIsNotNone(form.new_role)
        self.assertListEqual(choices, form.new_role.choices)
开发者ID:BMeu,项目名称:Aerarium,代码行数:35,代码来源:forms_test.py

示例4: test_permission_required_all_not_all_permissions

# 需要导入模块: from app.userprofile import User [as 别名]
# 或者: from app.userprofile.User import role [as 别名]
    def test_permission_required_all_not_all_permissions(self):
        """
            Test the `permission_required_all` decorator if the user does not have all the requested permissions.

            Expected result: The request is aborted with an error 403.
        """

        email = '[email protected]'
        name = 'Jane Doe'
        password = '123456'
        user = User(email, name)
        user.set_password(password)
        user.role = Role('Administrator')
        user.role.permissions = Permission.EditRole

        db.session.add(user)
        db.session.commit()

        user.login(email, password)

        self.assertTrue(user.role.has_permission(Permission.EditRole))
        self.assertFalse(user.role.has_permission(Permission.EditUser))

        with self.assertRaises(Forbidden):
            permission_required_all(Permission.EditRole, Permission.EditUser)(self.view_function)()
开发者ID:BMeu,项目名称:Aerarium,代码行数:27,代码来源:decorators_test.py

示例5: test_delete_has_users_no_role

# 需要导入模块: from app.userprofile import User [as 别名]
# 或者: from app.userprofile.User import role [as 别名]
    def test_delete_has_users_no_role(self):
        """
            Test deleting a role if there are still users and no role is given.

            Expected result: An error is raised.
        """
        name = 'Administrator'
        role = Role(name=name)
        user = User('[email protected]', 'Jane Doe')
        user.role = role
        db.session.add(role)
        db.session.add(user)
        db.session.commit()

        with self.assertRaises(ValueError) as exception_cm:
            role.delete()

            loaded_role = Role.load_from_name(name)

            self.assertIn('A new role must be given', str(exception_cm.exception))
            self.assertIsNotNone(loaded_role)
            self.assertEqual(loaded_role, user.role)
开发者ID:BMeu,项目名称:Aerarium,代码行数:24,代码来源:role_test.py


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