def create_default_users_and_perms():
    """
        Adds the roles and perm to the DB. It adds only roles, perms and links between them that are not inside the db
        It is possible adding new role or perm and connecting them just modifiying the following lists
    """

    # perms = db.DBSession.query(Perm).all()
    # if len(perms) > 0:
    #     return

    default_perms = ( ("add_user",   "Add User"),
                    ("edit_user",  "Edit User"),
                    ("add_role",   "Add Role"),
                    ("edit_role",  "Edit Role"),
                    ("add_perm",   "Add Permission"),
                    ("edit_perm",  "Edit Permission"),

                    ("add_network",    "Add network"),
                    ("edit_network",   "Edit network"),
                    ("delete_network", "Delete network"),
                    ("share_network",  "Share network"),
                    ("edit_topology",  "Edit network topology"),

                    ("add_project",    "Add Project"),
                    ("edit_project",   "Edit Project"),
                    ("delete_project", "Delete Project"),
                    ("share_project",  "Share Project"),

                    ("edit_data", "Edit network data"),
                    ("view_data", "View network data"),

                    ("add_template", "Add Template"),
                    ("edit_template", "Edit Template"),

                    ("add_dimension", "Add Dimension"),
                    ("update_dimension", "Update Dimension"),
                    ("delete_dimension", "Delete Dimension"),

                    ("add_unit", "Add Unit"),
                    ("update_unit", "Update Unit"),
                    ("delete_unit", "Delete Unit")


                    )

    default_roles = (
                    ("admin",    "Administrator"),
                    ("dev",      "Developer"),
                    ("modeller", "Modeller / Analyst"),
                    ("manager",  "Manager"),
                    ("grad",     "Graduate"),
                    ("developer", "Developer"),
                    ("decision", "Decision Maker"),
                )

    roleperms = (
            # Admin permissions
            ('admin', "add_user"),
            ('admin', "edit_user"),
            ('admin', "add_role"),
            ('admin', "edit_role"),
            ('admin', "add_perm"),
            ('admin', "edit_perm"),
            ('admin', "add_network"),
            ('admin', "edit_network"),
            ('admin', "delete_network"),
            ('admin', "share_network"),
            ('admin', "add_project"),
            ('admin', "edit_project"),
            ('admin', "delete_project"),
            ('admin', "share_project"),
            ('admin', "edit_topology"),
            ('admin', "edit_data"),
            ('admin', "view_data"),
            ('admin', "add_template"),
            ('admin', "edit_template"),

            ('admin', "add_dimension"),
            ('admin', "update_dimension"),
            ('admin', "delete_dimension"),

            ('admin', "add_unit"),
            ('admin', "update_unit"),
            ('admin', "delete_unit"),

            # Developer permissions
            ("developer", "add_network"),
            ("developer", "edit_network"),
            ("developer", "delete_network"),
            ("developer", "share_network"),
            ("developer", "add_project"),
            ("developer", "edit_project"),
            ("developer", "delete_project"),
            ("developer", "share_project"),
            ("developer", "edit_topology"),
            ("developer", "edit_data"),
            ("developer", "view_data"),
            ("developer", "add_template"),
            ("developer", "edit_template"),

            ('developer', "add_dimension"),
            ('developer', "update_dimension"),
            ('developer', "delete_dimension"),

            ('developer', "add_unit"),
            ('developer', "update_unit"),
            ('developer', "delete_unit"),

            # modeller permissions
            ("modeller", "add_network"),
            ("modeller", "edit_network"),
            ("modeller", "delete_network"),
            ("modeller", "share_network"),
            ("modeller", "edit_topology"),
            ("modeller", "add_project"),
            ("modeller", "edit_project"),
            ("modeller", "delete_project"),
            ("modeller", "share_project"),
            ("modeller", "edit_data"),
            ("modeller", "view_data"),

            # Manager permissions
            ("manager", "edit_data"),
            ("manager", "view_data"),
    )

    # Map for code to ID
    id_maps_dict = {
        "perm": {},
        "role": {}
    }
    # Adding perms
    perm_dict = {}
    for code, name in default_perms:
        perm = Perm(code=code, name=name)
        perm_dict[code] = perm
        perms_by_name = db.DBSession.query(Perm).filter(Perm.code==code).all()
        if len(perms_by_name)==0:
            # Adding perm
            log.debug("# Adding PERM {}".format(code))
            db.DBSession.add(perm)
            db.DBSession.flush()

        perm_by_name = db.DBSession.query(Perm).filter(Perm.code==code).one()
        id_maps_dict["perm"][code] = perm_by_name.id

    # Adding roles
    role_dict = {}
    for code, name in default_roles:
        role = Role(code=code, name=name)
        role_dict[code] = role
        roles_by_name = db.DBSession.query(Role).filter(Role.code==code).all()
        if len(roles_by_name)==0:
            # Adding perm
            log.debug("# Adding ROLE {}".format(code))
            db.DBSession.add(role)
            db.DBSession.flush()

        role_by_name = db.DBSession.query(Role).filter(Role.code==code).one()
        id_maps_dict["role"][code] = role_by_name.id

    # Adding connections
    for role_code, perm_code in roleperms:
        #log.info("Link Role:{}({}) <---> Perm:{}({})".format(role_code, id_maps_dict["role"][role_code], perm_code, id_maps_dict["perm"][perm_code]))

        links_found = db.DBSession.query(RolePerm).filter(RolePerm.role_id==id_maps_dict["role"][role_code]).filter(RolePerm.perm_id==id_maps_dict["perm"][perm_code]).all()
        if len(links_found)==0:
            # Adding link
            log.debug("# Adding link")
            roleperm = RolePerm()
            # roleperm.role = role_dict[role_code]
            # roleperm.perm = perm_dict[perm_code]
            roleperm.role_id = id_maps_dict["role"][role_code]
            roleperm.perm_id = id_maps_dict["perm"][perm_code]
            db.DBSession.add(roleperm)
            db.DBSession.flush()

    db.DBSession.flush()
def create_default_users_and_perms ( ) : default_perms = ( ( "add_user" , "Add User" ) , ( "edit_user" , "Edit User" ) , ( "add_role" , "Add Role" ) , ( "edit_role" , "Edit Role" ) , ( "add_perm" , "Add Permission" ) , ( "edit_perm" , "Edit Permission" ) , ( "add_network" , "Add network" ) , ( "edit_network" , "Edit network" ) , ( "delete_network" , "Delete network" ) , ( "share_network" , "Share network" ) , ( "edit_topology" , "Edit network topology" ) , ( "add_project" , "Add Project" ) , ( "edit_project" , "Edit Project" ) , ( "delete_project" , "Delete Project" ) , ( "share_project" , "Share Project" ) , ( "edit_data" , "Edit network data" ) , ( "view_data" , "View network data" ) , ( "add_template" , "Add Template" ) , ( "edit_template" , "Edit Template" ) , ( "add_dimension" , "Add Dimension" ) , ( "update_dimension" , "Update Dimension" ) , ( "delete_dimension" , "Delete Dimension" ) , ( "add_unit" , "Add Unit" ) , ( "update_unit" , "Update Unit" ) , ( "delete_unit" , "Delete Unit" ) ) default_roles = ( ( "admin" , "Administrator" ) , ( "dev" , "Developer" ) , ( "modeller" , "Modeller / Analyst" ) , ( "manager" , "Manager" ) , ( "grad" , "Graduate" ) , ( "developer" , "Developer" ) , ( "decision" , "Decision Maker" ) , ) roleperms = ( ( 'admin' , "add_user" ) , ( 'admin' , "edit_user" ) , ( 'admin' , "add_role" ) , ( 'admin' , "edit_role" ) , ( 'admin' , "add_perm" ) , ( 'admin' , "edit_perm" ) , ( 'admin' , "add_network" ) , ( 'admin' , "edit_network" ) , ( 'admin' , "delete_network" ) , ( 'admin' , "share_network" ) , ( 'admin' , "add_project" ) , ( 'admin' , "edit_project" ) , ( 'admin' , "delete_project" ) , ( 'admin' , "share_project" ) , ( 'admin' , "edit_topology" ) , ( 'admin' , "edit_data" ) , ( 'admin' , "view_data" ) , ( 'admin' , "add_template" ) , ( 'admin' , "edit_template" ) , ( 'admin' , "add_dimension" ) , ( 'admin' , "update_dimension" ) , ( 'admin' , "delete_dimension" ) , ( 'admin' , "add_unit" ) , ( 'admin' , "update_unit" ) , ( 'admin' , "delete_unit" ) , ( "developer" , "add_network" ) , ( "developer" , "edit_network" ) , ( "developer" , "delete_network" ) , ( "developer" , "share_network" ) , ( "developer" , "add_project" ) , ( "developer" , "edit_project" ) , ( "developer" , "delete_project" ) , ( "developer" , "share_project" ) , ( "developer" , "edit_topology" ) , ( "developer" , "edit_data" ) , ( "developer" , "view_data" ) , ( "developer" , "add_template" ) , ( "developer" , "edit_template" ) , ( 'developer' , "add_dimension" ) , ( 'developer' , "update_dimension" ) , ( 'developer' , "delete_dimension" ) , ( 'developer' , "add_unit" ) , ( 'developer' , "update_unit" ) , ( 'developer' , "delete_unit" ) , ( "modeller" , "add_network" ) , ( "modeller" , "edit_network" ) , ( "modeller" , "delete_network" ) , ( "modeller" , "share_network" ) , ( "modeller" , "edit_topology" ) , ( "modeller" , "add_project" ) , ( "modeller" , "edit_project" ) , ( "modeller" , "delete_project" ) , ( "modeller" , "share_project" ) , ( "modeller" , "edit_data" ) , ( "modeller" , "view_data" ) , ( "manager" , "edit_data" ) , ( "manager" , "view_data" ) , ) id_maps_dict = { "perm" : { } , "role" : { } } perm_dict = { } for code , name in default_perms : perm = Perm ( code = code , name = name ) perm_dict [ code ] = perm perms_by_name = db . DBSession . query ( Perm ) . filter ( Perm . code == code ) . all ( ) if len ( perms_by_name ) == 0 : log . debug ( "# Adding PERM {}" . format ( code ) ) db . DBSession . add ( perm ) db . DBSession . flush ( ) perm_by_name = db . DBSession . query ( Perm ) . filter ( Perm . code == code ) . one ( ) id_maps_dict [ "perm" ] [ code ] = perm_by_name . id role_dict = { } for code , name in default_roles : role = Role ( code = code , name = name ) role_dict [ code ] = role roles_by_name = db . DBSession . query ( Role ) . filter ( Role . code == code ) . all ( ) if len ( roles_by_name ) == 0 : log . debug ( "# Adding ROLE {}" . format ( code ) ) db . DBSession . add ( role ) db . DBSession . flush ( ) role_by_name = db . DBSession . query ( Role ) . filter ( Role . code == code ) . one ( ) id_maps_dict [ "role" ] [ code ] = role_by_name . id for role_code , perm_code in roleperms : links_found = db . DBSession . query ( RolePerm ) . filter ( RolePerm . role_id == id_maps_dict [ "role" ] [ role_code ] ) . filter ( RolePerm . perm_id == id_maps_dict [ "perm" ] [ perm_code ] ) . all ( ) if len ( links_found ) == 0 : log . debug ( "# Adding link" ) roleperm = RolePerm ( ) roleperm . role_id = id_maps_dict [ "role" ] [ role_code ] roleperm . perm_id = id_maps_dict [ "perm" ] [ perm_code ] db . DBSession . add ( roleperm ) db . DBSession . flush ( ) db . DBSession . flush ( )
Adds the roles and perm to the DB. It adds only roles, perms and links between them that are not inside the db
        It is possible adding new role or perm and connecting them just modifiying the following lists