Пример #1
0
def delete_permission(user_id, object_id):

    with TuRODSSession() as session:

        sql = "delete from R_OBJT_ACCESS WHERE object_id = {} and user_id = {}".format(
            object_id, 24746)
        alias = 'del_permissions'

        query = SpecificQuery(session, sql, alias)

        # register specific query in iCAT
        _ = query.register()

        exception = None

        try:
            query.execute()
            query.get_results()
        except Exception as e:
            exception = e

        # delete specific query
        _ = query.remove()

        if exception is not None:
            log_and_raise_exception('Error in delete_permission', exception)
Пример #2
0
def get_permissions_dict():
    permissions_dict = {}
    with TuRODSSession() as session:

        sql = "select token_id, token_name from R_TOKN_MAIN"
        columns = [CollectionAccess.user_id, CollectionAccess.name]
        alias = 'get_permissions_codes'
        query = SpecificQuery(session, sql, alias, columns)

        # register specific query in iCAT
        _ = query.register()
        exception = None

        try:
            for result in query.get_results():
                token_id = result[CollectionAccess.user_id]
                name = result[CollectionAccess.name]
                permissions_dict[token_id] = name

        except Exception as e:
            exception = e

        _ = query.remove()

        if exception is not None:
            log_and_raise_exception('Error in get_permissions_dict', exception)

    return permissions_dict
Пример #3
0
def update_permissions(user, access, recursive, name):

    access_type_id = get_access_type_id(access)
    object_id = get_collection_id(name)
    users_dict = get_user_dict()
    user_id = list(users_dict.keys())[list(users_dict.values()).index(user)]

    with TuRODSSession() as session:
        sql = "update R_OBJT_ACCESS set access_type_id = {} where object_id = {} and user_id = {}" \
            .format(access_type_id, object_id, user_id)

        alias = 'update_permissions2'
        query = SpecificQuery(session, sql, alias)

        # register specific query in iCAT
        _ = query.register()

        exception = None
        try:
            query.execute()
        except CAT_NO_ROWS_FOUND:
            pass
        except Exception as e:
            exception = e

        _ = query.remove()

        if exception is not None:
            log_and_raise_exception('Error in changing the permissions access_type_id = {} object_id = {} and user_id = {}' \
                        .format(access_type_id, object_id, user_id), exception)
Пример #4
0
def get_tree(node, irods_user, filters, allowed_collection_ids):

    root = None
    thumbs_ids_collection = None

    irods_user_id = get_irods_user_id(irods_user)

    with TuRODSSession(irods_user) as session:
        try:
            coll = session.collections.get(node)
            
            if is_allowed(allowed_collection_ids, coll.path):
                root = Node(coll.name, id=coll.id,path=coll.path, expanded=False)
                thumbs_ids_collection = search_objects(coll, root, filters, irods_user_id)
                for col in coll.subcollections:
                    if is_allowed(allowed_collection_ids, col.path):
                        Node(col.name, root, id=col.id, path=col.path)
                        
            if root is None:
                return None, thumbs_ids_collection, coll.id
            
            
            return root, thumbs_ids_collection, coll.id
        except CAT_NO_ROWS_FOUND:
            pass
        except Exception as e:
            log_and_raise_exception("Error in get_tree", e)
        
    return root, thumbs_ids_collection, None
Пример #5
0
def get_thumbnail_list(sql, thumbs_ids_collection):
    
    with TuRODSSession() as session:          
        columns = [DataObject.id]

        alias = 'get_thumbnail_list'
        query = SpecificQuery(session, sql, alias, columns)
        
        # register specific query in iCAT
        _ = query.register()

        exception = None
        try:
            results = query.get_results()
            for result in results:
                object_id = result[DataObject.id]
                thumbs_ids_collection.append(object_id)

        except CAT_NO_ROWS_FOUND:
            pass
        except Exception as e:
            exception = e

        # delete specific query
        _ = query.remove()
        
        if exception is not None:
            log_and_raise_exception("Error in get_thumbnail_list", exception)
        
    return thumbs_ids_collection
Пример #6
0
def determine_object_type( session, attribute, irods_user_id):
    
    
    all_ints = True
    all_dates = True 
    no_rows = False
    all_times = True
    
    sql = "select r_data_meta_main.meta_attr_value from R_DATA_MAIN " \
        "JOIN R_OBJT_METAMAP r_data_metamap ON R_DATA_MAIN.data_id = r_data_metamap.object_id " \
        "JOIN R_META_MAIN r_data_meta_main ON r_data_metamap.meta_id = r_data_meta_main.meta_id " \
        "JOIN R_OBJT_ACCESS OA ON OA.object_id = R_DATA_MAIN.data_id " \
        "where user_id in ( select group_user_id from R_user_group where user_id = {0}) and r_data_meta_main.meta_attr_name = '{1}'".format(irods_user_id, attribute)


    alias = 'determine_object_type'
    columns = [DataObjectMeta.value]
    query = SpecificQuery(session, sql, alias, columns)
    
    # register specific query in iCAT
    _ = query.register()
    exception = None

    try:
        for result in query.get_results():
            value = result[DataObjectMeta.value]

            if all_ints:
                all_ints = is_float(value)

            if all_times:
                all_times = is_time(value)

            if all_dates:
                all_dates = is_date(value)
                    
            if not all_ints and not all_dates and not all_times:
                break  
    except CAT_NO_ROWS_FOUND:
        no_rows = True
                
    except Exception as e:
        exception = e


    # delete specific query
    _ = query.remove()           
    
    if exception is not None:
        log_and_raise_exception("Error in determine_object_type", exception)
        
    if no_rows:
        return None
    if all_ints:
        return 'De'
    if all_dates:
        return 'Da'
    if all_times:
        return 'Ti'
    return 'St'       
Пример #7
0
def remove_none(attribute_name):
          
    with TuRODSSession() as session:

        sql = "select meta_attr_value from R_META_MAIN where meta_attr_name = '{}'".format(attribute_name)
        
        alias = 'remove_none'
        columns = [CollectionMeta.value]

        query = SpecificQuery(session, sql, alias, columns)
        
        # register specific query in iCAT
        _ = query.register()
        exception = None
        
        try:
            for result in query.get_results():
                print("Result: '{}'".format(result[CollectionMeta.value]))

        except Exception as e:
            exception = e
        # delete specific query
        _ = query.remove()
        
        if exception is not None:
            log_and_raise_exception('Error in remove_none', exception)
Пример #8
0
def has_objects(clauses, user, starting_folder):
    
    coll_ids = {}
    
    irods_user_id = get_irods_user_id(user.profile.irods_user)
                
    with TuRODSSession() as session:

        base_sql = "select distinct R_DATA_MAIN.coll_id, R_COLL_MAIN.coll_name from R_DATA_MAIN " \
            "JOIN R_COLL_MAIN ON R_DATA_MAIN.coll_id = R_COLL_MAIN.coll_id " \
            "JOIN R_OBJT_METAMAP r_data_metamap ON R_DATA_MAIN.data_id = r_data_metamap.object_id " \
            "JOIN R_META_MAIN r_data_meta_main ON r_data_metamap.meta_id = r_data_meta_main.meta_id " \
            "JOIN R_OBJT_ACCESS OA ON OA.object_id = R_DATA_MAIN.data_id " \
            "WHERE user_id in ( select group_user_id from R_user_group where user_id = {0})".format(irods_user_id)


        nb_of_filters = len(clauses)    
        columns = [DataObject.collection_id, DataObject.path]
                      
        if nb_of_filters == 1:
            sql = base_sql + clauses[0]
                                 
        else:
            sql = ''
            n = 0
            for clause in clauses:
                if n == 0:
                    sql = base_sql + clause
                    n = n + 1
                else:
                    sql = sql + ' intersect ' + base_sql + clause
        alias = 'list_data_name_idyy'
         
        query = SpecificQuery(session, sql, alias, columns)
        
        # register specific query in iCAT
        _ = query.register()
        
        exception = None

        try:
            for result in query.get_results():
                collid = result[DataObject.collection_id]
                path = result[DataObject.path]
                for folder in starting_folder.split(';'):    
                    if path.startswith(folder):
                        coll_ids[path] = collid
                        break

        except CAT_NO_ROWS_FOUND:
            pass
        except Exception as e:
            exception = e
        _ = query.remove()

        if exception is not None:
            log_and_raise_exception('Error in has_objects',exception)
    
    return coll_ids
Пример #9
0
def get_tree_root(node, irods_user, filters, allowed_collection_ids, root):

    ids = {}

    thumbs_ids_collection = None

    if allowed_collection_ids is not None:
        
        with TuRODSSession(irods_user) as session:
            coll = session.collections.get(node)
            my_root = Node(node, root, id=coll.id, path=node, expanded=False)

            for path in sorted(allowed_collection_ids.keys()):
                splitted = path.split(node)
                starting_root1 = node
                my_parent = my_root
                
                if len(splitted) == 2:
                    sub_folders = splitted[1][1:].split('/')
                    for x in range( len(sub_folders)):
                        p = starting_root1 + '/' + sub_folders[x]
                        
                        if p in ids:
                            my_parent = ids[p]
                        else:
                            try:
                                coll = session.collections.get(p)
                                my_parent = Node(p, my_parent, path=p, id=coll.id, expanded=False)
                                ids[p] = my_parent
                            except Exception as e:
                                print('Failed', e)
                                pass
                        starting_root1 = p

        return None, None

    irods_user_id = get_irods_user_id(irods_user)

    with TuRODSSession(irods_user) as session:
        try:
            coll = session.collections.get(node)
            
            if is_allowed(allowed_collection_ids, coll.path):
                sub = Node(node, root, id=coll.id, path=coll.path, expanded=False)
                thumbs_ids_collection = None
                #thumbs_ids_collection = search_objects(coll, sub, filters, irods_user_id)
                
                for col in coll.subcollections:
                    if is_allowed(allowed_collection_ids, col.path):
                        Node(col.name, sub, id=col.id, path=col.path)
            
            return thumbs_ids_collection, coll.id
        except CAT_NO_ROWS_FOUND:
            pass
        except Exception as e:
            log_and_raise_exception("Error in get_tree_root", e)

    return thumbs_ids_collection, None
Пример #10
0
def permissions(folder, name):

    users_dict = get_user_dict()
    show_groups(users_dict)

    with TuRODSSession() as session:
        coll = session.collections.get(name)

    permission = []
    user_with_permissions = {}

    with TuRODSSession() as session:

        sql = "select user_id, access_type_id from R_OBJT_ACCESS WHERE object_id = {}".format(
            coll.id)
        columns = [CollectionAccess.user_id, CollectionAccess.access_id]
        alias = 'get_permissions'
        query = SpecificQuery(session, sql, alias, columns)

        # register specific query in iCAT
        _ = query.register()
        exception = None

        try:
            for result in query.get_results():
                usr_id = result[CollectionAccess.user_id]
                access_id = result[CollectionAccess.access_id]
                permission.append({
                    'user': users_dict[usr_id],
                    'access': permissions_dict[access_id],
                    'hasRights': True
                })
                user_with_permissions[usr_id] = usr_id
                print(usr_id, users_dict[usr_id])

        except CAT_NO_ROWS_FOUND:
            pass
        except Exception as e:
            exception = e

        _ = query.remove()

        if exception is not None:
            log_and_raise_exception('Error fetching permissions', exception)

        for usr_id in users_dict:
            if usr_id not in user_with_permissions:
                permission.append({
                    'user': users_dict[usr_id],
                    'access': '',
                    'hasRights': False
                })

    return permission
Пример #11
0
def get_distinct_object_metadata_attr_per_user(usr, irods_user_id):

    with TuRODSSession() as session:
        
        sql = "select distinct r_data_meta_main.meta_attr_name from R_DATA_MAIN " \
            "JOIN R_OBJT_METAMAP r_data_metamap ON R_DATA_MAIN.data_id = r_data_metamap.object_id " \
            "JOIN R_META_MAIN r_data_meta_main ON r_data_metamap.meta_id = r_data_meta_main.meta_id " \
            "JOIN R_OBJT_ACCESS OA ON OA.object_id = R_DATA_MAIN.data_id " \
            "where user_id in ( select group_user_id from R_user_group where user_id = {0}) order by r_data_meta_main.meta_attr_name".format(irods_user_id)


        alias = 'list_data_name_id118'
        columns = [DataObjectMeta.name]
        query = SpecificQuery(session, sql, alias, columns)
        
        # register specific query in iCAT
        _ = query.register()
        exception = None
        new_att_list = []
        try:
            for result in query.get_results():
                name = result[DataObjectMeta.name]
                name_unique = name.replace(' ', '%20')

                # if we have two attribute 'ColorMode' and 'ColorMode ' update_or_create does not see the difference @%$$%    
                uima, created = UserIrodsMetaDataAttribute.objects.update_or_create(
                    name_unique__exact=name_unique, object_or_collection=True, user=usr)
                if created:
                    uima.name = name
                    uima.name_unique = name_unique
                    uima.save()
                    new_att_list.append(uima)
                 
        except CAT_NO_ROWS_FOUND:
            pass                
        except Exception as e:
            exception = e

        # delete specific query
        _ = query.remove()
        
        if exception is not None:
            log_and_raise_exception('Error in get_distinct_object_metadata_attr_per_user', exception)
                
    #for uima in UserIrodsMetaDataAttribute.objects.filter(user=usr, object_or_collection=True):
    for uima in new_att_list:
        uima.type = determine_object_type(session, uima.name, irods_user_id)
        if uima.type is None:
            uima.delete()
        else:
            uima.save()
Пример #12
0
def testje(path, user_id):
    with TuRODSSession() as session:
        sql = "select object_id from R_OBJT_ACCESS where user_id = {} and object_id in" \
        "(select R_COLL_MAIN.coll_id from R_COLL_MAIN WHERE R_COLL_MAIN.coll_name like '{}%' )". \
        format(user_id, path)



        sql = "select R_COLL_MAIN.coll_id from R_COLL_MAIN JOIN R_OBJT_ACCESS ON R_COLL_MAIN.coll_id = R_OBJT_ACCESS.object_id " \
        "WHERE R_COLL_MAIN.coll_name like '{}% and R_OBJT_ACCESS.user_id = {}".format(path, user_id)


        sql = "select R_COLL_MAIN.coll_id from R_COLL_MAIN " \
                "JOIN R_OBJT_ACCESS OA ON OA.object_id = R_COLL_MAIN.coll_id " \
                "WHERE user_id = " + str(user_id)

        #sql = "select R_COLL_MAIN.coll_id from R_COLL_MAIN WHERE R_COLL_MAIN.coll_name like '{}%' ".format(path)
        print(sql)

        alias = 'blepper14'
        columns = [Collection.id]
        query = SpecificQuery(session, sql, alias, columns)

        # register specific query in iCAT
        _ = query.register()

        exception = None
        n = 0
        try:
            query.execute()
            for result in query.get_results():
                n = n + 1
                print(n, result[Collection.id])

                print(get_collection_name(result[Collection.id]))

        except CAT_NO_ROWS_FOUND:
            print('no exception')
        except Exception as e:
            exception = e

        _ = query.remove()

        if exception is not None:
            log_and_raise_exception('Error', exception)
Пример #13
0
def resize_picture_in_temp(obj, path):
    
    byte_io = None
    try:    
        with obj.open('r') as f:

            image_binary = f.read()
            with Image(blob=image_binary) as img:
                img.format = 'jpeg' 
                img.transform(resize='x400')
                byte_io = BytesIO()
                img.save(byte_io)
                
    except Exception as e:
        log_and_raise_exception("Error in resize_picture_in_temp on path {}".format(path), e)
    except:
        log_and_raise_exception("Error in resize_picture_in_temp on path {}".format(path), e)
    
    return byte_io
Пример #14
0
def create_thumbnails():
    
    lst = []
    for usr in User.objects.all():
        profile = usr.profile
        
        if profile is not None:
                        
            if profile.create_thumbs is False:
                continue
            irods_user = profile.irods_user
            root = profile.root
            
            #root = '/tempZone/home/garys'

            with TuRODSSession(irods_user) as session:
                try:
                    coll = session.collections.get(root)
                    create_thumbnails_recursive(coll, lst)
                except Exception as e:
                    log_and_raise_exception("Error in create_thumbnails", e)

    '''
    placed the TuRODSSession in the loop: when the file for some reason is not available anymore ( bug iRods? )
    then a new connection would be made and the old one not be disconnected. By placing in the loop: after the with it will be closed!
    '''
    for entry in lst:
        file_data = None
        path = entry['path']
        folder_id = entry['f_id']
        with TuRODSSession(irods_user) as session:
            print('Creating thumb for ', path)
            obj = session.data_objects.get(path)
            file_data = resize_picture_in_temp(obj, path)
             
        if file_data is not None:
            t = Thumbnail()
            t.image = 'data:image/jpeg;base64,{}'.format(base64.b64encode(file_data.getvalue()).decode("ascii"))
            t.id = obj.id
            t.name = os.path.basename(obj.path)
            t.folder_id = folder_id
            t.save()             
Пример #15
0
def show_groups(users_dict):
    with TuRODSSession() as session:

        query = session.query(UserGroup.id, UserGroup.name)
        for result in query.get_results():
            print(result[UserGroup.id], result[UserGroup.name])

        sql = "select group_user_id, user_id from R_user_group"

        alias = 'blepper15'
        columns = [CollectionAccess.user_id, CollectionAccess.access_id]
        query = SpecificQuery(session, sql, alias, columns)

        # register specific query in iCAT
        _ = query.register()

        exception = None
        n = 0
        try:
            query.execute()
            for result in query.get_results():
                n = n + 1
                user = users_dict[result[CollectionAccess.access_id]]
                #print(n,result[CollectionAccess.user_id], user)
                print(result[CollectionAccess.user_id],
                      result[CollectionAccess.access_id])

        except CAT_NO_ROWS_FOUND:
            print('no exception')
        except Exception as e:
            exception = e

        _ = query.remove()

        if exception is not None:
            log_and_raise_exception('Error', exception)
Пример #16
0
def search_objects(coll, parent, filters, irods_user_id):
    
    folder = coll.path
    
    clauses = []
    thumbs_ids_collection = []

    with TuRODSSession() as session:

        base_sql = "select R_DATA_MAIN.data_name, R_DATA_MAIN.data_id, R_DATA_MAIN.data_size from R_DATA_MAIN " \
            "JOIN R_OBJT_METAMAP r_data_metamap ON R_DATA_MAIN.data_id = r_data_metamap.object_id " \
            "JOIN R_META_MAIN r_data_meta_main ON r_data_metamap.meta_id = r_data_meta_main.meta_id " \
            "JOIN R_OBJT_ACCESS OA ON OA.object_id = R_DATA_MAIN.data_id " \
            "where R_DATA_MAIN.coll_id = {0} and user_id in (select group_user_id from R_user_group where user_id = {1})".format(coll.id, irods_user_id)


        if filters is not None:
            for key in filters.keys():
                clause = create_obj_filter(key, filters[key])
                if clause != None:
                    clauses.append(clause)

        nb_of_filters = len(clauses)

                
        columns = [DataObject.name, DataObject.id, DataObject.size]
                
        if nb_of_filters == 0:
            sql = "select R_DATA_MAIN.data_name, R_DATA_MAIN.data_id, R_DATA_MAIN.data_size from R_DATA_MAIN " \
            "JOIN R_OBJT_ACCESS OA ON OA.object_id = R_DATA_MAIN.data_id " \
            "where R_DATA_MAIN.coll_id = {0} and user_id in (select group_user_id from R_user_group where user_id = {1})".format(coll.id, irods_user_id)

                      
        elif nb_of_filters == 1:
            sql = base_sql + clauses[0]
                                 
        else:
            sql = ''
            n = 0
            for clause in clauses:
                if n == 0:
                    sql = base_sql + clause
                    n = n + 1
                else:
                    sql = sql + ' intersect ' + base_sql + clause

        alias = 'xx_list_data_name_id11'
        query = SpecificQuery(session, sql, alias, columns)
        
        # register specific query in iCAT
        _ = query.register()
        exception = None
        

        try:
            thumbs = []
            results = query.get_results()
            for result in results:
                name = result[DataObject.name]
                path = folder  + "/" + name
                id = result[DataObject.id]
                size = result[DataObject.size]         
         
                filename, extension = os.path.splitext(name)
                if is_thumbnail(extension):
                    thumbs.append(thumbs)
                    #th = Thumbnail.objects.filter(id=id).first()
                    #if th is not None:
                    thumbs_ids_collection.append(id)
                        
                Node(name, parent=parent, id=id, path=path, leaf=True, size=size)

        except CAT_NO_ROWS_FOUND:
            pass
        except Exception as e:
            exception = e

        # delete specific query
        _ = query.remove()
        if exception is not None:
            log_and_raise_exception("Error in search_objects", exception)
        
    return thumbs_ids_collection