Exemple #1
0
def get_all_node_models():
    """
    This function returns the list of all the node_models of the project. It cares about inheritance and overloading.
    """
    from bulb.contrib.auth.node_models import get_user_node_model, get_permission_node_model, get_group_node_model
    from bulb.contrib.sessions.node_models import get_session_node_model
    from bulb.db.node_models import Node
    node_models_list = []

    # # Allow overloaded native classes.
    Permission_class_is_needed = False
    Group_class_is_needed = False
    User_class_is_needed = False
    Session_class_is_needed = False

    for file_path in get_files_paths_list("node_models.py"):

        # Import the module from his path
        spec = importlib.util.spec_from_file_location("node_models", file_path)
        node_models = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(node_models)

        # Get all the module classes that have in their parents the Node class.
        node_model_dict = node_models.__dict__

        for k, v in node_model_dict.items():
            try:
                # Explanation : __module__ return only the name of the module where is contained the node_class (here :
                # "node_models"), but if the node_class is an import, __module__ return the full module path (example :
                # "bulb.contrib.sessions.node_models"). So, this line prevent the detection of the imported
                # classes in the node_models files.
                if Node in v.__mro__ and v.__module__ == "node_models":

                    # # Add overloaded native classes.
                    if v.__name__ == "Permission":
                        Permission_class_is_needed = True

                    elif v.__name__ == "Group":
                        Group_class_is_needed = True

                    elif v.__name__ == "User":
                        User_class_is_needed = True

                    elif v.__name__ == "Session":
                        Session_class_is_needed = True

                    else:
                        node_models_list.append(v)

            except:
                pass

    # # Add overloaded native classes.
    if Permission_class_is_needed:
        node_models_list.append(get_permission_node_model())

    if Group_class_is_needed:
        node_models_list.append(get_group_node_model())

    if User_class_is_needed:
        node_models_list.append(get_user_node_model())

    if Session_class_is_needed:
        node_models_list.append(get_session_node_model())

    return node_models_list
Exemple #2
0
from bulb.contrib.auth.node_models import get_user_node_model
from django import template

User = get_user_node_model()

register = template.Library()


@register.filter
def has_perm(user, permission_codename):
    if isinstance(user, User):
        return user.has_perm(permission_codename)
    return False