Exemplo n.º 1
0
def main(dataset_id, qdict):

    # Open database connection.

    cnx = dbconfig.connect(readonly=False, devel=qdict['dev'])

    # Check access.

    if not dbconfig.restricted_access_allowed() and dbutil.restricted_access(
            cnx, 'datasets', dataset_id):
        dbutil.restricted_error()

    # Query the project id, dataset type, and sequence number.

    c = cnx.cursor()
    q = 'SELECT id,project_id,type,seqnum FROM datasets WHERE id=%s'
    c.execute(q, (dataset_id, ))
    rows = c.fetchall()
    if len(rows) == 0:
        raise IOError('Unable to fetch dataset id %d' % dataset_id)
    row = rows[0]
    project_id = row[1]
    dataset_type = row[2]
    seqnum = row[3]

    # Query the succeeding sequence number

    q = 'SELECT id, project_id, type, seqnum FROM datasets WHERE project_id=%s AND type=%s AND seqnum>%s ORDER BY seqnum'

    c.execute(q, (project_id, dataset_type, seqnum))
    rows = c.fetchall()
    if len(rows) > 0:
        row = rows[0]
        next_dataset_id = row[0]
        next_seqnum = row[3]

        # Swap sequence numbers.

        q = 'UPDATE datasets SET seqnum=%s WHERE id=%s'
        c.execute(q, (next_seqnum, dataset_id))
        q = 'UPDATE datasets SET seqnum=%s WHERE id=%s'
        c.execute(q, (seqnum, next_dataset_id))
        cnx.commit()

    # Generate redirect html document header to invoke the dataset editor for
    # the newly created document.

    url = ''
    url = '%s/edit_datasets.py?id=%d&%s' % \
          (dbconfig.base_url, project_id, dbargs.convert_args(qdict))
    print 'Content-type: text/html'
    print 'Status: 303 See Other'
    print 'Location: %s' % url
    print
    print '<!DOCTYPE html>'
    print '<html>'
    print '<body>'
    print 'If page does not automatically reload click this <a href=%s>link</a>' % url
    print '</body>'
    print '</html>'
Exemplo n.º 2
0
def main(argdict):

    # Open database connection.

    qdict = dbargs.extract_qdict(argdict)
    cnx = dbconfig.connect(readonly=False, devel=qdict['dev'])

    # Get table name and id (primary key).
    # Both of these values should exist and be non-null, or else operation will fail.

    table = ''
    id = 0
    saveurl = ''
    update_db = False
    if 'table' in argdict:
        table = argdict['table']
    if 'id' in argdict:
        id = int(argdict['id'])
    if 'saveurl' in argdict and 'submit' in argdict and \
       (argdict['submit'] == 'Save' or argdict['submit'] == 'Back'):
        saveurl = argdict['saveurl']
    if 'submit' in argdict and (argdict['submit'] == 'Save'
                                or argdict['submit'] == 'Update'):
        update_db = True

    # Check access restrictions.

    if not dbconfig.restricted_access_allowed():

        # Check whether this table/id is restricted access.

        if dbutil.restricted_access(cnx, table, id):
            dbutil.restricted_error()

        # If the projects table is being updated to a status which is anything other
        # than '' or 'Requested', it is an error.

        if table == 'projects' and 'status' in argdict and \
           argdict['status'] != '' and argdict['status'] != 'Requested':
            dbutil.restricted_error()

    if update_db:

        # Update database.

        c = cnx.cursor()
        q = 'UPDATE %s SET ' % table
        params = []

        # Loop over columns of this table.

        cols = databaseDict[table]
        first = True
        for n in range(len(cols)):
            coltup = cols[n]
            colname = coltup[0]
            coltype = coltup[2]
            colarray = coltup[3]
            coldesc = coltup[4]

            if colname != '' and colname != 'id':

                # Did form supply a value for this column?

                if colname in argdict:

                    # Maybe add separator.

                    if first:
                        first = False
                    else:
                        q += ', '

                    # Add value.

                    if colarray == 0:

                        # Scalar column.

                        if coltype[0:3] == 'INT':
                            q += '%s=%%s' % colname
                            params.append(int(argdict[colname]))
                        elif coltype[0:6] == 'DOUBLE':
                            q += '%s=%%s' % colname
                            params.append(float(argdict[colname]))
                        elif coltype[0:7] == 'VARCHAR':
                            q += '%s=%%s' % colname
                            params.append(argdict[colname])

                    else:

                        # String array.

                        strs = argdict[colname].split()
                        strid = dbutil.update_strings(cnx, strs)
                        q += '%s=%%s' % colname
                        params.append(strid)

        # Add where clause.

        q += ' WHERE id=%s'
        params.append(id)

        # Run query.

        c.execute(q, params)

        # Special handling for table groups.

        if table == 'groups':

            # Loop over arguments to find projects to add to this group.

            project_ids = []
            for k in argdict:
                if k.startswith('add'):
                    project_ids.append(int(argdict[k]))

            # Loop over projects.

            for project_id in project_ids:
                q = 'INSERT INTO group_project SET group_id=%s,project_id=%s'
                c.execute(q, (id, project_id))

            # Loop over arguments to find projects to remove from this group.

            project_ids = []
            for k in argdict:
                if k.startswith('remove'):
                    project_ids.append(int(argdict[k]))

            # Loop over projects.

            for project_id in project_ids:
                q = 'DELETE FROM group_project WHERE group_id=%s AND project_id=%s'
                c.execute(q, (id, project_id))

        # Commit updates.

        cnx.commit()

    # Calculate redirect url.

    url = '%s/query_projects.py' % dbconfig.base_url
    if saveurl != '':
        url = saveurl
    elif 'HTTP_REFERER' in os.environ:
        url = os.environ['HTTP_REFERER']

    # Generate html redirect document.

    print 'Content-type: text/html'
    print 'Status: 303 See Other'
    print 'Location: %s' % url
    print
    print '<!DOCTYPE html>'
    print '<html>'
    print '<body>'
    if table != '':
        print 'Database table "%s" updated.' % table
    else:
        print 'Database not updated.'
    print '<br><br>'
    print 'If page does not automatically reload click this <a href=%s>link</a>' % url
    print '</body>'
    print '</html>'
Exemplo n.º 3
0
def main(project_id, update_id, qdict):

    # Open database connection.

    cnx = dbconfig.connect(readonly=False, devel=qdict['dev'])

    # See if we want to update any dataset.

    if update_id != 0:

        update_ok = False

        # Query dataset name.

        c = cnx.cursor()
        q = 'SELECT name FROM datasets WHERE id=%s'
        c.execute(q, (update_id, ))
        rows = c.fetchall()
        if len(rows) == 0:
            raise IOError('Unable to fetch dataset id %d' % update_id)
        dataset_name = rows[0][0]

        # Query experiment name.

        q = 'SELECT experiment FROM projects WHERE id=%s'
        c.execute(q, (project_id, ))
        rows = c.fetchall()
        if len(rows) == 0:
            raise IOError('Unable to fetch project id %d' % project_id)
        row = rows[0]
        experiment = row[0]

        # Query event count form sam.

        files = 0
        events = 0
        parent_files = 0
        parent_events = 0
        update_ok = True
        r = dbutil.get_stats(dbconfig.samweb_url[experiment], dataset_name)
        if r == None:
            update_ok = False
        else:
            files = r[0]
            events = r[1]
            r = dbutil.get_parent_stats(dbconfig.samweb_url[experiment],
                                        dataset_name)
            if r == None:
                update_ok = False
            else:
                parent_files = r[0]
                parent_events = r[1]

        # Do update.

        if update_ok:

            # Check access.

            if not dbconfig.restricted_access_allowed(
            ) and dbutil.restricted_access(cnx, 'datasets', dataset_id):
                dbutil.restricted_error()

            # Update database.

            q = 'UPDATE datasets SET events=%s, files=%s, parent_events=%s, parent_files=%s WHERE id=%s'
            c.execute(q,
                      (events, files, parent_events, parent_files, update_id))
            cnx.commit()

        else:

            url = '%s/edit_datasets.py?id=%d&%s' % \
                  (dbconfig.base_url, project_id, dbargs.convert_args(qdict))
            print 'Content-type: text/html'
            print
            print '<!DOCTYPE html>'
            print '<html>'
            print '<head>'
            print '<title>Update Datasets</title>'
            print '</head>'
            print '<body>'
            print 'Failed to update dataset statistics.'
            print '<br><br>'
            print '<a href=%s>Return to dataset list</a>.' % url
            print '</body>'
            print '</html>'
            return

    # Generate html document header.

    print 'Content-type: text/html'
    print
    print '<!DOCTYPE html>'
    print '<html>'
    print '<head>'
    print '<title>Project Datasets Editor</title>'
    print '</head>'
    print '<body>'
    print '<a href=%s/query_projects.py?%s>Project list</a><br>' % \
        (dbconfig.base_url, dbargs.convert_args(qdict))

    # Generate main parg of html document.

    datasets_form(cnx, project_id, qdict)

    # Generate html document trailer.

    print '</body>'
    print '</html>'