コード例 #1
0
def cleanup_roles():
    roles = Bootstrapper.get_roles()

    # Get stale Roles
    stale_roles = Group.objects.filter(
        name__istartswith=Bootstrapper.prefix
    ).exclude(
        id__in=[role.get_group().id for role in roles]
    ).delete()

    return stale_roles
コード例 #2
0
    def handle(self, *args, **options):
        self.verbosity = options.get('verbosity')

        # Run role DB synchronization
        roles = Bootstrapper.get_roles()
        synchronize_roles(roles)

        # Emit the post-signal for apps to hook
        post_synchronize_roles.send(
            sender=DjangoHatsConfig
        )

        # Print out useful completion text
        if self.verbosity > 0:
            self.stdout.write('Role synchronization complete.')
コード例 #3
0
 def test_cleanup_roles(self):
     roles = Bootstrapper.get_roles()
     group = Scientist.get_group()
     group.permissions.add(
         Permission.objects.create(
             codename='temporary',
             content_type=ContentType.objects.get_for_model(User)))
     permission_count = Permission.objects.count()
     Scientist.group = None
     Scientist._meta.name = '404'
     synchronize_roles(roles)
     Scientist._meta.permissions = ()
     cleanup_roles()
     self.assertEqual(Group.objects.count(), 3)
     self.assertTrue(
         Group.objects.get(name__icontains=Scientist.get_slug()))
     self.assertRaises(Group.DoesNotExist,
                       Group.objects.get,
                       name__icontains='scientist')
     self.assertEqual(Permission.objects.count(), permission_count)
     Scientist._meta.name = 'scientist'
     Scientist._meta.permissions = ('change_user', )
コード例 #4
0
 def test_synchronize_roles(self):
     roles = Bootstrapper.get_roles()
     group = Scientist.get_group()
     group.permissions.add(
         Permission.objects.create(
             codename='temporary',
             content_type=ContentType.objects.get_for_model(User)))
     permission_count = Permission.objects.count()
     Scientist.group = None
     Scientist._meta.name = '404'
     synchronize_roles(roles)
     self.assertEqual(Group.objects.count(), 4)
     self.assertTrue(
         Group.objects.get(name__icontains=Scientist.get_slug()))
     self.assertEqual(Permission.objects.count(), permission_count)
     _permissions = Scientist._meta.permissions
     Scientist._meta.permissions = ()
     synchronize_roles(roles)
     self.assertEqual(Scientist.get_group().permissions.count(), 0)
     Scientist._meta.permissions = _permissions
     synchronize_roles(roles)
     Scientist._meta.name = 'scientist'
コード例 #5
0
 def setUp(self, *args, **kwargs):
     '''Clears `Roles` cache for testing.
     '''
     for role in Bootstrapper.get_roles():
         setattr(role, 'group', None)
     return super(RolesTestCase, self).setUp(*args, **kwargs)
コード例 #6
0
 def test_register(self):
     roles = Bootstrapper.get_roles()
     expected_roles = [Scientist, BadlyNamedModel]
     for role in expected_roles:
         self.assertTrue(role in roles)