#!/usr/bin/python # there is this module for obtaining cgi form data #import cgi import sys # define a function to print error messages def error(msg): print """ Error: %s""" % msg print "\n" sys.exit() import psycopg2 #connect to a database try: con = psycopg2.connect(database='jensen', host='cs-linux', user='mickey',password='MoUsE') except: error("Error connecting to server") # make a cursor object for this connection # you can make as many as you like curs = con.cursor() ucurs = con.cursor() # for update operations # do a query curs.execute("SELECT name, street, city, prov, postal_code, phone FROM address order by name") # get the resulting table, a list of lists import pygtk pygtk.require('2.0') import gtk def n2b (str): if str == None: return " " else: return str.rstrip() class Prof: # This is a callback function. The data arguments are ignored # in this example. More on callbacks below. # when button is clicked, get the next row and put pname in label def next(self, widget, data=None): #row = curs.fetchone() if self.nextrow < len(self.tabledata)-1: self.nextrow += 1 self.showrow(self.nextrow) def prev(self, widget, data=None): if self.nextrow > 0: self.nextrow -= 1 self.showrow(self.nextrow) def showrow(self, index): row = self.tabledata[index] self.oldname = row[0] self.entry[0].set_text(n2b(row[0])) self.entry[1].set_text(n2b(row[1])) self.entry[2].set_text(n2b(row[2])) self.entry[3].set_text(n2b(row[3])) self.entry[4].set_text(n2b(row[4])) self.entry[5].set_text(n2b(row[5])) def update(self, widget, data=None): # When update is pressed, UPDATE with the data in entry name = self.entry[0].get_text() street = self.entry[1].get_text() city = self.entry[2].get_text() prov = self.entry[3].get_text() postal_code = self.entry[4].get_text() phone = self.entry[5].get_text() print "Update called, %s %s %s %s" % (name, street, city, phone) if name == self.oldname: ucurs.execute ("UPDATE address set street = %s, city = %s, prov = %s, postal_code = %s, phone = %s WHERE name = %s",\ [street, city, prov, postal_code, phone, name]) #update our stored data as well self.tabledata[self.nextrow] = [name, street, city, prov, postal_code, phone] else: #New name, insert, don't change old row, insert instead ucurs.execute ("Insert into address values (%s,%s,%s,%s,%s,%s)",\ [name, street, city, prov, postal_code, phone]) self.tabledata.append([name, street, city, prov, postal_code, phone]) print "NEW row inserted" def rollback(self, widget, data=None): con.rollback() #print "all updates rolled back on the database" #print "(you will continue to see them in THIS gui,\n so you can redo anything you want to keep)" popup=gtk.MessageDialog(message_format="Updates rolled back on the Database!", buttons=gtk.BUTTONS_CLOSE) popup.format_secondary_markup("(you will continue to see them in THIS gui,\n so you can redo anything you want to keep)") resp = popup.run() #show and wait for close signal if resp == gtk.RESPONSE_CLOSE: popup.destroy() def delete_event(self, widget, event, data=None): # If you return False in the "delete_event" signal handler, # GTK will emit the "destroy" signal. Returning True means # you don't want the window to be destroyed. # This is useful for popping up 'are you sure you want to quit?' # type dialogs. print "delete event occurred" # Change False to True and the main window will not be destroyed # with a "delete_event". return False # Another callback def destroy(self, widget, data=None): curs.close() # close the database connection con.commit() con.close() print "Connection closed" gtk.main_quit() def __init__(self): self.tabledata = curs.fetchall() #use in GUI to scroll through rows self.nextrow = -1 self.oldname = None # create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) # When the window is given the "delete_event" signal (this is given # by the window manager, usually by the "close" option, or on the # titlebar), we ask it to call the delete_event () function # as defined above. The data passed to the callback # function is NULL and is ignored in the callback function. self.window.connect("delete_event", self.delete_event) # Here we connect the "destroy" event to a signal handler. # This event occurs when we call gtk_widget_destroy() on the window, # or if we return False in the "delete_event" callback. self.window.connect("destroy", self.destroy) # Sets the border width of the window. self.window.set_border_width(10) # Creates a new button with the label "Hello World". self.button = gtk.Button("Next Record") # When the button receives the "clicked" signal, it will call the # function hello() passing it None as its argument. The hello() # function is defined above. self.button.connect("clicked", self.next, None) self.buttonback = gtk.Button("Previous Record") self.buttonback.connect("clicked", self.prev, None) self.ubutton = gtk.Button("Update") self.ubutton.connect("clicked", self.update, None) self.rollbutton = gtk.Button("Rollback updates") self.rollbutton.connect("clicked", self.rollback, None) # --- now I add a label to hold pname values self.labels = ['Name',"Street","City","Province",'Postal Code','Phone'] self.plabel = [] self.entry = [] for init in self.labels: self.plabel.append(gtk.Label(init)) self.entry.append(gtk.Entry(20)) #self.plabel.append(gtk.Label("Prof name")) self.vboxl = gtk.VBox(True, 5) for label in self.plabel: self.vboxl.add(label) self.vboxl.add(self.button) self.vboxl.add(self.buttonback) # The final step is to display this newly created widget. # showall does this # self.button.show() self.vboxr = gtk.VBox(True, 5) for e in self.entry: self.vboxr.add(e) self.vboxr.add(self.ubutton) self.vboxr.add(self.rollbutton) self.hbox = gtk.HBox(False, 5) self.hbox.add(self.vboxl) self.hbox.add(self.vboxr) self.window.add(self.hbox) # and the window self.window.show_all() def main(self): # All PyGTK applications must have a gtk.main(). Control ends here # and waits for an event to occur (like a key press or mouse event). gtk.main() # If the program is run directly or passed as an argument to the python # interpreter then create a HelloWorld instance and show it if __name__ == "__main__": gui = Prof() gui.main()