Example #1
0
 def __getattr__(self, name):
     # Perform custom logic here
     if name == '__file__':
         return __file__
     elif name == '__path__':
         return os.path.dirname(__file__)
     try:
         from invenio.base.helpers import utf8ifier
         return utf8ifier(self.wrapped[name])
     except:
         pass
Example #2
0
 def __getattr__(self, name):
     # Perform custom logic here
     if name == '__file__':
         return __file__
     elif name == '__path__':
         return os.path.dirname(__file__)
     try:
         from invenio.base.helpers import utf8ifier
         return utf8ifier(self.wrapped[name])
     except:
         pass
Example #3
0
def get_values_recursively(subfield, phrases):
    """Finds all values suitable for later tokenizing in
       field/subfield of bibfield record.
       @param subfield: name of the field/subfield
       @param phrases: container for phrases (for example empty list)

       FIXME: move this function to bibfield!
       As soon as possible. Note that journal tokenizer
       also needs to be changed.
    """
    if _is_collection(subfield):
        for s in _get_values(subfield):
            get_values_recursively(s, phrases)
    elif subfield is not None:
        phrases.append(utf8ifier(subfield))
Example #4
0
def get_values_recursively(subfield, phrases):
    """Finds all values suitable for later tokenizing in
       field/subfield of bibfield record.
       @param subfield: name of the field/subfield
       @param phrases: container for phrases (for example empty list)

       FIXME: move this function to bibfield!
       As soon as possible. Note that journal tokenizer
       also needs to be changed.
    """
    if _is_collection(subfield):
        for s in _get_values(subfield):
            get_values_recursively(s, phrases)
    elif subfield is not None:
        phrases.append(utf8ifier(subfield))
Example #5
0
 def __getattr__(self, name):
     """Get attr."""
     # Perform custom logic here
     if name == '__file__':
         return __file__
     elif name == '__path__':
         return os.path.dirname(__file__)
     try:
         from invenio.base.helpers import utf8ifier
         warnings.warn(
             "Usage of invenio.config.{0} is deprecated".format(name),
             RemovedInInvenio23Warning
         )
         return utf8ifier(self.wrapped[name])
     except Exception:
         pass
Example #6
0
 def _save_master_format(obj, eng):
     from invenio.base.helpers import utf8ifier
     from invenio.modules.formatter.models import Bibfmt
     from invenio.ext.sqlalchemy import db
     from zlib import compress
     eng.log.info('Saving master record to DB')
     bibfmt = Bibfmt(
         id_bibrec=obj[1]['recid'],
         format=obj[1].additional_info.master_format,
         kind='master',
         last_updated=obj[1]['modification_date'],
         value=compress(
             utf8ifier(obj[0] if obj[1].additional_info.master_format ==
                       'marc' else obj[1].legacy_export_as_marc())))
     db.session.add(bibfmt)
     db.session.commit()
     eng.log.info('Master record saved to DB')
Example #7
0
 def _save_master_format(obj, eng):
     from invenio.base.helpers import utf8ifier
     from invenio.modules.formatter.models import Bibfmt
     from invenio.ext.sqlalchemy import db
     from zlib import compress
     eng.log.info('Saving master record to DB')
     bibfmt = Bibfmt(id_bibrec=obj[1]['recid'],
                     format=obj[1].additional_info.master_format,
                     kind='master',
                     last_updated=obj[1]['modification_date'],
                     value=compress(utf8ifier(
                         obj[0]
                         if obj[1].additional_info.master_format == 'marc'
                         else obj[1].legacy_export_as_marc()
                     )))
     db.session.add(bibfmt)
     db.session.commit()
     eng.log.info('Master record saved to DB')
Example #8
0
def run_sql(sql, param=None, n=0, with_desc=False, with_dict=False,
            run_on_slave=False, connection=None):
    """Run SQL on the server with PARAM and return result.

    :param param: tuple of string params to insert in the query (see
    notes below)
    :param n: number of tuples in result (0 for unbounded)
    :param with_desc: if True, will return a DB API 7-tuple describing
    columns in query.
    :param with_dict: if True, will return a list of dictionaries
    composed of column-value pairs
    :param connection: if provided, uses the given connection.
    :return: If SELECT, SHOW, DESCRIBE statements, return tuples of data,
    followed by description if parameter with_desc is
    provided.
    If SELECT and with_dict=True, return a list of dictionaries
    composed of column-value pairs, followed by description
    if parameter with_desc is provided.
    If INSERT, return last row id.
    Otherwise return SQL result as provided by database.

    @note: When the site is closed for maintenance (as governed by the
    config variable CFG_ACCESS_CONTROL_LEVEL_SITE), do not attempt
    to run any SQL queries but return empty list immediately.
    Useful to be able to have the website up while MySQL database
    is down for maintenance, hot copies, table repairs, etc.
    @note: In case of problems, exceptions are returned according to
    the Python DB API 2.0.  The client code can import them from
    this file and catch them.
    """
    if cfg['CFG_ACCESS_CONTROL_LEVEL_SITE'] == 3:
        # do not connect to the database as the site is closed for maintenance:
        return []
    elif cfg['CFG_ACCESS_CONTROL_LEVEL_SITE'] > 0:
        # Read only website
        if not sql.upper().startswith("SELECT") and \
                not sql.upper().startswith("SHOW"):
            return

    if param:
        param = tuple(param)

    # FIXME port database slave support
    dbhost = cfg['CFG_DATABASE_HOST']
    if run_on_slave and cfg['CFG_DATABASE_SLAVE']:
        dbhost = cfg['CFG_DATABASE_SLAVE']

    if 'sql-logger' in cfg.get('CFG_DEVEL_TOOLS', []):
        log_sql_query(dbhost, sql, param)

    gc.disable()
    engine = db.engine.execution_options(use_unicode=0)
    sql = sql.replace('`', '"')
    current_app.logger.info(sql)
    if param is None:
        cur = engine.execute(sql.replace('%', '%%'))
    else:
        cur = engine.execute(sql, (param, ))
    gc.enable()

    if string.upper(string.split(sql)[0]) in \
            ("SELECT", "SHOW", "DESC", "DESCRIBE"):
        if n:
            recset = cur.fetchmany(n)
        else:
            recset = cur.fetchall()

        from invenio.base.helpers import utf8ifier
        recset = map(dict if with_dict else tuple, recset)
        recset = utf8ifier(recset)

        if with_desc:
            return recset, cur.description
        else:
            return recset
    else:
        if string.upper(string.split(sql)[0]) == "INSERT":
            return cur.lastrowid
        return cur
Example #9
0
def run_sql(sql,
            param=None,
            n=0,
            with_desc=False,
            with_dict=False,
            run_on_slave=False,
            connection=None):
    """Run SQL on the server with PARAM and return result.

    :param param: tuple of string params to insert in the query (see
    notes below)
    :param n: number of tuples in result (0 for unbounded)
    :param with_desc: if True, will return a DB API 7-tuple describing
    columns in query.
    :param with_dict: if True, will return a list of dictionaries
    composed of column-value pairs
    :param connection: if provided, uses the given connection.
    :return: If SELECT, SHOW, DESCRIBE statements, return tuples of data,
    followed by description if parameter with_desc is
    provided.
    If SELECT and with_dict=True, return a list of dictionaries
    composed of column-value pairs, followed by description
    if parameter with_desc is provided.
    If INSERT, return last row id.
    Otherwise return SQL result as provided by database.

    @note: When the site is closed for maintenance (as governed by the
    config variable CFG_ACCESS_CONTROL_LEVEL_SITE), do not attempt
    to run any SQL queries but return empty list immediately.
    Useful to be able to have the website up while MySQL database
    is down for maintenance, hot copies, table repairs, etc.
    @note: In case of problems, exceptions are returned according to
    the Python DB API 2.0.  The client code can import them from
    this file and catch them.
    """
    if cfg['CFG_ACCESS_CONTROL_LEVEL_SITE'] == 3:
        # do not connect to the database as the site is closed for maintenance:
        return []
    elif cfg['CFG_ACCESS_CONTROL_LEVEL_SITE'] > 0:
        # Read only website
        if not sql.upper().startswith("SELECT") and \
                not sql.upper().startswith("SHOW"):
            return

    if param:
        param = tuple(param)

    # FIXME port database slave support
    dbhost = cfg['CFG_DATABASE_HOST']
    if run_on_slave and cfg['CFG_DATABASE_SLAVE']:
        dbhost = cfg['CFG_DATABASE_SLAVE']

    if 'sql-logger' in cfg.get('CFG_DEVEL_TOOLS', []):
        log_sql_query(dbhost, sql, param)

    gc.disable()
    engine = db.engine.execution_options(use_unicode=0)
    sql = sql.replace('`', '"')
    current_app.logger.info(sql)
    if param is None:
        cur = engine.execute(sql.replace('%', '%%'))
    else:
        cur = engine.execute(sql, (param, ))
    gc.enable()

    if string.upper(string.split(sql)[0]) in \
            ("SELECT", "SHOW", "DESC", "DESCRIBE"):
        if n:
            recset = cur.fetchmany(n)
        else:
            recset = cur.fetchall()

        from invenio.base.helpers import utf8ifier
        recset = map(dict if with_dict else tuple, recset)
        recset = utf8ifier(recset)

        if with_desc:
            return recset, cur.description
        else:
            return recset
    else:
        if string.upper(string.split(sql)[0]) == "INSERT":
            return cur.lastrowid
        return cur
Example #10
0
def tupletotable(header=[], tuple=[], start='', end='', extracolumn='', highlight_rows_p=False, alternate_row_colors_p=False):
    """create html table for a tuple.

         header - optional header for the columns

          tuple - create table of this

          start - text to be added in the beginning, most likely beginning of a form

            end - text to be added in the end, mot likely end of a form.

    extracolumn - mainly used to put in a button.

      highlight_rows_p - if the cursor hovering a row should highlight the full row or not

alternate_row_colors_p - if alternate background colours should be used for the rows
    """

    # study first row in tuple for alignment
    align = []
    try:
        firstrow = tuple[0]

        if type(firstrow) in [int, long]:
            align = ['admintdright']
        elif type(firstrow) in [str, dict]:
            align = ['admintdleft']
        else:
            for item in firstrow:
                if type(item) is int:
                    align.append('admintdright')
                else:
                    align.append('admintdleft')
    except IndexError:
        firstrow = []

    tblstr = ''
    for h in header + ['']:
        tblstr += '  <th class="adminheader">%s</th>\n' % (h, )
    if tblstr:
        tblstr = ' <tr>\n%s\n </tr>\n' % (tblstr, )

    tblstr = start + '<table class="admin_wvar_nomargin">\n' + tblstr

    # extra column
    try:
        extra = '<tr class="%s">' % (
            highlight_rows_p and 'admin_row_highlight' or '')

        if type(firstrow) not in [int, long, str, dict]:
            # for data in firstrow: extra += '<td class="%s">%s</td>\n' % ('admintd', data)
            for i in range(len(firstrow)):
                extra += '<td class="{0}">{1}</td>\n'.format(
                    align[i], firstrow[i])
        else:
            extra += '  <td class="%s">%s</td>\n' % (align[0], firstrow)
        extra += '<td class="extracolumn" rowspan="%s" style="vertical-align: top;">\n%s\n</td>\n</tr>\n' % (
            len(tuple), extracolumn)
    except IndexError:
        extra = ''
    tblstr += extra

    # for i in range(1, len(tuple)):
    j = 0
    for row in tuple[1:]:
        j += 1
        tblstr += ' <tr class="%s %s">\n' % (highlight_rows_p and 'admin_row_highlight' or '',
                                             (j % 2 and alternate_row_colors_p) and 'admin_row_color' or '')
        # row = tuple[i]
        if type(row) not in [int, long, str, dict]:
            # for data in row: tblstr += '<td class="admintd">%s</td>\n' % (data,)
            for i in range(len(row)):
                tblstr += '<td class="{0}">{1}</td>\n'.format(align[i], utf8ifier(row[i]))
        else:
            tblstr += '  <td class="%s">%s</td>\n' % (align[0], row)
        tblstr += ' </tr> \n'

    tblstr += '</table> \n '
    tblstr += end

    return tblstr
Example #11
0
def tupletotable(header=[],
                 tuple=[],
                 start='',
                 end='',
                 extracolumn='',
                 highlight_rows_p=False,
                 alternate_row_colors_p=False):
    """create html table for a tuple.

         header - optional header for the columns

          tuple - create table of this

          start - text to be added in the beginning, most likely beginning of a form

            end - text to be added in the end, mot likely end of a form.

    extracolumn - mainly used to put in a button.

      highlight_rows_p - if the cursor hovering a row should highlight the full row or not

alternate_row_colors_p - if alternate background colours should be used for the rows
    """

    # study first row in tuple for alignment
    align = []
    try:
        firstrow = tuple[0]

        if type(firstrow) in [int, long]:
            align = ['admintdright']
        elif type(firstrow) in [str, dict]:
            align = ['admintdleft']
        else:
            for item in firstrow:
                if type(item) is int:
                    align.append('admintdright')
                else:
                    align.append('admintdleft')
    except IndexError:
        firstrow = []

    tblstr = ''
    for h in header + ['']:
        tblstr += '  <th class="adminheader">%s</th>\n' % (h, )
    if tblstr:
        tblstr = ' <tr>\n%s\n </tr>\n' % (tblstr, )

    tblstr = start + '<table class="admin_wvar_nomargin">\n' + tblstr

    # extra column
    try:
        extra = '<tr class="%s">' % (highlight_rows_p and 'admin_row_highlight'
                                     or '')

        if type(firstrow) not in [int, long, str, dict]:
            # for data in firstrow: extra += '<td class="%s">%s</td>\n' % ('admintd', data)
            for i in range(len(firstrow)):
                extra += '<td class="{0}">{1}</td>\n'.format(
                    align[i], firstrow[i])
        else:
            extra += '  <td class="%s">%s</td>\n' % (align[0], firstrow)
        extra += '<td class="extracolumn" rowspan="%s" style="vertical-align: top;">\n%s\n</td>\n</tr>\n' % (
            len(tuple), extracolumn)
    except IndexError:
        extra = ''
    tblstr += extra

    # for i in range(1, len(tuple)):
    j = 0
    for row in tuple[1:]:
        j += 1
        tblstr += ' <tr class="%s %s">\n' % (
            highlight_rows_p and 'admin_row_highlight' or '',
            (j % 2 and alternate_row_colors_p) and 'admin_row_color' or '')
        # row = tuple[i]
        if type(row) not in [int, long, str, dict]:
            # for data in row: tblstr += '<td class="admintd">%s</td>\n' % (data,)
            for i in range(len(row)):
                tblstr += '<td class="{0}">{1}</td>\n'.format(
                    align[i], utf8ifier(row[i]))
        else:
            tblstr += '  <td class="%s">%s</td>\n' % (align[0], row)
        tblstr += ' </tr> \n'

    tblstr += '</table> \n '
    tblstr += end

    return tblstr