def __new__(cls, name, bases, attrs): # Call to super super_new = super(RoleMetaClass, cls).__new__(cls, name, bases, attrs) # Check if the Role is registered in the global namespace if bases and Role in bases: super_new._meta = type('Meta', (), {}) super_new._meta.permissions = () # Extract potential Meta information from super if hasattr(super_new, 'Meta'): for attr in [attr for attr in dir(super_new.Meta) if not callable(attr) and not attr.startswith('__')]: setattr(super_new._meta, attr, getattr(super_new.Meta, attr)) # Register the new Role Bootstrapper.register(super_new) return super_new
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
def __new__(cls, name, bases, attrs): # Call to super super_new = super(RoleMetaClass, cls).__new__(cls, name, bases, attrs) # Check if the Role is registered in the global namespace if bases and Role in bases: super_new._meta = type('Meta', (), {}) super_new._meta.permissions = () # Extract potential Meta information from super if hasattr(super_new, 'Meta'): for attr in [ attr for attr in dir(super_new.Meta) if not callable(attr) and not attr.startswith('__') ]: setattr(super_new._meta, attr, getattr(super_new.Meta, attr)) # Register the new Role Bootstrapper.register(super_new) return super_new
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.')
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', )
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'
def ready(self): Bootstrapper.load()
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)
def test_register(self): roles = Bootstrapper.get_roles() expected_roles = [Scientist, BadlyNamedModel] for role in expected_roles: self.assertTrue(role in roles)