def register(self, n): parent = None try: lastid = self.main[n[0][0]] parent = self.collection[lastid] except: pass if parent == None: parent = Category(n[0][0]) if n[0][1] != None: col = n[0][1] else: col = ColorGenerator.to_html_color(self.color_generator.generate_rgb()) parent.color = col lastid = self.add_to_collection(parent) self.main[n[0][0]] = lastid #color = parent.color for x in n[1:]: new_child = None try: cids = self.names[x[0]] except: new_child = True if new_child == None: for i in cids: if i in parent.childs: new_child = False lastid = i parent = self.collection[i] break if new_child == True: nc = Category(x[0], parent=lastid) if x[1] != None: col = x[1] else: col = ColorGenerator.to_html_color(self.color_generator.generate_rgb()) nc.color = col lastid = self.add_to_collection(nc) parent.childs.append(lastid) parent = nc return lastid
def register(self, n): parent = None c = self.connection.cursor() for cat in n: print("locating: " + str(cat[0]) + ", " + str(parent)) if parent: c.execute("SELECT id,name,id_parent FROM category WHERE name=? and id_parent=?", [cat[0], parent]) else: c.execute("SELECT id,name,id_parent FROM category WHERE name=? and id_parent IS NULL", [cat[0]]) r = c.fetchone() if r: parent = r[0] else: c.execute("INSERT INTO category(name, color, id_parent) VALUES (?, ?, ?)", [cat[0], ColorGenerator.to_html_color(self.color_generator.generate_rgb()), parent]) parent = c.lastrowid return parent