Example #1
0
def add_module_file(plpy, td):
    """Postgres database trigger for adding a module file

    When a module file index.cnxml is added to the database, the trigger
    transforms it into html and stores it in the database as index.cnxml.html.
    """
    import plpydbapi

    filename = td['new']['filename']
    if filename != 'index.cnxml':
        return

    module_ident = td['new']['module_ident']

    # Delete index.cnxml.html
    stmt = plpy.prepare('''DELETE FROM module_files
    WHERE filename = 'index.cnxml.html' AND module_ident = $1
    RETURNING fileid''', ['integer'])
    result = plpy.execute(stmt, [module_ident])
    if result:
        # There can only be one fileid returned from the above sql
        # "module_files_idx" UNIQUE, btree (module_ident, filename)
        fileid = result[0]['fileid']
        stmt = plpy.prepare('DELETE FROM files WHERE fileid = $1', ['integer'])
        plpy.execute(stmt, [fileid])

    with plpydbapi.connect() as db_connection:
        with db_connection.cursor() as cursor:
            plpy.log('produce html and abstract html for {}'.format(module_ident))
            produce_html_for_module(db_connection, cursor, module_ident)
            produce_html_for_abstract(db_connection, cursor, module_ident)
        db_connection.commit()
    return
Example #2
0
def republish_module_trigger(plpy, td):
    """Postgres database trigger for republishing a module

    When a module is republished, the versions of the collections that it is
    part of will need to be updated (a minor update).


    e.g. there is a collection c1 v2.1, which contains module m1 v3

    m1 is updated, we have a new row in the modules table with m1 v4

    this trigger will create increment the minor version of c1, so we'll have
    c1 v2.2

    we need to create a collection tree for c1 v2.2 which is exactly the same
    as c1 v2.1, but with m1 v4 instead of m1 v3, and c1 v2.2 instead of c1 v2.2
    """
    import plpydbapi

    plpy.log('Trigger fired on %s' % (td['new']['moduleid'],))

    with plpydbapi.connect() as db_connection:
        with db_connection.cursor() as cursor:
            modified = republish_module(td, cursor, db_connection)
            plpy.log('modified: {}'.format(modified))
            plpy.log('insert values:\n{}\n'.format('\n'.join([
                '{}: {}'.format(key, value)
                for key, value in td['new'].iteritems()])))
        db_connection.commit()

    return modified