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>'
def request_form(cnx, qdict, argdict): # Construct global disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed() and dbutil.restricted_access( cnx, 'projects', id): disabled = 'disabled' #for key in qdict: # print '%s = %s' % (key, qdict[key]) # print '<br>' #print '<br>' #for key in argdict: # print '%s = %s' % (key, argdict[key]) # print '<br>' #print '<br>' # Generate form. print '<h2>Sample Request</h2>' print '<form action="%s/request.py" method="post" target="_self">' % dbconfig.rel_url print '<input type="submit" name="reload" value="Reload">' print '<br>' print '<br>' # Put input fields in a table. print '<table border=1 style="border-collapse:collapse">' # Add hidden qdict input fields. for key in qdict: print '<input type="hidden" name="%s" value="%s">' % ( dbutil.convert_str(key), dbutil.convert_str(qdict[key])) # Add readonly requester field. user = '' if 'SSO_USERID' in os.environ: user = os.environ['SSO_USERID'] print '<tr>' print '<td><label for="user">Requester: </label></td>' print '<td><input type="text" id="user" name="user" value="%s" readonly></td>' % user print '</tr>' # Sample name field. name = '' if 'name' in argdict: name = argdict['name'] print '<tr>' print '<td><label for="name">Sample name: </label></td>' print '<td><input type="text" id="name" name="name" size=80 value="%s"></td>' % name print '</tr>' # Sample description field. desc = '' if 'description' in argdict: desc = argdict['description'] print '<tr>' print '<td><label for="description">Sample description: </label></td>' print '<td><textarea id="description" name="description" rows=2 cols=80>' print desc, print '</textarea></td>' print '</tr>' # Version field. version = '' if 'version' in argdict: version = argdict['version'] print '<tr>' print '<td><label for="version">Release version: </label></td>' print '<td><input type="text" id="version" name="version" size=80 value="%s"></td>' % version print '</tr>' # Input dataset field. input = '' if 'input' in argdict: input = argdict['input'] print '<tr>' print '<td><label for="input">Input dataset: </label></td>' print '<td><input type="text" id="input" name="input" size=80 value="%s"></td>' % input print '</tr>' # Experiment field. selexp = '' if 'exp' in argdict: selexp = argdict['exp'] print '<tr>' print '<td><label for="exp">Experiment: </label></td>' print '<td><select id="exp" name="exp" size=0>' for experiment in dbconfig.pulldowns['experiment']: sel = '' if experiment == selexp: sel = 'selected' print '<option value="%s" %s>%s</option>' % (experiment, sel, experiment) print '</select></td>' print '</tr>' # File type field. selftype = '' if 'ftype' in argdict: selftype = argdict['ftype'] print '<tr>' print '<td><label for="ftype">File type: </label></td>' print '<td><select id="ftype" name="ftype" size=0>' for file_type in dbconfig.pulldowns['file_type']: sel = '' if file_type == selftype: sel = 'selected' print '<option value="%s" %s>%s</option>' % (file_type, sel, file_type) print '</select></td>' print '</tr>' # Role field. selrole = '' if 'role' in argdict: selrole = argdict['role'] print '<tr>' print '<td><label for="role">Role: </label></td>' print '<td><select id="role" name="role" size=0>' for role in dbconfig.pulldowns['role']: sel = '' if role == selrole: sel = 'selected' print '<option value="%s" %s>%s</option>' % (role, sel, role) print '</select></td>' print '</tr>' # Campaign field. selcamp = '' if 'camp' in argdict: selcamp = argdict['camp'] print '<tr>' print '<td><label for="camp">Campaign: </label></td>' print '<td><select id="camp" name="camp" size=0>' for campaign in dbconfig.pulldowns['campaign']: sel = '' if campaign == selcamp: sel = 'selected' print '<option value="%s" %s>%s</option>' % (campaign, sel, campaign) print '</select></td>' print '</tr>' # Working group field. selgrp = '' if 'wgroup' in argdict: selgrp = argdict['wgroup'] print '<tr>' print '<td><label for="wgroup">Working group: </label></td>' print '<td><select id="wgroup" name="wgroup" size=0>' for group in dbconfig.pulldowns['physics_group']: sel = '' if group == selgrp: sel = 'selected' print '<option value="%s" %s>%s</option>' % (group, sel, group) print '</select></td>' print '</tr>' # Target date field. date = '' if 'date' in argdict: date = argdict['date'] print '<tr>' print '<td><label for="date">Target date: </label></td>' print '<td><input type="date" id="date" name="date" value="%s"></td>' % date print '</tr>' # Priority field. selpri = 'low' if 'priority' in argdict: selpri = argdict['priority'] print '<tr>' print '<td><label for="priority">Priority: </label></td>' print '<td><select id="priority" name="priority" size=0>' for priority in ('low', 'medium', 'high'): sel = '' if priority == selpri: sel = 'selected' print '<option value="%s" %s>%s</option>' % (priority, sel, priority) print '</select></td>' print '</tr>' # Number of events field. num_events = 0 if 'num_events' in argdict: num_events = int(argdict['num_events']) print '<tr>' print '<td><label for="num_events">Number of events: </label></td>' print '<td><input type="number" id="num_events" name="num_events" value=%d></td>' % num_events print '</tr>' # Number of jobs. num_jobs = 0 if 'num_jobs' in argdict: num_jobs = int(argdict['num_jobs']) print '<tr>' print '<td><label for="num_jobs">Number of jobs: </label></td>' print '<td><input type="number" id="num_jobs" name="num_jobs" value=%d></td>' % num_jobs print '</tr>' # Maximum files per job. max_files = 0 if 'max_files' in argdict: max_files = int(argdict['max_files']) print '<tr>' print '<td><label for="max_files">Maximum files per job: </label></td>' print '<td><input type="number" id="max_files" name="max_files" value=%d></td>' % max_files print '</tr>' # Schema field. selschema = '' if 'schema' in argdict: selschema = argdict['schema'] print '<tr>' print '<td><label for="schema">Schema: </label></td>' print '<td><select id="schema" name="schema" size=0>' for schema in dbconfig.pulldowns['schema']: sel = '' if schema == selschema: sel = 'selected' print '<option value="%s" %s>%s</option>' % (schema, sel, schema) print '</select></td>' print '</tr>' # Valication check 1 - Grid print '<tr>' print '<td><label for="validation">Validation: </label></td>' print '<td>' checked = '' if 'gridtest' in argdict: checked = 'checked' print '<input type="checkbox" id="gridtest" name="gridtest" value="yes" %s>' % checked print '<label for="gridtest"> Work flow has been tested on grid</label>' print '<br>' checked = '' if 'samtest' in argdict: checked = 'checked' print '<input type="checkbox" id="samtest" name="samtest" value="yes" %s>' % checked print '<label for="samtest"> Output files have proper sam metadata</label>' print '<br>' checked = '' if 'nametest' in argdict: checked = 'checked' print '<input type="checkbox" id="nametest" name="nametest" value="yes" %s>' % checked print '<label for="nametest"> Artroot output files have unique names</label>' print '</td>' print '</tr>' # Sample instructions field. inst = '' if 'instructions' in argdict: inst = argdict['instructions'] print '<tr>' print '<td><label for="instructions">Special instructions: </label></td>' print '<td><textarea id="instructions" name="instructions" rows=2 cols=80>' print inst, print '</textarea></td>' print '</tr>' # End of main table. print '</table>' # Generate an ordered list of existing fcls. # Each element of the list will be a 5-tuple (stage, fcl, memory, time, size). empty_tuple = ('', '', 0, 0, 0) fcl_keys = [] for key in argdict: if key.startswith('fcl_'): fcl_keys.append(key) fcllist = [] for fcl_key in sorted(fcl_keys): stage_key = 'stage_%s' % fcl_key[4:] if stage_key in argdict: stage = argdict[stage_key] fcl = argdict[fcl_key] mem_key = 'memory_%d' % len(fcllist) memory = 0 if mem_key in argdict: memory = int(argdict[mem_key]) time_key = 'time_%d' % len(fcllist) time_event = 0 if time_key in argdict: time_event = int(argdict[time_key]) size_key = 'size_%d' % len(fcllist) size_event = 0 if size_key in argdict: size_event = float(argdict[size_key]) fcl_tuple = (stage, fcl, memory, time_event, size_event) # Check for insert fcl. insert_key = 'insert_%d' % len(fcllist) if insert_key in argdict: fcllist.append(empty_tuple) # Check for clone fcl. clone_key = 'clone_%d' % len(fcllist) if clone_key in argdict: fcllist.append(fcl_tuple) # Check for delete fcl. delete_key = 'delete_%d' % len(fcllist) if delete_key in argdict: # Only process this delete action once. del argdict[delete_key] continue # Check move up/down. if len(fcllist) > 0: up_key = 'up_%d' % len(fcllist) down_key = 'down_%d' % (len(fcllist) - 1) if up_key in argdict or down_key in argdict: # Insert this (stage, key) in the next to last position. fcllist.insert(len(fcllist) - 1, fcl_tuple) continue # Add this (stage, fcl). fcllist.append(fcl_tuple) # If the number of fcl files is zero, or the last stage is not blank, # add a new blank stage at the end. if len(fcllist) == 0 or fcllist[-1][0] != '': fcllist.append(empty_tuple) # Generate FCL table. print '<h3>FCL Files</h3>' print '<table border=1 style="border-collapse:collapse">' print '<tr>' print '<th>Stage</th>' print '<th>FCL File</th>' print '<th> Memory (MB) </th>' print '<th> Time/ev (sec) </th>' print '<th> Size/ev (MB) </th>' print '</tr>' # Add editable fields for existing fcl files. n = 0 for fcltuple in fcllist: stage = fcltuple[0] fcl = fcltuple[1] memory = fcltuple[2] time_event = fcltuple[3] size_event = fcltuple[4] print '<tr>' print '<td><input type="text" id="stage_%d" name="stage_%d" size-20 value="%s"></td>' % ( n, n, stage) print '<td><input type="text" id="fcl_%d" name="fcl_%d" size=80 value="%s"></td>' % ( n, n, fcl) print '<td><input type="number" id="memory_%d" name="memory_%d" size=12 value=%d></td>' % ( n, n, memory) print '<td><input type="number" id="time_%d" name="time_%d" size=12 value=%d></td>' % ( n, n, time_event) print '<td><input type="number" id="size_%d" name="size_%d" size=12 step=0.01 value=%8.2f></td>' % ( n, n, size_event) # Add insert button. print '<td>' print '<div title="Insert FCL">' print '<input type="submit" id="insert_%d" name="insert_%d" value="➕">' % ( n, n) print '</div>' print '</td>' # Add clone button. print '<td>' print '<div title="Clone FCL">' print '<input type="submit" id="clone_%d" name="clone_%d" value="⎘">' % ( n, n) print '</div>' print '</td>' # Add delete button. print '<td>' print '<div title="Delete FCL">' print '<input type="submit" id="delete_%d" name="delete_%d" value="🗑">' % ( n, n) print '</div>' print '</td>' # Add Up button. print '<td>' print '<div title="Move up">' print '<input type="submit" id="up_%d" name="up_%d" value="▲">' % ( n, n) print '</div>' print '</td>' # Add Down button. print '<td>' print '<div title="Move down">' print '<input type="submit" id="down_%d" name="down_%d" value="▼">' % ( n, n) print '</div>' print '</td>' print '</tr>' n += 1 # End of FCL table. print '</table>' # Add submit and cancel buttons. print '<br>' print '<input type="submit" name="submit" value="Submit" %s>' % disabled print '<input type="submit" name="view" value="View" %s>' % disabled print '<input type="submit" name="clear" value="Clear">' print '<input type="submit" name="cancel" value="Cancel">' # End of form. print '</form>' # Done. return
def override_form(cnx, id, qdict): # Construct global disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed() and dbutil.restricted_access( cnx, 'overrides', id): disabled = 'disabled' # Query override from database. c = cnx.cursor() q = 'SELECT stage_id,name,override_type,value FROM overrides WHERE id=%s' c.execute(q, (id, )) rows = c.fetchall() if len(rows) == 0: raise IOError('Unable to fetch override id %d' % id) row = rows[0] stage_id = row[0] name = row[1] override_type = row[2] value = row[3] # Query stage name and project id. q = 'SELECT id, name, project_id FROM stages WHERE id=%s' c.execute(q, (stage_id, )) stage_rows = c.fetchall() if len(stage_rows) == 0: raise IOError('Unable to fetch stage id %d' % stage_id) stage_row = stage_rows[0] stage_name = stage_row[1] project_id = stage_row[2] project_name = dbutil.get_project_name(cnx, project_id) # Generate form. print '<h2>Project %s</h2>' % project_name print '<h2>Stage %s</h2>' % stage_name print '<h2>Override %s</h2>' % name print '<form action="%s/dbhandler.py" method="post">' % dbconfig.rel_url # Add hidden input field to store table name. print '<input type="hidden" id="table" name="table" value="overrides">' # Add hidden input field to store save url (parent of this page). print '<input type="hidden" id="saveurl" name="saveurl" value="%s/edit_stage.py?id=%d&%s">' % \ (dbconfig.base_url, stage_id, dbargs.convert_args(qdict)) # Add hidden qdict input fields. for key in qdict: print '<input type="hidden" name="%s" value="%s">' % ( dbutil.convert_str(key), dbutil.convert_str(qdict[key])) # Add form fields in a table. print '<table border=1 style="border-collapse:collapse">' print '<tr>' print '<td>' print '<label for="id">ID:</label>' print '</td>' print '<td>' print '<input type="number" id="id" name="id" value="%d" readonly>' % id print '</td>' print '</tr>' print '<tr>' print '<td>' print '<label for="stage_id">Stage ID:</label>' print '</td>' print '<td>' print '<input type="number" id="stage_id" name="stage_id" value="%d" readonly>' % stage_id print '</td>' print '</tr>' print '<tr>' print '<td>' print '<label for="name">Name:</label>' print '</td>' print '<td>' print '<input type="text" id="name" name="name" value="%s">' % name print '</td>' print '</tr>' print '<tr>' print '<td>' print '<label for="override_type">Type:</label>' print '</td>' print '<td>' print '<select id="override_type" name="override_type" size=0>' pulldown_list = pulldowns['override_type'] for override_value in pulldown_list: sel = '' if override_value == override_type: sel = 'selected' print '<option value="%s" %s>%s</option>' % (override_value, sel, override_value) print '</select>' print '</td>' print '</tr>' print '<tr>' print '<td>' print '<label for="value">Value:</label>' print '</td>' print '<td>' print '<input type="text" id="value" name="value" value="%s">' % value print '</td>' print '</tr>' # Finish table. print '</table>' # Add "Save" and "Back" buttons. print '<input type="submit" name="submit" value="Save" %s>' % disabled print '<input type="submit" name="submit" value="Update" %s>' % disabled print '<input type="submit" name="submit" value="Back">' print '</form>'
def main(qdict): # Extract arguments. results_per_page = qdict['results_per_page'] current_page = qdict['page'] pattern = qdict['pattern'] devel = qdict['dev'] sort = qdict['sort'] # Open database connection and query groups. cnx = dbconfig.connect(readonly=True, devel=qdict['dev']) # Get list of project groups. groups = list_groups(cnx, pattern, sort) max_page = (len(groups) + results_per_page - 1) / results_per_page # Generate html document header. print 'Content-type: text/html' print print '<!DOCTYPE html>' print '<html>' print '<head>' print '<title>Project Groups</title>' print '</head>' print '<body>' # Add styles. print '<style>' print '.small-btn {' print ' padding:0 5px;' print ' text-decoration:none;' print ' border:none;' print ' background-color:white;' print '}' print '</style>' # Add link to project full list. print '<a href=%s/query_projects.py?%s>Project list</a><br>' % \ (dbconfig.base_url, dbargs.convert_args(qdict, 'gid', 0)) # Generate main part of html document. print '<h1>Project Groups</h1>' # Add button to create new group. print '<form action="%s/add_group.py?%s" method="post" target="_self">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict)) print '<label for="submit">Generate a new empty project group: </label>' print '<input type="submit" id="submit" value="New Group">' print '</form>' print '<p>' # Generate search panel form. search_panel(results_per_page, pattern, devel) # Display number of results. print '<p>%d project groups found</p>' % len(groups) # Generate upper navigation panel. page_links(qdict, max_page) # Group table. print '<table border=1 style="border-collapse:collapse">' print '<tr>' print '<th nowrap>' print ' Group ID ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_groups.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'id_u')) if sort == 'id_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_groups.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'id_d')) if sort == 'id_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' print '<th nowrap>' print ' Group Name ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_groups.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'name_u')) if sort == 'name_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_groups.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'name_d')) if sort == 'name_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' print '</tr>' # Loop over groups. for i in range((current_page - 1) * results_per_page, min(current_page * results_per_page, len(groups))): group = groups[i] print '<tr>' id = group[0] name = group[1] print '<td align="center">%d</td>' % id # Construct disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed(): disabled = 'disabled' # Add group name with link to projects page. url = '%s/query_projects.py?%s' % \ (dbconfig.base_url, dbargs.convert_args(qdict, 'gid', id)) print '<td> <a href="%s">%s</a> </td>' % (url, name) # Add Edit button/column print '<td>' print '<form target="_self" action="%s/edit_group.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Edit group">' print '<input type="submit" value="✎">' print '</div>' print '</form>' print '</td>' # Add Clone button/column print '<td>' print '<form target="_self" action="%s/clone_group.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Clone group">' print '<input type="submit" value="⎘">' print '</div>' print '</form>' print '</td>' # Add Delete button/column print '<td>' print '<form action="%s/delete_group.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Delete group">' print '<input type="submit" value="🗑" %s>' % disabled print '</div>' print '</form>' print '</td>' print '</tr>' print '</table>' # Add lower navigation panel. page_links(qdict, max_page) # Generate html document trailer. print '</body>' print '</html>'
def project_form(cnx, id, qdict): # Construct global disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed() and dbutil.restricted_access( cnx, 'projects', id): disabled = 'disabled' # Construct disable option that applies only to status options. option_disabled = '' if not dbconfig.restricted_access_allowed(): option_disabled = 'disabled' # Get project name. name = dbutil.get_project_name(cnx, id) # Get experiment. experiment = dbutil.get_project_experiment(cnx, id) # Generate form. print '<h2>Project %s</h2>' % name # Add button to insert another stage. print '<h2>Stages</h2>' print '<form action="%s/add_stage.py?id=%d&%s" method="post" target="_self">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<input type="submit" value="Add Stage" %s>' % disabled print '</form>' print '<br>' # Query stage ids belonging to this project. c = cnx.cursor() q = 'SELECT id, name FROM stages WHERE project_id=%s ORDER BY seqnum' c.execute(q, (id, )) rows = c.fetchall() if len(rows) > 0: # Add links to edit project stages. print '<table border=1 style="border-collapse:collapse">' print '<tr>' print '<th> Stage ID </th>' print '<th> Stage Name </th>' print '</tr>' for row in rows: print '<tr>' stage_id = row[0] stage_name = row[1] print '<td align="center">%d</td>' % stage_id print '<td> <a target="_self" href="%s/edit_stage.py?id=%d&%s">%s</a> </td>' % \ (dbconfig.base_url, stage_id, dbargs.convert_args(qdict), stage_name) # Add Edit button/column print '<td>' print '<form target="_self" action="%s/edit_stage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, stage_id, dbargs.convert_args(qdict)) print '<div title="Edit stage">' print '<input type="submit" value="✎">' print '</div>' print '</form>' print '</td>' # Add Clone button/column print '<td>' print '<form target="_self" action="%s/clone_stage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, stage_id, dbargs.convert_args(qdict)) print '<div title="Clone stage">' print '<input type="submit" value="⎘" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Delete button/column print '<td>' print '<form action="%s/delete_stage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, stage_id, dbargs.convert_args(qdict)) print '<div title="Delete stage">' print '<input type="submit" value="🗑" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Up button/column print '<td>' print '<form action="%s/up_stage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, stage_id, dbargs.convert_args(qdict)) print '<div title="Move up">' print '<input type="submit" value="▲" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Down button/column print '<td>' print '<form action="%s/down_stage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, stage_id, dbargs.convert_args(qdict)) print '<div title="Move down">' print '<input type="submit" value="▼" %s>' % disabled print '</div>' print '</form>' print '</td>' # Finish row. print '</tr>' # Finish table. print '</table>' print '<h2>Project Data</h2>' # Query full project from database. q = 'SELECT %s FROM projects WHERE id=%%s' % dbutil.columns('projects') c.execute(q, (id, )) rows = c.fetchall() if len(rows) == 0: raise IOError('Unable to fetch project id %d' % id) row = rows[0] print '<form action="%s/dbhandler.py" method="post">' % dbconfig.rel_url # Add hidden input field to store table name. print '<input type="hidden" id="table" name="table" value="projects">' # Add hidden input field to store save url (parent of this page). print '<input type="hidden" id="saveurl" name="saveurl" value="%s/query_projects.py?%s">' % \ (dbconfig.base_url, dbargs.convert_args(qdict)) # Add hidden qdict input fields. for key in qdict: print '<input type="hidden" name="%s" value="%s">' % ( dbutil.convert_str(key), dbutil.convert_str(qdict[key])) # Loop over fields of this project. # Put fields in a table. print '<table border=1 style="border-collapse:collapse">' cols = databaseDict['projects'] for n in range(len(cols)): coltup = cols[n] colname = coltup[0] coltype = coltup[2] colarray = coltup[3] coldesc = coltup[4] if colname != '': # Set readonly attribute readonly = '' if colname == 'id': readonly = 'readonly' elif disabled != '': readonly = 'readonly' print '<tr>' print '<td>' print '<label for="%s">%s: </label>' % (colname, coldesc) print '</td>' print '<td>' if colarray == 0: # Scalar column. if coltype[0:3] == 'INT': print '<input type="number" id="%s" name="%s" size=10 value="%d" %s>' % \ (colname, colname, row[n], readonly) elif coltype[0:6] == 'DOUBLE': print '<input type="text" id="%s" name="%s" size=10 value="%8.6f" %s>' % \ (colname, colname, row[n], readonly) elif coltype[0:7] == 'VARCHAR': if colname in pulldowns: print '<select id="%s" name="%s" size=0 %s>' % ( colname, colname, disabled) pulldown_list = pulldowns[colname] if type(pulldown_list) == type({}): if experiment in pulldown_list: pulldown_list = pulldowns[colname][experiment] else: pulldown_list = [''] for value in pulldown_list: sel = '' if value == row[n]: sel = 'selected' if value == '' or value == 'Requested': print '<option value="%s" %s>%s</option>' % ( value, sel, value) else: print '<option value="%s" %s %s>%s</option>' % ( value, sel, option_disabled, value) print '</select>' else: print '<input type="text" id="%s" name="%s" size=100 value="%s" %s>' % \ (colname, colname, row[n], readonly) else: # Array columns. # Display using multiline <textarea>. strs = dbutil.get_strings(cnx, row[n]) print '<textarea id="%s" name="%s" rows=%d cols=80 %s>' % \ (colname, colname, max(len(strs),1), readonly) print '\n'.join(strs) print '</textarea>' print '</td>' print '</tr>' # Finish table. print '</table>' # Add "Save" and "Back" buttons. print '<input type="submit" name="submit" value="Save" %s>' % disabled print '<input type="submit" name="submit" value="Update" %s>' % disabled print '<input type="submit" name="submit" value="Back">' print '</form>'
def substage_form(cnx, id, qdict): # Construct global disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed() and dbutil.restricted_access( cnx, 'substages', id): disabled = 'disabled' # Query substage from database. c = cnx.cursor() q = 'SELECT %s FROM substages WHERE id=%%s' % dbutil.columns('substages') c.execute(q, (id, )) rows = c.fetchall() if len(rows) == 0: raise IOError('Unable to fetch substage id %d' % id) row = rows[0] fclname = row[1] stage_id = row[2] # Query stage name and project id. q = 'SELECT id, name, project_id FROM stages WHERE id=%s' c.execute(q, (stage_id, )) stage_rows = c.fetchall() if len(stage_rows) == 0: raise IOError('Unable to fetch stage id %d' % stage_id) stage_row = stage_rows[0] stage_name = stage_row[1] project_id = stage_row[2] project_name = dbutil.get_project_name(cnx, project_id) # Generate form. print '<h2>Project %s</h2>' % project_name print '<h2>Stage %s</h2>' % stage_name print '<h2>FCL %s</h2>' % fclname print '<form action="%s/dbhandler.py" method="post">' % dbconfig.rel_url # Add hidden input field to store table name. print '<input type="hidden" id="table" name="table" value="substages">' # Add hidden input field to store save url (parent of this page). print '<input type="hidden" id="saveurl" name="saveurl" value="%s/edit_stage.py?id=%d&%s">' % \ (dbconfig.base_url, stage_id, dbargs.convert_args(qdict)) # Add hidden qdict input fields. for key in qdict: print '<input type="hidden" name="%s" value="%s">' % ( dbutil.convert_str(key), dbutil.convert_str(qdict[key])) # Loop over fields of this stage. # Put fields in a table. print '<table border=1 style="border-collapse:collapse">' cols = databaseDict['substages'] for n in range(len(cols)): coltup = cols[n] colname = coltup[0] coltype = coltup[2] colarray = coltup[3] coldesc = coltup[4] if colname != '': # Set readonly attribute readonly = '' if colname == 'id' or colname == 'stage_id': readonly = 'readonly' elif disabled != '': readonly = 'readonly' print '<tr>' print '<td>' print '<label for="%s">%s: </label>' % (colname, coldesc) print '</td>' print '<td>' if colarray == 0: # Scalar column. if coltype[0:3] == 'INT': print '<input type="number" id="%s" name="%s" size=10 value="%d" %s>' % \ (colname, colname, row[n], readonly) elif coltype[0:6] == 'DOUBLE': print '<input type="text" id="%s" name="%s" size=100 value="%8.6f" %s>' % \ (colname, colname, row[n], readonly) elif coltype[0:7] == 'VARCHAR': print '<input type="text" id="%s" name="%s" size=100 value="%s" %s>' % \ (colname, colname, row[n], readonly) else: # Array columns. # Display using multiline <textarea>. strs = dbutil.get_strings(cnx, row[n]) print '<textarea id="%s" name="%s" rows=%d cols=80 %s>' % \ (colname, colname, max(len(strs), 1), readonly) print '\n'.join(strs) print '</textarea>' print '</td>' print '</tr>' # Finish table. print '</table>' # Add "Save" and "Back" buttons. print '<input type="submit" name="submit" value="Save" %s>' % disabled print '<input type="submit" name="submit" value="Update" %s>' % disabled print '<input type="submit" name="submit" value="Back">' print '</form>'
def main(qdict): # Extract arguments. results_per_page = qdict['results_per_page'] current_page = qdict['page'] pattern = qdict['pattern'] group = qdict['group'] status = qdict['status'] devel = qdict['dev'] gid = qdict['gid'] file_type = qdict['file_type'] campaign = qdict['campaign'] sort = qdict['sort'] experiment = qdict['experiment'] # Open database connection and query projects. cnx = dbconfig.connect(readonly = True, devel = qdict['dev']) # Get list of projects. prjs = list_projects(cnx, pattern, group, status, gid, experiment, file_type, campaign, sort) max_page = (len(prjs) + results_per_page - 1) / results_per_page # Generate html document header. print 'Content-type: text/html' print print '<!DOCTYPE html>' print '<html>' print '<head>' print '<title>Projects</title>' print '</head>' print '<body>' # Add styles. print '<style>' print '.small-btn {' print ' padding:0 5px;' print ' text-decoration:none;' print ' border:none;' print ' background-color:white;' print '}' print '</style>' # Add link to project group list. print '<a href=%s/query_groups.py?%s>Group list</a><br>' % \ (dbconfig.base_url, dbargs.convert_args(qdict)) # Generate main part of html document. print '<h1>Projects</h1>' # Add button to request a new sample. print '<form action="%s/request.py?%s" method="post" target="_self">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict)) print '<label for="submit">Request a sample: </label>' print '<input type="submit" id="submit" value="Request">' print '</form>' print '<p>' # Add button to create new project. print '<form action="%s/add_project.py?%s" method="post" target="_self">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict)) print '<label for="submit">Generate a new empty project: </label>' print '<input type="submit" id="submit" value="New Project">' print '</form>' print '<p>' # Add button to import a project from local xml file. print '<form action="%s/import_project.py?%s" method="post" target="_self">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict)) print '<label for="submit">Import project from local XML file: </label>' print '<input type="submit" id="submit" value="Import Project">' print '</form>' print '<p>' # Generate search panel form. search_panel(results_per_page, pattern, group, status, gid, experiment, file_type, campaign, devel) # Display number of results. print '<p>%d projects found</p>' % len(prjs) # Generate upper navigation panel. page_links(qdict, max_page) # Project table. print '<table border=1 style="border-collapse:collapse">' print '<tr>' # Project ID column. print '<th nowrap>' print ' Project ID ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'id_u')) if sort == 'id_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'id_d')) if sort == 'id_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' # Project name column. print '<th nowrap>' print ' Project Name (Datasets) ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'name_u')) if sort == 'name_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'name_d')) if sort == 'name_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' # Experiment column. print '<th nowrap>' print ' Experiment ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'experiment_u')) if sort == 'experiment_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'experiment_d')) if sort == 'experiment_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' # File type column. print '<th nowrap>' print ' File Type ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'file_type_u')) if sort == 'file_type_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'file_type_d')) if sort == 'file_type_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' # Campaign column. print '<th nowrap>' print ' Campaign ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'campaign_u')) if sort == 'campaign_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'campaign_d')) if sort == 'campaign_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' # Physics group column. print '<th nowrap>' print ' Physics ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'group_u')) if sort == 'group_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'group_d')) if sort == 'group_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' # Status column. print '<th nowrap>' print ' Status ' # Add sort buttons. print '<div style="display:inline-block">' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'status_u')) if sort == 'status_u': print '<input class="small-btn" type="submit" value="▲">' else: print '<input class="small-btn" type="submit" value="△">' print '</form>' print '<form action="%s/query_projects.py?%s" method="post">' % \ (dbconfig.rel_url, dbargs.convert_args(qdict, 'sort', 'status_d')) if sort == 'status_d': print '<input class="small-btn" type="submit" value="▼">' else: print '<input class="small-btn" type="submit" value="▽">' print '</form>' print '</div>' print '</th>' print '</tr>' # Loop over projects. for i in range((current_page-1) * results_per_page, min(current_page * results_per_page, len(prjs))): prj = prjs[i] print '<tr>' id = prj[0] name = prj[1] file_type = prj[2] experiment = prj[3] campaign = prj[4] physics_group = prj[5] status = prj[6] color_style = '' if status in colors: color_style = 'style="background-color: %s"' % colors[status] print '<td align="center" %s>%d</td>' % (color_style, id) # Construct disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed() and dbutil.restricted_access(cnx, 'projects', id): disabled = 'disabled' # Add link to datasets page with project name. print '<td %s> <a target=_self href=%s/edit_datasets.py?id=%d&%s>%s</a> </td>' % \ (color_style, dbconfig.base_url, id, dbargs.convert_args(qdict), name) # Add middle columns print '<td align="center" %s> %s </td>' % (color_style, experiment) print '<td align="center" %s> %s </td>' % (color_style, file_type) print '<td align="center" %s> %s </td>' % (color_style, campaign) print '<td align="center" %s> %s </td>' % (color_style, physics_group) print '<td align="center" %s> %s </td>' % (color_style, status) # Add XML button/column xml_disabled = '' if dbutil.xml_disabled(cnx, id): xml_disabled = 'disabled' print '<td>' print '<form target="_self" action="%s/export_project.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Generate project.py XML">' print '<input type="submit" value="XML" %s>' % xml_disabled print '</div>' print '</form>' print '</td>' # Add FIFE button/column fife_disabled = '' if dbutil.xml_disabled(cnx, id): fife_disabled = 'disabled' print '<td>' print '<form target="_self" action="%s/export_fife.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Generate fife_launch config">' print '<input type="submit" value="FIFE" %s>' % xml_disabled print '</div>' print '</form>' print '</td>' # Add POMS button/column print '<td>' print '<form target="_self" action="%s/export_poms.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Generate POMS .ini file">' print '<input type="submit" value="POMS">' print '</form>' print '</td>' # Add Edit button/column print '<td>' print '<form target="_self" action="%s/edit_project.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Edit project">' print '<input type="submit" value="✎">' print '</div>' print '</form>' print '</td>' # Add Clone button/column print '<td>' print '<form target="_self" action="%s/clone_project.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Clone project">' print '<input type="submit" value="⎘">' print '</div>' print '</form>' print '</td>' # Add Delete button/column print '<td>' print '<form action="%s/delete_project.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<div title="Delete project">' print '<input type="submit" value="🗑" %s>' % disabled print '</div>' print '</form>' print '</td>' print '</tr>' print '</table>' # Add lower navigation panel. page_links(qdict, max_page) # Generate html document trailer. print '</body>' print '</html>'
def group_form(cnx, id, qdict): # Construct global disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed(): disabled = 'disabled' # Get group name. name = dbutil.get_group_name(cnx, id) # Generate form. print '<h2>Group %s</h2>' % name # Query full group from database. c = cnx.cursor() q = 'SELECT %s FROM groups WHERE id=%%s' % dbutil.columns('groups') c.execute(q, (id, )) rows = c.fetchall() if len(rows) == 0: raise IOError('Unable to fetch group id %d' % id) row = rows[0] print '<form action="%s/dbhandler.py" method="post">' % dbconfig.rel_url # Add hidden input field to store table name. print '<input type="hidden" id="table" name="table" value="groups">' # Add hidden input field to store save url (parent of this page). print '<input type="hidden" id="saveurl" name="saveurl" value="%s/query_groups.py?%s">' % \ (dbconfig.base_url, dbargs.convert_args(qdict)) # Add hidden qdict input fields. for key in qdict: print '<input type="hidden" name="%s" value="%s">' % ( dbutil.convert_str(key), dbutil.convert_str(qdict[key])) # Loop over fields of this group. # Put fields in a table. print '<table border=1 style="border-collapse:collapse">' cols = databaseDict['groups'] for n in range(len(cols)): coltup = cols[n] colname = coltup[0] coltype = coltup[2] colarray = coltup[3] coldesc = coltup[4] if colname != '': # Set readonly attribute readonly = '' if colname == 'id': readonly = 'readonly' elif disabled != '': readonly = 'readonly' print '<tr>' print '<td>' print '<label for="%s">%s: </label>' % (colname, coldesc) print '</td>' print '<td>' if colarray == 0: # Scalar column. if coltype[0:3] == 'INT': print '<input type="number" id="%s" name="%s" size=10 value="%d" %s>' % \ (colname, colname, row[n], readonly) elif coltype[0:6] == 'DOUBLE': print '<input type="text" id="%s" name="%s" size=10 value="%8.6f" %s>' % \ (colname, colname, row[n], readonly) elif coltype[0:7] == 'VARCHAR': if colname in pulldowns: print '<select id="%s" name="%s" size=0 %s>' % ( colname, colname, disabled) for value in pulldowns[colname]: sel = '' if value == row[n]: sel = 'selected' if value == '' or value == 'Requested': print '<option value="%s" %s>%s</option>' % ( value, sel, value) else: print '<option value="%s" %s %s>%s</option>' % ( value, sel, option_disabled, value) print '</select>' else: print '<input type="text" id="%s" name="%s" size=100 value="%s" %s>' % \ (colname, colname, row[n], readonly) else: # Array columns. # Display using multiline <textarea>. strs = dbutil.get_strings(cnx, row[n]) print '<textarea id="%s" name="%s" rows=%d cols=80 %s>' % \ (colname, colname, max(len(strs),1), readonly) print '\n'.join(strs) print '</textarea>' print '</td>' print '</tr>' # Finish table. print '</table>' # Projects section. print '<h3>Projects</h3>' # Query all projects. project_ids = [] q = 'SELECT id,name FROM projects ORDER BY name' c.execute(q, ()) rows = c.fetchall() for row in rows: project_id = row[0] project_ids.append(project_id) # Query all projects in this group. project_group_ids = set() q = 'SELECT project_id FROM group_project WHERE group_id=%s' c.execute(q, (id, )) rows = c.fetchall() for row in rows: project_id = row[0] project_group_ids.add(project_id) # Projects table. print '<table border=1 style="border-collapse:collapse">' print '<tr>' print '<th>Group Projects</th>' print '<th>Available Projects</th>' print '</tr>' # Generate project lists. print '<td>' # Generate selection list for projects in group. # Loop over project ids in name order. print '<select id="ingroup" name="remove" size=20 multiple>' for project_id in project_ids: if project_id in project_group_ids: project_name = dbutil.get_project_name(cnx, project_id) print '<option value="%d">%s</option>' % (project_id, project_name) print '</select>' print '</td>' print '<td>' # Generate selection list for projects not in group. # Loop over project ids in name order. print '<select id="available" name="add" size=20 multiple>' for project_id in project_ids: if not project_id in project_group_ids: project_name = dbutil.get_project_name(cnx, project_id) print '<option value="%d">%s</option>' % (project_id, project_name) print '</select>' print '</td>' # Finish datasets table. print '</table>' # Add "Save" and "Back" buttons. print '<input type="submit" name="submit" value="Save" %s>' % disabled print '<input type="submit" name="submit" value="Update" %s>' % disabled print '<input type="submit" name="submit" value="Back">' print '</form>'
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>'
def datasets_form(cnx, project_id, qdict): # Query project name and experiment. c = cnx.cursor() q = 'SELECT name, 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] name = row[0] experiment = row[1] # Construct global disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed() and dbutil.restricted_access( cnx, 'projects', project_id): disabled = 'disabled' # Generate form. print '<h2>Datasets for Project %s</h2>' % name for dataset_type in ('input', 'output'): title_type = dataset_type[0].upper() + dataset_type[1:] # Section title. print '<h3>%s Datasets</h3>' % title_type # Add button to add dataset. print '<form action="%s/add_dataset.py?id=%d&type=%s&%s" method="post">' % \ (dbconfig.rel_url, project_id, dataset_type, dbargs.convert_args(qdict)) print '<label for="submit">Add %s Dataset: </label>' % title_type print '<input type="submit" id="submit" value="Add" %s>' % disabled print '</form>' print '<p>' # Query datasets belonging to this project and type. q = 'SELECT id, name, files, events, parent_files, parent_events, parent_id FROM datasets WHERE project_id=%s AND type=%s ORDER BY seqnum' c.execute(q, (project_id, dataset_type)) rows = c.fetchall() # Make dataset table. print '<table border=1 style="border-collapse:collapse">' print '<tr>' print '<th> Dataset ID </th>' print '<th> Dataset Name </th>' print '<th> Files </th>' print '<th> Events </th>' if dataset_type == 'output': print '<th> Parent Files </th>' print '<th> Parent Events </th>' print '<th> Complete (%) </th>' print '</tr>' for row in rows: print '<tr>' dataset_id = row[0] dataset_name = row[1] nfile = row[2] nev = row[3] pfile = row[4] pev = row[5] parent_id = row[6] # If the parent_id is nonzero, query statistics of parent dataset. pfile2 = 0 pev2 = 0 if parent_id != 0: q2 = 'SELECT files, events FROM datasets WHERE id=%s' c.execute(q2, (parent_id, )) rows2 = c.fetchall() if len(rows2) > 0: row2 = rows2[0] pfile2 = row2[0] pev2 = row2[1] # Calculate complete fraction. frac = 0. if pev2 > 0: frac = float(pev) / float(pev2) print '<td align="center">%d</td>' % dataset_id print '<td> <a href=%s/definitions/name/%s>%s</a> </td>' % \ (dbconfig.samweb_url[experiment], dataset_name, dataset_name) print '<td align="right"> %d </td>' % nfile print '<td align="right"> %d </td>' % nev if dataset_type == 'output': print '<td align="right"> %d </td>' % pfile print '<td align="right"> %d </td>' % pev print '<td align="right"> %6.2f </td>' % (100. * frac) # Add Update button/column print '<td>' print '<form action="%s/edit_datasets.py?id=%d&update=%d&%s" method="post">' % \ (dbconfig.rel_url, project_id, dataset_id, dbargs.convert_args(qdict)) print '<div title="Update statistics">' print '<input type="submit" value="+Stats" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Edit button/column print '<td>' print '<form target="_self" action="%s/edit_dataset.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, dataset_id, dbargs.convert_args(qdict)) print '<div title="Edit dataset">' print '<input type="submit" value="✎" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Clone button/column print '<td>' print '<form action="%s/clone_dataset.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, dataset_id, dbargs.convert_args(qdict)) print '<div title="Clone dataset">' print '<input type="submit" value="⎘" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Delete button/column print '<td>' print '<form action="%s/delete_dataset.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, dataset_id, dbargs.convert_args(qdict)) print '<div title="Delete dataset">' print '<input type="submit" value="🗑" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Up button/column print '<td>' print '<form action="%s/up_dataset.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, dataset_id, dbargs.convert_args(qdict)) print '<div title="Move up">' print '<input type="submit" value="▲" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Down button/column print '<td>' print '<form action="%s/down_dataset.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, dataset_id, dbargs.convert_args(qdict)) print '<div title="Move down">' print '<input type="submit" value="▼" %s>' % disabled print '</div>' print '</form>' print '</td>' # Finish row. print '</tr>' # Finish table. print '</table>'
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>'
def stage_form(cnx, id, qdict): # Construct global disabled option for restricted controls. disabled = '' if not dbconfig.restricted_access_allowed() and dbutil.restricted_access( cnx, 'stages', id): disabled = 'disabled' # Query project name and id from database. c = cnx.cursor() q = 'SELECT id, name, project_id FROM stages WHERE id=%s' c.execute(q, (id, )) rows = c.fetchall() if len(rows) == 0: raise IOError('Unable to fetch stage id %d' % id) row = rows[0] name = row[1] project_id = row[2] project_name = dbutil.get_project_name(cnx, project_id) # Generate form. print '<h2>Project %s</h2>' % project_name print '<h2>Stage %s</h2>' % name # Substages section. print '<h2>Substages</h2>' # Add a button to add a new substage. print '<form action="%s/add_substage.py?id=%d&%s" method="post" target="_self">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<input type="submit" value="Add Substage" %s>' % disabled print '</form>' print '<br>' # Query substage ids belonging to this stage. q = 'SELECT id, fclname FROM substages WHERE stage_id=%s ORDER BY seqnum' c.execute(q, (id, )) rows = c.fetchall() if len(rows) > 0: # Add links to edit project stages. print '<table border=1 style="border-collapse:collapse">' print '<tr>' print '<th> Substage ID </th>' print '<th> FCL </th>' print '</tr>' for row in rows: print '<tr>' substage_id = row[0] fclname = row[1] print '<td align="center">%d</td>' % substage_id print '<td> <a target="_self" href="%s/edit_substage.py?id=%d&%s">%s</a> </td>' % \ (dbconfig.base_url, substage_id, dbargs.convert_args(qdict), fclname) # Add Edit button/column print '<td>' print '<form target="_self" action="%s/edit_substage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, substage_id, dbargs.convert_args(qdict)) print '<div title="Edit substage">' print '<input type="submit" value="✎">' print '</div>' print '</form>' print '</td>' # Add Clone button/column print '<td>' print '<form target="_self" action="%s/clone_substage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, substage_id, dbargs.convert_args(qdict)) print '<div title="Clone substage">' print '<input type="submit" value="⎘" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Delete button/column print '<td>' print '<form action="%s/delete_substage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, substage_id, dbargs.convert_args(qdict)) print '<div title="Delete substage">' print '<input type="submit" value="🗑" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Up button/column print '<td>' print '<form action="%s/up_substage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, substage_id, dbargs.convert_args(qdict)) print '<div title="Move up">' print '<input type="submit" value="▲" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Down button/column print '<td>' print '<form action="%s/down_substage.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, substage_id, dbargs.convert_args(qdict)) print '<div title="Move down">' print '<input type="submit" value="▼" %s>' % disabled print '</div>' print '</form>' print '</td>' # Finish row. print '</tr>' # Finish table. print '</table>' # Overrides section. print '<h2>Overrides</h2>' # Add a button to add a new override. print '<form action="%s/add_override.py?id=%d&%s" method="post" target="_self">' % \ (dbconfig.rel_url, id, dbargs.convert_args(qdict)) print '<table>' print '<tr>' print '<td>' print '<label for="name">Name:</label>' print '</td>' print '<td>' print '<input type="text" id="name" name="name" value="">' print '</td>' print '</tr>' print '<tr>' print '<td>' print '<label for="override_type">Type:</label>' print '</td>' print '<td>' print '<select id="override_type" name="override_type" size=0>' pulldown_list = pulldowns['override_type'] sel = 'selected' for value in pulldown_list: print '<option value="%s" %s>%s</option>' % (value, sel, value) sel = '' print '</select>' print '</td>' print '</tr>' print '<tr>' print '<td>' print '<label for="value">Value:</label>' print '</td>' print '<td>' print '<input type="text" id="value" name="value" value="">' print '</td>' print '</tr>' print '</table>' print '<input type="submit" value="Add Override" %s>' % disabled print '</form>' print '<br>' # Query override ids belonging to this stage. q = 'SELECT id, name, override_type, value FROM overrides WHERE stage_id=%s ORDER BY override_type' c.execute(q, (id, )) rows = c.fetchall() if len(rows) > 0: # Add links to edit project stages. print '<table border=1 style="border-collapse:collapse">' print '<tr>' print '<th> ID </th>' print '<th> Name </th>' print '<th> Type </th>' print '<th> Value </th>' print '</tr>' for row in rows: print '<tr>' override_id = row[0] name = row[1] override_type = row[2] value = row[3] print '<td align="center">%d</td>' % override_id print '<td align="left"> %s </td>' % name print '<td align="left"> %s </td>' % override_type print '<td align="value" > %s </td>' % value # Add Edit button/column print '<td>' print '<form target="_self" action="%s/edit_override.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, override_id, dbargs.convert_args(qdict)) print '<div title="Edit override">' print '<input type="submit" value="✎">' print '</div>' print '</form>' print '</td>' # Add Clone button/column print '<td>' print '<form target="_self" action="%s/clone_override.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, override_id, dbargs.convert_args(qdict)) print '<div title="Clone override">' print '<input type="submit" value="⎘" %s>' % disabled print '</div>' print '</form>' print '</td>' # Add Delete button/column print '<td>' print '<form action="%s/delete_override.py?id=%d&%s" method="post">' % \ (dbconfig.rel_url, override_id, dbargs.convert_args(qdict)) print '<div title="Delete override">' print '<input type="submit" value="🗑" %s>' % disabled print '</div>' print '</form>' print '</td>' # Finish row. print '</tr>' # Finish table. print '</table>' print '<h2>Stage Data</h2>' # Query full stage from database. q = 'SELECT %s FROM stages WHERE id=%%s' % dbutil.columns('stages') c.execute(q, (id, )) rows = c.fetchall() if len(rows) == 0: raise IOError('Unable to fetch stage id %d' % id) row = rows[0] print '<form action="%s/dbhandler.py" method="post">' % dbconfig.rel_url # Add hidden input field to store table name. print '<input type="hidden" id="table" name="table" value="stages">' # Add hidden input field to store save url (parent of this page). print '<input type="hidden" id="saveurl" name="saveurl" value="%s/edit_project.py?id=%d&%s">' % \ (dbconfig.base_url, project_id, dbargs.convert_args(qdict)) # Add hidden qdict input fields. for key in qdict: print '<input type="hidden" name="%s" value="%s">' % ( dbutil.convert_str(key), dbutil.convert_str(qdict[key])) # Loop over fields of this stage. # Put fields in a table. print '<table border=1 style="border-collapse:collapse">' cols = databaseDict['stages'] for n in range(len(cols)): coltup = cols[n] colname = coltup[0] coltype = coltup[2] colarray = coltup[3] coldesc = coltup[4] if colname != '': # Set readonly attribute readonly = '' if colname == 'id' or colname == 'project_id': readonly = 'readonly' elif disabled != '': readonly = 'readonly' print '<tr>' print '<td>' print '<label for="%s">%s: </label>' % (colname, coldesc) print '</td>' print '<td>' if colarray == 0: # Scalar column. if coltype[0:3] == 'INT': print '<input type="number" id="%s" name="%s" size=10 value="%d" %s>' % \ (colname, colname, row[n], readonly) elif coltype[0:6] == 'DOUBLE': value = row[n] if value == None: value = 0. print '<input type="text" id="%s" name="%s" size=100 value="%8.6f" %s>' % \ (colname, colname, value, readonly) elif coltype[0:7] == 'VARCHAR': if colname in pulldowns: print '<select id="%s" name="%s" size=0 %s>' % ( colname, colname, disabled) pulldown_list = pulldowns[colname] if type(pulldown_list) == type({}): if experiment in pulldown_list: pulldown_list = pulldowns[colname][experiment] else: pulldown_list = [''] for value in pulldown_list: sel = '' if value == row[n]: sel = 'selected' print '<option value="%s" %s>%s</option>' % ( value, sel, value) print '</select>' else: print '<input type="text" id="%s" name="%s" size=100 value="%s" %s>' % \ (colname, colname, row[n], readonly) # Add datasets. #if (colname == 'defname' or colname == 'ana_defname') and row[n] != '': # dbutil.add_dataset(cnx, project_id, 'output', row[n]) #if colname == 'inputdef' and row[n] != '': # dbutil.add_dataset(cnx, project_id, 'input', row[n]) else: # Array columns. # Display using multiline <textarea>. strs = dbutil.get_strings(cnx, row[n]) print '<textarea id="%s" name="%s" rows=%d cols=80 %s>' % \ (colname, colname, max(len(strs), 1), readonly) print '\n'.join(strs) print '</textarea>' print '</td>' print '</tr>' # Finish table. print '</table>' # Add "Save" and "Back" buttons. print '<input type="submit" name="submit" value="Save" %s>' % disabled print '<input type="submit" name="submit" value="Update" %s>' % disabled print '<input type="submit" name="submit" value="Back">' print '</form>'