123
authortanya <tanya@tanin.oblgaz>
Mon, 8 Feb 2010 15:02:32 +0000 (17:02 +0200)
committertanya <tanya@tanin.oblgaz>
Mon, 8 Feb 2010 15:02:32 +0000 (17:02 +0200)
database/db.py
list.py

index 1455111..bdb24a0 100755 (executable)
@@ -20,7 +20,7 @@ class DbSqlite:
     def connect(self):
 
         """connect to database"""
-        dbname = "meabookdb"
+        dbname = "meabookdb_oblgas"
         if os.access(dbname, os.F_OK|os.R_OK|os.W_OK):
             conn = sqlite3.connect(dbname)
         else:
@@ -74,6 +74,46 @@ class DbSqlite:
         else:
             id = res[0]
         return id
+    
+    def get_parent(self, id):
+        
+        if self.conn is None:
+            return None
+        cur = self.conn.cursor()
+        cur.execute("select parent from struct where id=?", (id,))
+        res = cur.fetchone()
+        id = 0
+        if res != None:
+            id = res[0]
+        return id
+    
+    def get_name_struct(self, id):
+        
+        if self.conn is None:
+            return None
+        cur = self.conn.cursor()
+        cur.execute("select name from struct where id=?", (id,))
+        res = cur.fetchone()
+        name = "All"
+        if res != None:
+            name = res[0]
+        return name
+
+    def get_id_struct(self, value, parent):
+        
+        """get id for value into struct"""
+        if self.conn is None:
+            return None
+        cur = self.conn.cursor()
+        cur.execute("select id from struct where name=? and parent=?", (value, parent))
+        res = cur.fetchone()
+        id = 0
+        if res is None:
+            cur.execute("insert into struct (name, parent) values (?, ?)", (value, parent))
+            id = cur.lastrowid
+        else:
+            id = res[0]
+        return id
 
     def ins_record(self, rec):
 
@@ -94,32 +134,22 @@ class DbSqlite:
         
         struct = ["", ""]
         for key, val in rec.items():
-            #print "key=%s value=%s" % (key, val)
-            #cur.execute("select id from field where name=?", (key,))
-
-            #id_field = cur.fetchone()
-            #if id_field is None:
-            #    #write new field ваы
-            #    cur.execute("insert into field (name) values (?)", (key,))
-            #    id_field = cur.lastrowid
-            #else:
-            #    id_field = id_field[0]
-            # insert row into data
-            #print "id=%s id_field=%s value=%s" % (id_record, id_field, val)
+            if val == None:
+                continue
             id_field = self.get_id("field", key)
             if key == 'company':
                 struct[0] = val
             if key == 'department':
                 struct[1] = val
-            for i in val:
-                cur.execute("insert into data (id, id_field, value) values (?, ?, ?)", 
-                (id_record, id_field, i))
-
+            #for i in val:
+            cur.execute("insert into data (id, id_field, value) values (?, ?, ?)", 
+                (id_record, id_field, val))
         #insert comppane into table struct
-        id0 = self.get_id("struct", struct[0][0])
+        id0 = self.get_id("struct", struct[0])
         cur.execute("update struct set parent=0 where id=?", (id0,))
-        id1 = self.get_id("struct", struct[1][0])
-        cur.execute("update struct set parent=? where id=?", (id0, id1))
+        
+        id1 = self.get_id_struct(struct[1], id0)
+        #cur.execute("update struct set parent=? where id=?", (id0, id1))
         cur.execute("insert into relation (id_data, id_struct) values (?, ?)", (id_record, id1))
         self.conn.commit()
         #conn.close()
diff --git a/list.py b/list.py
index 7872afb..a04b6e8 100755 (executable)
--- a/list.py
+++ b/list.py
@@ -1,4 +1,5 @@
 #!/usr/bin/python
+# -*- coding: utf-8 -*-
 import pygtk
 pygtk.require('2.0')
 import gtk
@@ -41,10 +42,17 @@ class List:
     
         self.window.set_border_width(10)
 
-        vbox = gtk.VBox(False, 4)
+        vbox = gtk.VBox(False, 0)
 
-        self.hbox = gtk.HBox(False, 2)
+        self.hbox = gtk.HBox(False, 0)
         
+        self.buttons = {} 
+        self.buttons[0] = gtk.Button(u"Всего")
+        self.max_buttons = 1;
+        self.buttons[0].connect("clicked", self.button_back, 0, 0)
+        self.hbox.add(self.buttons[0])
+        self.buttons[0].show()
+       
 
         sw = gtk.ScrolledWindow()
         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
@@ -54,14 +62,14 @@ class List:
         vbox.pack_end(sw, True, True, 0)
         
         self.dbo = db.DbSqlite()
-        #data = self.dbo.get_allrecords('cn')
         data = self.dbo.get_list(0)
 
-        store = gtk.ListStore(int, str, int)
-        for d in data:
-            store.append([d, data[d], 0])
+        self.tree = gtk.TreeView()
+        self.show_list(0)
+        #store = gtk.ListStore(int, str, int)
+        #for d in data:
+        #    store.append([d, data[d], 0])
         
-        self.tree = gtk.TreeView(store)
         self.tree.set_rules_hint(True)
 
         self.tree.connect("row-activated", self.select_item)
@@ -101,7 +109,6 @@ class List:
 
     def select_item(self, widget, path, column):
         
-        store = gtk.ListStore(int, str, int)
         parent = widget.get_model()[path][0]
         type = widget.get_model()[path][2]
         if type == 1:
@@ -109,12 +116,20 @@ class List:
             return
         print parent
         #create button for back
-        but = gtk.Button(widget.get_model()[path][1])
-        #add callback function
-        but.connect("clicked", self.button_back, parent)
+        if self.buttons.has_key(self.max_buttons) == 0:
+            self.buttons[self.max_buttons] = gtk.Button()
+        self.buttons[self.max_buttons].set_label(widget.get_model()[path][1])
+        self.buttons[self.max_buttons].connect("clicked", self.button_back, parent, self.max_buttons)
+
+        self.hbox.add(self.buttons[self.max_buttons])
+        self.buttons[self.max_buttons].show()
+        self.max_buttons = self.max_buttons + 1
+        self.buttons[self.max_buttons] = gtk.Button()
+        self.show_list(parent)
 
-        self.hbox.add(but)
-        but.show()
+    def show_list(self, parent):
+
+        store = gtk.ListStore(int, str, int)
         data = self.dbo.get_list(parent)
         for d in data:
             store.append([d, data[d], 0])
@@ -122,10 +137,20 @@ class List:
         data = self.dbo.get_allrecords('cn', parent)
         for d in data:
             store.append([d, data[d], 1])
-        self.tree.set_model(store)    
+        self.tree.set_model(store) 
+
+    def button_back(self, widget, id=None, number=None):
 
-    def button_back(self, widget, data=None):
-        print "button back data= %i " % data
+        """return back"""
+        self.show_list(id)
+         
+        # delete not use buttons
+        while self.max_buttons - 1 > number:
+            self.buttons[self.max_buttons-1].set_label = "";
+
+            self.max_buttons = self.max_buttons - 1
+        
+        print "button back data= %i " % number
 
     def show_record(self, id):
         self.dialog = gtk.Dialog("Item descrition",