def post_save_project_handler(sender, **kwargs): """ fn that gets called after a QProject is saved; if it's just been created, then the corresponding permissions and groups need to be setup :param sender: :param kwargs: :return: """ created = kwargs.pop("created", True) project = kwargs.pop("instance", None) if created: assert project.groups.count() == 0 for group_suffix, permission_prefixes in GROUP_PERMISSIONS.iteritems(): group_name = "{0}_{1}".format(project.name, group_suffix) group = Group( name=group_name ) group.save() for permission_prefix in permission_prefixes: permission_codename = "{0}_{1}".format(permission_prefix, project.name) permission_description = "{0} {1} instances".format(permission_prefix, project.name) content_type = ContentType.objects.get(app_label=APP_LABEL, model='qproject') (permission, created_permission) = Permission.objects.get_or_create( codename=permission_codename, name=permission_description, content_type=content_type, ) group.permissions.add(permission) group.save() project.groups.add(group)
def post_save_project_handler(sender, **kwargs): """ fn that gets called after a QProject is saved; if it's just been created, then the corresponding permissions and groups need to be setup :param sender: :param kwargs: :return: """ created = kwargs.pop("created", True) project = kwargs.pop("instance", None) if created: assert project.groups.count() == 0 for group_suffix, permission_prefixes in GROUP_PERMISSIONS.items(): group_name = "{0}_{1}".format(project.name, group_suffix) group = Group(name=group_name) group.save() for permission_prefix in permission_prefixes: permission_codename = "{0}_{1}".format(permission_prefix, project.name) permission_description = "{0} {1} instances".format( permission_prefix, project.name) content_type = ContentType.objects.get(app_label=APP_LABEL, model='qproject') (permission, created_permission) = Permission.objects.get_or_create( codename=permission_codename, name=permission_description, content_type=content_type, ) group.permissions.add(permission) group.save() project.groups.add(group)
def test_project_groups_created(self): """ test that the correct groups and permissions exist after creating a project :return: """ for group_suffix, permission_prefixes in GROUP_PERMISSIONS.iteritems(): group = self.test_project.get_group(group_suffix) self.assertIsNotNone(group.pk) for permission_prefix in permission_prefixes: permission = self.test_project.get_permission(permission_prefix) self.assertIsNotNone(permission.pk) self.assertIn(permission, group.permissions.all())
def post_delete_project_handler(sender, **kwargs): """ fn that gets called after a QProject is deleted; the corresponding permissions and groups need to be deleted :param sender: :param kwargs: :return: """ project = kwargs.pop("instance", None) if project: groups = [project.get_group(group_suffix) for group_suffix in GROUP_PERMISSIONS.keys()] permissions = set([permission for group in groups for permission in group.permissions.all()]) for permission in permissions: permission.delete() for group in groups: group.delete()
def post_save_project_handler(sender, **kwargs): """ fn that gets called after a QProject is saved; if it's just been created, then the corresponding permissions and groups need to be setup :param sender: :param kwargs: :return: """ created = kwargs.pop("created", True) project = kwargs.pop("instance", None) if project and created: for group_suffix, permission_prefixes in GROUP_PERMISSIONS.iteritems(): group = project.get_group(group_suffix, create_group=True) for permission_prefix in permission_prefixes: permission = project.get_permission(permission_prefix, create_permission=True) group.permissions.add(permission) group.save()