(self, check=None, content_object=None, generic=False)
return result
Assign a permission to a user.

        To assign permission for all checks: let check=None.
        To assign permission for all objects: let content_object=None.

        If generic is True then "check" will be suffixed with _modelname.
Assign a permission to a user.
Assign a permission to a user .
def assign(self, check=None, content_object=None, generic=False):
        """
        Assign a permission to a user.

        To assign permission for all checks: let check=None.
        To assign permission for all objects: let content_object=None.

        If generic is True then "check" will be suffixed with _modelname.
        """
        result = []

        if not content_object:
            content_objects = (self.model,)
        elif not isinstance(content_object, (list, tuple)):
            content_objects = (content_object,)
        else:
            content_objects = content_object

        if not check:
            checks = self.generic_checks + getattr(self, "checks", [])
        elif not isinstance(check, (list, tuple)):
            checks = (check,)
        else:
            checks = check

        for content_object in content_objects:
            # raise an exception before adding any permission
            # i think Django does not rollback by default
            if not isinstance(content_object, (Model, ModelBase)):
                raise NotAModel(content_object)
            elif isinstance(content_object, Model) and not content_object.pk:
                raise UnsavedModelInstance(content_object)

            content_type = ContentType.objects.get_for_model(content_object)

            for check in checks:
                if isinstance(content_object, Model):
                    # make an authority per object permission
                    codename = self.get_codename(check, content_object, generic,)
                    try:
                        perm = Permission.objects.get(
                            user=self.user,
                            group=self.group,
                            codename=codename,
                            approved=True,
                            content_type=content_type,
                            object_id=content_object.pk,
                        )
                    except Permission.DoesNotExist:
                        perm = Permission.objects.create(
                            user=self.user,
                            group=self.group,
                            content_object=content_object,
                            codename=codename,
                            approved=True,
                        )

                    result.append(perm)

                elif isinstance(content_object, ModelBase):
                    # make a Django permission
                    codename = self.get_django_codename(
                        check, content_object, generic, without_left=True,
                    )
                    try:
                        perm = DjangoPermission.objects.get(codename=codename)
                    except DjangoPermission.DoesNotExist:
                        name = check
                        if "_" in name:
                            name = name[0 : name.find("_")]
                        perm = DjangoPermission(
                            name=name, codename=codename, content_type=content_type,
                        )
                        perm.save()
                    self.user.user_permissions.add(perm)
                    result.append(perm)

        return result
def assign ( self , check = None , content_object = None , generic = False ) : result = [ ] if not content_object : content_objects = ( self . model , ) elif not isinstance ( content_object , ( list , tuple ) ) : content_objects = ( content_object , ) else : content_objects = content_object if not check : checks = self . generic_checks + getattr ( self , "checks" , [ ] ) elif not isinstance ( check , ( list , tuple ) ) : checks = ( check , ) else : checks = check for content_object in content_objects : # raise an exception before adding any permission # i think Django does not rollback by default if not isinstance ( content_object , ( Model , ModelBase ) ) : raise NotAModel ( content_object ) elif isinstance ( content_object , Model ) and not content_object . pk : raise UnsavedModelInstance ( content_object ) content_type = ContentType . objects . get_for_model ( content_object ) for check in checks : if isinstance ( content_object , Model ) : # make an authority per object permission codename = self . get_codename ( check , content_object , generic , ) try : perm = Permission . objects . get ( user = self . user , group = self . group , codename = codename , approved = True , content_type = content_type , object_id = content_object . pk , ) except Permission . DoesNotExist : perm = Permission . objects . create ( user = self . user , group = self . group , content_object = content_object , codename = codename , approved = True , ) result . append ( perm ) elif isinstance ( content_object , ModelBase ) : # make a Django permission codename = self . get_django_codename ( check , content_object , generic , without_left = True , ) try : perm = DjangoPermission . objects . get ( codename = codename ) except DjangoPermission . DoesNotExist : name = check if "_" in name : name = name [ 0 : name . find ( "_" ) ] perm = DjangoPermission ( name = name , codename = codename , content_type = content_type , ) perm . save ( ) self . user . user_permissions . add ( perm ) result . append ( perm ) return result