Example #1
0
def dbLoadAllSettings(db, user):
    """ Load all settings and arrange them in a tree:
            klass -> group_id -> id -> SETTING_STUFF
        where SETTING_STUFF is the enriched form:
            {
                'setting': Setting object as from db
                + more on-the-fly calculated things
            }
        .
    """
    richSettingTree = {
        klass: {
            groupId: {st.id: _enrichSettingObject(st)
                      for st in kgSettingList}
            for groupId, kgSettingList in convertIterableToDictOfLists(
                kSettingList,
                keyer=lambda st: st.group_id,
                valuer=lambda st: st,
            ).items()
        }
        for klass, kSettingList in convertIterableToDictOfLists(
            (Setting(**sDict) for sDict in dbRetrieveAllRecords(
                db,
                'settings',
                dbTablesDesc=dbSchema,
            )),
            keyer=lambda st: st.klass,
            valuer=lambda st: st,
        ).items()
    }
    #
    return richSettingTree
Example #2
0
def dbGetAllRoles(db, user):
    """ Get all available roles for a given user.
        This has to be accessible to anyone.
    """
    return (Role(**r) for r in dbRetrieveAllRecords(
        db,
        'roles',
        dbTablesDesc=dbSchema,
    ))
Example #3
0
def getAttemptedLogins(db):
    """ Retrieve and return all attempted logins collected.
        Caution: may become heavy as time goes by (unless
        some cleanup or restricted query logic is introduced).
    """
    return (AttemptedLogin(**atDict) for atDict in dbRetrieveAllRecords(
        db,
        'attempted_logins',
        dbTablesDesc=dbSchema,
    ))
Example #4
0
def dbGetAllUsers(db, user, accountDeletionInProgress=False):
    """Get a listing of all existing users (except the '' system user)."""
    if accountDeletionInProgress or userIsAdmin(db, user):
        return (
            User(**u)
            for u in dbRetrieveAllRecords(
                db,
                'users',
                dbTablesDesc=dbSchema,
            )
            if u['username'] != ''
        )
    else:
        raise OstracionError('Insufficient permissions')
Example #5
0
def fixRoleTablesAddingRoleClass(db):
    """ Deal with the schema changes
        to bring 'roles' from
            primary key = role_id
        to
            primary_key = (role_class, role_id)
        by acting on all involved tables
        while preserving the contents thereof.

        Returns whether it did something or not as a bool
    """
    rolesTableExists = dbTableExists(db, 'roles')
    roleColumns = dbQueryColumns(db, 'roles') if rolesTableExists else {}
    mustAct = all([
        rolesTableExists,
        'role_class' not in roleColumns,
        'system' in roleColumns,
    ])
    if not mustAct:
        return False
    else:
        print(' * Applying patch to roles')
        # get ready to dance
        targetSchema = {
            tempTableName(tName): tDesc
            for tName, tDesc in dbSchema.items()
            if tName in legacySchema
        }
        targetTableCreationOrder = {
            tempTableName(k): v
            for k, v in tableCreationOrder.items()
        }
        # create the transitional tables (new schema)
        for ntName, ntContents in sorted(
                targetSchema.items(),
                key=lambda tnc: targetTableCreationOrder[tnc[0]]):
            print('     * creating "%s" ' % ntName, end='')
            dbCreateTable(
                db,
                ntName,
                {k: v for k, v in ntContents.items() if k != 'foreign_keys'},
            )
            print('done.')
        # copy items table by table
        print('     * populating ...')
        for srcTName, srcSchema in sorted(
                legacySchema.items(),
                key=lambda tnc: tableCreationOrder[tnc[0]]):
            print('         - "%s" ...' % srcTName)
            # read all items from this srcTName and
            # copy it - with changes - onto tempTableName(srcTName)
            dstTName = tempTableName(srcTName)
            dstSchema = targetSchema[dstTName]
            for inRecordIndex, inRecord in enumerate(dbRetrieveAllRecords(
                db,
                srcTName,
                dbTablesDesc=legacySchema,
            )):
                print('             record[%i] ... ' % inRecordIndex, end='')
                outRecord = convertRoleRelatedRecord(db, legacySchema,
                                                     srcTName, inRecord)
                dbAddRecordToTable(
                    db,
                    dstTName,
                    outRecord,
                    dbTablesDesc=targetSchema,
                )
                print('inserted.')
            print('         - done "%s"' % srcTName)
        print('     * done populating.')
        # we drop the original tables and recreate them with the new schema
        print('     * dropping legacy tables ...')
        for srcTName, srcSchema in sorted(
                legacySchema.items(),
                key=lambda tnc: tableCreationOrder[tnc[0]],
                reverse=True):
            print('         - "%s" ... ' % srcTName, end='')
            dbDeleteTable(db, srcTName)
            print('dropped.')
        print('     * done dropping legacy tables.')
        # we recreate the tables with the new schema
        print('     * re-creating tables ...')
        for srcTName, _ in sorted(
                legacySchema.items(),
                key=lambda tnc: tableCreationOrder[tnc[0]]):
            print('         - "%s" ... ' % srcTName, end='')
            dbCreateTable(
                db,
                srcTName,
                dbSchema[srcTName],
            )
            print('re-created.')
        print('     * done re-creating tables.')
        # we populate the recreated tables with the temporary tables' contents
        print('     * repopulating tables ...')
        for srcTName, srcSchema in sorted(
                legacySchema.items(),
                key=lambda tnc: tableCreationOrder[tnc[0]]):
            print('         - "%s" ... ' % srcTName)
            for recordIndex, record in enumerate(dbRetrieveAllRecords(
                db,
                tempTableName(srcTName),
                dbTablesDesc=targetSchema,
            )):
                print('             record [%i] ... ' % recordIndex, end='')
                dbAddRecordToTable(
                    db,
                    srcTName,
                    record,
                    dbTablesDesc=dbSchema,
                )
                print('inserted.')
            print('         - done "%s"' % srcTName)
        print('     * done repopulating tables.')
        # we drop the temporary tables
        print('     * dropping temporary tables ...')
        for srcTName, srcSchema in sorted(
                legacySchema.items(),
                key=lambda tnc: tableCreationOrder[tnc[0]],
                reverse=True):
            print('         - "%s" ... ' % srcTName, end='')
            deleteeTName = tempTableName(srcTName)
            dbDeleteTable(db, deleteeTName)
            print('dropped.')
        print('     * done dropping temporary tables.')
        #
        return True
Example #6
0
 #
 tempSchema = recursivelyMergeDictionaries(
     {
         tName: {
             'columns': [
                 col
                 for col in dbSchema[tName]['columns'] if
                 col[0] not in {'title', 'dvector_title'}
             ]
         }
     },
     defaultMap={tName: dbSchema[tName]},
 )
 for prevLinkEntryDict in dbRetrieveAllRecords(
         db,
         tName,
         tempSchema,
 ):
     print('<', end='')
     newLink = Link(**(recursivelyMergeDictionaries(
         {'title': prevLinkEntryDict['name']},
         prevLinkEntryDict,
     ))).asDict()
     print(newLink)
     dbUpdateRecordOnTable(
         db,
         tName,
         newLink,
         dbTablesDesc=dbSchema,
     )
     print('*> ', end='')
Example #7
0
def explicitLoopSimilaritySearch(
        db,
        searchTerm,
        searchBoxes=True,
        searchFiles=True,
        searchLinks=True,
        useDescription=False):
    """ Explicit loop over files/boxes
        and use a 'similarity' logic to pick matching results.

        Same return object as 'sqliteLikeSubstringSearch'.
    """
    searchTermDVector = textToDVector(searchTerm)
    if len(searchTermDVector) > 0:
        #
        if searchFiles:
            foundFiles = [
                ffRichItem
                for ffRichItem in (
                    {
                        'item': File(**fileDict),
                        'score': _itemSimilarityMatchScore(
                            'file',
                            searchTermDVector,
                            fileDict,
                            useDescription,
                        ),
                    }
                    for fileDict in dbRetrieveAllRecords(
                        db,
                        'files',
                        dbTablesDesc=dbSchema,
                    )
                )
                if ffRichItem['score'] >= similarityScoreThreshold
            ]
        else:
            foundFiles = []
        #
        if searchBoxes:
            foundBoxes = [
                fbRichItem
                for fbRichItem in (
                    {
                        'item': Box(**boxDict),
                        'score': _itemSimilarityMatchScore(
                            'box',
                            searchTermDVector,
                            boxDict,
                            useDescription,
                        ),
                    }
                    for boxDict in dbRetrieveAllRecords(
                        db,
                        'boxes',
                        dbTablesDesc=dbSchema,
                    )
                    if boxDict['box_id'] != ''
                )
                if fbRichItem['score'] >= similarityScoreThreshold
            ]
        else:
            foundBoxes = []
        #
        if searchLinks:
            foundLinks = [
                flRichItem
                for flRichItem in (
                    {
                        'item': Link(**linkDict),
                        'score': _itemSimilarityMatchScore(
                            'link',
                            searchTermDVector,
                            linkDict,
                            useDescription,
                        ),
                    }
                    for linkDict in dbRetrieveAllRecords(
                        db,
                        'links',
                        dbTablesDesc=dbSchema,
                    )
                )
                if flRichItem['score'] >= similarityScoreThreshold
            ]
        else:
            foundLinks = []
        #
        return {
            'files': foundFiles,
            'boxes': foundBoxes,
            'links': foundLinks,
        }
    else:
        return {
            'files': [],
            'boxes': [],
            'links': [],
            'message': 'Not enough digits/letters in search term',
        }
Example #8
0
def explicitLoopSubstringSearch(
        db,
        searchTerm,
        searchBoxes=True,
        searchFiles=True,
        searchLinks=True,
        caseSensitive=True,
        useDescription=False):
    """ Explicit loop over the files/boxes tables
        and pick the substring-matching items.
        Same return object as 'sqliteLikeSubstringSearch'.

        We explicitly browse all files and all boxes
        and apply the search with the options passed.
        Not so fast but highly extendable in the future.
    """
    #
    _strippedSearchTerm = stripToAscii(searchTerm)
    _caseAdjustedSearchTerm = (
        _strippedSearchTerm if caseSensitive else _strippedSearchTerm.lower()
    )
    searchTokens = {
        itm.strip()
        for itm in _caseAdjustedSearchTerm.split(' ')
        if itm.strip() != ''
    }
    if len(searchTokens) > 0:
        #
        tokenResLens = [
            (re.compile(re.escape(tk)), len(tk))
            for tk in searchTokens
        ]
        #
        if searchFiles:
            foundFiles = [
                ffRichItem
                for ffRichItem in (
                    {
                        'item': File(**fileDict),
                        'score': _itemSubstringMatchScore(
                            'file',
                            tokenResLens,
                            fileDict,
                            caseSensitive,
                            useDescription,
                        ),
                    }
                    for fileDict in dbRetrieveAllRecords(
                        db,
                        'files',
                        dbTablesDesc=dbSchema,
                    )
                )
                if ffRichItem['score'] > 0
            ]
        else:
            foundFiles = []
        #
        if searchBoxes:
            foundBoxes = [
                fbRichItem
                for fbRichItem in (
                    {
                        'item': Box(**boxDict),
                        'score': _itemSubstringMatchScore(
                            'box',
                            tokenResLens,
                            boxDict,
                            caseSensitive,
                            useDescription,
                        ),
                    }
                    for boxDict in dbRetrieveAllRecords(
                        db,
                        'boxes',
                        dbTablesDesc=dbSchema,
                    )
                    if boxDict['box_id'] != ''
                )
                if fbRichItem['score'] > 0
            ]
        else:
            foundBoxes = []
        #
        if searchLinks:
            foundLinks = [
                flRichItem
                for flRichItem in (
                    {
                        'item': Link(**linkDict),
                        'score': _itemSubstringMatchScore(
                            'link',
                            tokenResLens,
                            linkDict,
                            caseSensitive,
                            useDescription,
                        ),
                    }
                    for linkDict in dbRetrieveAllRecords(
                        db,
                        'links',
                        dbTablesDesc=dbSchema,
                    )
                )
                if flRichItem['score'] > 0
            ]
        else:
            foundLinks = []
        #
        return {
            'files': foundFiles,
            'boxes': foundBoxes,
            'links': foundLinks,
        }
    else:
        return {
            'files': [],
            'boxes': [],
            'links': [],
            'message': 'No search terms provided',
        }