예제 #1
0
def check_dataset_mismatch(session_id, cache, data, dataset):
    if not cache.hexists(session_id, cache_utils.CacheKeys.SEQUENCE.value):
        return False

    seq_fname = decompress_data(
        cache.hget(session_id, cache_utils.CacheKeys.SEQUENCE.value))
    seq_length = len(decompress_data(cache.hget(session_id, seq_fname)))
    data = decompress_data(data)

    if dataset in loaders.AdditionalDatasetReference._value2member_map_:
        if len(data) != seq_length:
            return seq_fname
        else:
            return False
    elif data[0] == 'PDB' or data[0] == 'DISTO':
        max_register = max(
            (max(data[1:],
                 key=itemgetter(0))[0], max(data[1:], key=itemgetter(1))[0]))
    else:
        max_register = max(
            (max(data, key=itemgetter(0))[0], max(data, key=itemgetter(1))[0]))

    if max_register > seq_length:
        return seq_fname

    return False
예제 #2
0
def serve_url(url, session_id, cache, logger):
    if cache.hexists(session_id, cache_utils.CacheKeys.USER.value):
        username = decompress_data(
            cache.hget(session_id, cache_utils.CacheKeys.USER.value))
    else:
        username = None

    if url == UrlIndex.HOME.value or url == UrlIndex.ROOT.value:
        return layouts.Home(session_id, username)
    elif url == UrlIndex.CONTACT.value:
        return layouts.Contact(session_id, username)
    elif url == UrlIndex.PLOT.value:
        if cache.hexists(session_id, cache_utils.CacheKeys.FIGURE_JSON.value):
            figure = decompress_data(
                cache.hget(session_id,
                           cache_utils.CacheKeys.FIGURE_JSON.value))
            display_settings = decompress_data(
                cache.hget(session_id,
                           cache_utils.CacheKeys.DISPLAY_CONTROL_JSON.value))
            return layouts.Plot(session_id, username, figure, display_settings)
        else:
            return layouts.Plot(session_id, username)
    elif url == UrlIndex.HELP.value:
        return layouts.Help(session_id, username, cache)
    elif url == UrlIndex.RIGDEN.value:
        return layouts.RigdenLab(session_id, username)
    elif url == UrlIndex.PRIVACY_POLICY.value:
        return layouts.PrivacyPolicy(session_id)
    elif url in (UrlIndex.USERS_PORTAL.value, UrlIndex.CREATE_USER.value,
                 UrlIndex.CHANGE_PASSWORD.value, UrlIndex.SHARE_SESSIONS.value,
                 UrlIndex.USER_STORAGE.value, UrlIndex.ACCOUNT_RECOVERY.value):
        if not postgres_utils.is_postgres_available(logger):
            return layouts.PostgresConnectionError()
        elif url == UrlIndex.USERS_PORTAL.value:
            return layouts.UsersPortal(username)
        elif url == UrlIndex.CREATE_USER.value:
            return layouts.CreateUser(username)
        elif url == UrlIndex.CHANGE_PASSWORD.value:
            return layouts.ChangeUserPassword(username)
        elif url == UrlIndex.SHARE_SESSIONS.value:
            return layouts.ShareSession(username)
        elif url == UrlIndex.ACCOUNT_RECOVERY.value:
            return layouts.AccountRecoveryPortal()
        elif url == UrlIndex.USER_STORAGE.value:
            if cache.hexists(session_id,
                             cache_utils.CacheKeys.SESSION_PKID.value):
                session_pkid = int(
                    cache.hget(session_id,
                               cache_utils.CacheKeys.SESSION_PKID.value))
                return layouts.UserStorage(username, session_pkid)
            else:
                return layouts.UserStorage(username)
    else:
        logger.error('404 page not found {}'.format(url))
        return layouts.noPage(url, username)
예제 #3
0
def upload_sequence(fname, fcontent, session_id, cache, logger):
    logger.info('Session {} sequence upload triggered'.format(session_id))

    if cache.hexists(session_id, fname):
        logger.info('Session {} filename {} already exists'.format(
            session_id, fname))
        return no_update, None, components.RepeatedInputModal(fname)
    if cache.hexists(session_id, cache_utils.CacheKeys.SEQUENCE.value):
        logger.info(
            'Session {} sequence dataset already uploaded'.format(session_id))
        return no_update, None, components.SequenceAlreadyUploadedModal()

    sequence_data, seq_hydrophobicity, invalid = loaders.SequenceLoader(
        fcontent)

    if invalid:
        logger.info('Session {} file {} is invalid sequence file'.format(
            session_id, fname))
        return no_update, None, components.InvalidFormatModal()

    mismatched = check_sequence_mismatch(session_id, cache,
                                         len(decompress_data(sequence_data)))
    if any(mismatched):
        logger.info('Session {} mismatch {} sequence file detected'.format(
            session_id, fname))
        return no_update, None, components.MismatchSequenceModal(*mismatched)

    logger.info('Session {} uploads {} - sequence'.format(session_id, fname))
    cache.hset(session_id, fname, sequence_data)
    cache.hset(session_id, cache_utils.CacheKeys.SEQUENCE_HYDROPHOBICITY.value,
               seq_hydrophobicity)
    cache.hset(session_id, cache_utils.CacheKeys.SEQUENCE.value,
               compress_data(fname))
    return components.FilenameAlert(
        fname, loaders.DatasetReference.SEQUENCE.value), None, None
예제 #4
0
def retrieve_sequence_fname(session_id, cache):
    if cache.hexists(session_id, cache_utils.CacheKeys.SEQUENCE.value):
        fname = decompress_data(
            cache.hget(session_id, cache_utils.CacheKeys.SEQUENCE.value))
        return components.FilenameAlert(fname, DatasetReference.SEQUENCE.value)
    else:
        return no_update
예제 #5
0
def decompress_session(session):
    for key in (b'id', b'user', b'session_pkid'):
        if key in session:
            del session[key]

    for key in session.keys():
        session[key] = decompress_data(session[key])
    return session
예제 #6
0
def change_password(new_password, old_password, cache, session_id, logger):
    username = decompress_data(
        cache.hget(session_id, cache_utils.CacheKeys.USER.value))
    if postgres_utils.change_password(username, old_password, new_password):
        logger.info('Session {} user {} changed password'.format(
            session_id, username))
        return components.SuccessChangePasswordAlert(username)
    else:
        return components.FailChangePasswordAlert(username)
예제 #7
0
def get_current_info(session_id, cache):
    username = decompress_data(
        cache.hget(session_id, cache_utils.CacheKeys.USER.value))
    if cache.hexists(session_id, cache_utils.CacheKeys.SESSION_PKID.value):
        current_session_pkid = cache.hget(
            session_id, cache_utils.CacheKeys.SESSION_PKID.value)
    else:
        current_session_pkid = None

    return username, current_session_pkid
예제 #8
0
def prepare_session_storage(session):
    for key in (b'id', b'user', b'session_pkid'):
        if key in session:
            del session[key]

    key_list = list(session.keys())
    for key in key_list:
        session[key.decode()] = decompress_data(session[key])
        del session[key]

    return compress_data(session)
예제 #9
0
def retrieve_contact_fnames(session_id, cache):
    fname_alerts = []
    if cache.hexists(session_id, cache_utils.CacheKeys.CONTACT_MAP.value):
        fname_list = decompress_data(
            cache.hget(session_id, cache_utils.CacheKeys.CONTACT_MAP.value))
        for fname in fname_list:
            fname_alerts.append(
                components.FilenameAlert(fname,
                                         DatasetReference.CONTACT_MAP.value))

    if not fname_alerts:
        return no_update

    return fname_alerts
예제 #10
0
def check_sequence_mismatch(session_id, cache, seq_length):
    mismatched = []

    if cache.hexists(session_id, cache_utils.CacheKeys.CONTACT_MAP.value):
        cmap_fnames = decompress_data(
            cache.hget(session_id, cache_utils.CacheKeys.CONTACT_MAP.value))
        for cmap_fname in cmap_fnames:
            cmap_data = decompress_data(cache.hget(session_id, cmap_fname))
            if cmap_data[0] == 'PDB' or cmap_data[0] == 'DISTO':
                cmap_data = cmap_data[1:]
            cmap_max_register = max((max(cmap_data, key=itemgetter(0))[0],
                                     max(cmap_data, key=itemgetter(1))[0]))
            if cmap_max_register > seq_length:
                mismatched.append(cmap_fname)

    for dataset in loaders.AdditionalDatasetReference:
        if cache.hexists(session_id, dataset.value):
            fnames = decompress_data(cache.hget(session_id, dataset.value))
            for fname in fnames:
                data = decompress_data(cache.hget(session_id, fname))
                if data is not None and len(data) != seq_length:
                    mismatched.append(fname)

    return mismatched
예제 #11
0
def retrieve_additional_fnames(session_id, cache):
    fname_alerts = []

    for dataset in AdditionalDatasetReference:
        if cache.hexists(session_id, dataset.value):
            dataset_fnames = decompress_data(
                cache.hget(session_id, dataset.value))
            for fname in dataset_fnames:
                fname_alerts.append(
                    components.FilenameAlert(fname, dataset.value))

    if not fname_alerts:
        return no_update
    else:
        return fname_alerts
예제 #12
0
def store_session(session_name, session_id, cache, logger):
    username = decompress_data(
        cache.hget(session_id, cache_utils.CacheKeys.USER.value))
    session = cache.hgetall(session_id)

    if not session_name:
        return components.SessionStoreModal(None)

    logger.info('Session {} user {} stores new session {}'.format(
        session_id, username, session_name))
    session_pkid = postgres_utils.store_session(username, session_name,
                                                session)
    cache.hset(session_id, cache_utils.CacheKeys.SESSION_PKID.value,
               session_pkid)

    return components.SessionStoreModal(session_name)
예제 #13
0
def retrieve_session(session_pkid):
    session = None
    username = None
    session_name = None
    session_data = perform_query(SqlQueries.RETRIEVE_SESSION.value, args=(session_pkid,), fetch=True)

    if session_data:
        perform_query(SqlQueries.UPDATE_LAST_ACCESS.value,
                      args=(datetime.datetime.now().strftime("%Y-%m-%d"), session_pkid), commit=True)
        session_data = session_data[0]
        now = datetime.datetime.now().strftime("%Y-%m-%d")
        perform_query(SqlQueries.UPDATE_SESSION_DATE.value, args=(now, session_pkid), commit=False)
        username = session_data[0]
        session_name = session_data[1]
        session = decompress_data(session_data[2])
        for key in session.keys():
            session[key] = compress_data(session[key])

    return username, session_name, session