def get_permission_and_content_type(self, permission, obj=None):
        """
        Given a permission string and (optionally) an object, returns a tuple
        of an AppPermission instance and a ContentType instance corresponding
        to that string (and object, if given).
        
        The permission string is given in the dotted form, either
        `'app_label.codename'` or
        `'app_label.codename.target_app.target_model'`.
        
        If the correct AppPermission and ContentType cannot be determined for
        the passed parameters, a `PermissionLookupError` is raised.
        """
        
        perm_ct = None
        bits = permission.split('.')
        app_label = bits.pop(0)
        if len(bits) >= 3:
            # either a ContentType was given, or this is a long codename
            try:
                perm_ct = ContentType.objects.get(
                    app_label=bits[-2], model=bits[-1])
            except ContentType.DoesNotExist:
                codename = '.'.join(bits)
            else:
                codename = '.'.join(bits[:-2])
        else:
            codename = '.'.join(bits)
        
        obj_ct = None
        if obj:
            obj_ct = ContentType.objects.get_for_model(obj)
        
        content_type = perm_ct or obj_ct
        if not (content_type):
            raise PermissionLookupError('ContentType must be given in permission, or object must be passed.')
        if perm_ct and obj_ct and perm_ct != obj_ct:
            raise PermissionLookupError('ContentType in permission and passed object mismatched.')
        
        try:
            return (
                self.get(
                    app_label=app_label,
                    content_types=content_type,
                    codename=codename
                ),
                content_type
            )
        except self.model.DoesNotExist:
            raise PermissionLookupError('AppPermission not found.')
dataset/ETHPy150Open jlecker/django-rubberstamp/rubberstamp/models.py/AppPermissionManager.get_permission_and_content_type/original