Moving webpage to wiki
[multilist] / src / libliststorehandler.py
index eee2b31..4c64fe0 100644 (file)
@@ -20,6 +20,11 @@ along with Multilist.  If not, see <http://www.gnu.org/licenses/>.
 Copyright (C) 2008 Christoph Würstle
 """
 
+from __future__ import with_statement
+
+import ConfigParser
+import csv
+import uuid
 import logging
 
 import gtk
@@ -37,36 +42,71 @@ _moduleLogger = logging.getLogger(__name__)
 
 class Liststorehandler(object):
 
+       SHOW_ALL = "all"
+       SHOW_NEW = "-1"
+       SHOW_ACTIVE = "0"
+       SHOW_COMPLETE = "1"
+       ALL_FILTERS = (SHOW_ALL, SHOW_NEW, SHOW_ACTIVE, SHOW_COMPLETE)
+
        def __init__(self, db, selection):
                self.db = db
+               self.__filter = self.SHOW_ALL
                self.liststore = None
                self.unitsstore = None
                self.selection = selection
-               self.collist = ("uid", "status", "title", "quantitiy", "unit", "price", "priority", "date", "private", "stores", "note", "custom1", "custom2")
+               self.collist = ("uid", "status", "title", "quantity", "unit", "price", "priority", "date", "private", "stores", "note", "custom1", "custom2")
 
-               #sql = "DROP TABLE items"
-               #self.db.speichereSQL(sql)
-
-               sql = "CREATE TABLE items (uid TEXT, list TEXT, category TEXT, status TEXT, title TEXT, quantitiy TEXT, unit TEXT, price TEXT, priority TEXT, date TEXT, pcdate TEXT, private TEXT, stores TEXT, note TEXT, custom1 TEXT, custom2 TEXT)"
+               sql = "CREATE TABLE items (uid TEXT, list TEXT, category TEXT, status TEXT, title TEXT, quantity TEXT, unit TEXT, price TEXT, priority TEXT, date TEXT, pcdate TEXT, private TEXT, stores TEXT, note TEXT, custom1 TEXT, custom2 TEXT)"
                self.db.speichereSQL(sql)
 
                self.selection.load()
                self.selection.connect("changed", self.update_list)
                #self.selection.connect("changedCategory", self.update_category)
 
-               """
-               sql = "INSERT INTO items (uid, list, category, title) VALUES (?, ?, ?, ?)"
-               import uuid
-               self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "atitel1"))
-               self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "btitel2"))
-               self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "ctitel3"))
-               
-               print "inserted"
-               """
+       def save_settings(self, config, sectionName):
+               config.set(sectionName, "filter", self.__filter)
+
+       def load_settings(self, config, sectionName):
+               try:
+                       selectedFilter = config.get(sectionName, "filter")
+                       self.set_filter(selectedFilter)
+               except ConfigParser.NoSectionError:
+                       pass
+               except ConfigParser.NoOptionError:
+                       pass
+
+       def export_data(self, filename):
+               sql = "SELECT list, category, uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 FROM items ORDER BY list, title ASC"
+               rows = self.db.ladeSQL(sql)
+               with open(filename, "w") as f:
+                       csvWriter = csv.writer(f)
+                       headerRow = ["list", "category"]
+                       headerRow.extend(self.collist)
+                       csvWriter.writerow(headerRow)
+                       csvWriter.writerows(rows)
+
+       def append_data(self, filename):
+               with open(filename, "r") as f:
+                       csvReader = csv.reader(f)
+                       for row in csvReader:
+                               uid = str(uuid.uuid4())
+                               row[2] = uid
+                               sql = "INSERT INTO items (list, category, uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
+                               self.db.speichereSQL(sql, row, rowid = uid)
+               self.db.commitSQL()
+               self.update_list()
+
+       def set_filter(self, filter):
+               assert filter in self.ALL_FILTERS
+               self.__filter = filter
+               self.update_list()
+
+       def get_filter(self):
+               return self.__filter
 
        def get_unitsstore(self):
-               if (self.unitsstore == None):
-                       self.unitsstore = gtk.ListStore(str, str, str, str, str,  str, str, str, str, str, str, str, str)
+               if self.unitsstore is None:
+                       self.unitsstore = gtk.ListStore(str, str, str, str, str, str, str, str, str, str, str, str, str)
                self.unitsstore.clear()
                #row(3) quantities
                #row 4 units
@@ -85,32 +125,34 @@ class Liststorehandler(object):
 
                return self.unitsstore
 
+       def __calculate_status(self):
+               if self.__filter == self.SHOW_ALL:
+                       status = self.SHOW_NEW
+               else:
+                       status = self.__filter
+               return status
+
        def get_liststore(self, titlesearch = ""):
-               if (self.liststore == None):
-                       self.liststore = gtk.ListStore(str, str, str, str, str,  str, str, str, str, str, str, str, str)
+               if self.liststore is None:
+                       self.liststore = gtk.ListStore(str, str, str, str, str, str, str, str, str, str, str, str, str)
                self.liststore.clear()
 
-               titlesearch = titlesearch+"%"
+               titlesearch = "%"+titlesearch+"%"
 
-               if (self.selection.get_status() == "0"): #only 0 and 1 (active items)
-                       sql = "SELECT uid, status, title, quantitiy, unit, price, priority, date, private, stores, note, custom1, custom2 FROM items WHERE list = ? AND category LIKE ? AND status> = ? AND title like ? ORDER BY category, status, title"
-                       rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), self.selection.get_status(), titlesearch))
+               if self.__filter != self.SHOW_ALL:
+                       status = self.__calculate_status()
+                       sql = "SELECT uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 FROM items WHERE list = ? AND category LIKE ? AND status = ? AND title like ? ORDER BY category, status, title"
+                       rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), status, titlesearch))
                else:
-                       sql = "SELECT uid, status, title, quantitiy, unit, price, priority, date, private, stores, note, custom1, custom2 FROM items WHERE list = ? AND category LIKE ? AND title LIKE ? ORDER BY category, title ASC"
+                       sql = "SELECT uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 FROM items WHERE list = ? AND category LIKE ? AND title LIKE ? ORDER BY category, title ASC"
                        rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), titlesearch))
 
-               #print rows
-               if ((rows is not None) and (len(rows) > 0)):
+               if rows is not None:
                        for row in rows:
-                               uid, status, title, quantitiy, unit, price, priority, date, private, stores, note, custom1, custom2 = row
-                               if unit == None:
-                                       pass
-                                       #unit = ""
-                               self.liststore.append([uid, status, title, quantitiy, unit, price, priority, date, private, stores, note, custom1, custom2])
-                       #else:
-                       #self.liststore.append(["-1", "-1", "", " ", "", "", "", "", "", "", "", "", ""])       
-               #import uuid
-               #self.liststore.append(["-1", "-1", "", "", "", "", "", "", "", "", "", "", ""])
+                               uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 = row
+                               if unit is None:
+                                       unit = ""
+                               self.liststore.append([uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2])
 
                return self.liststore
 
@@ -121,10 +163,6 @@ class Liststorehandler(object):
                return False
 
        def update_row(self, irow, icol, new_text):
-               #print "liststore 1"
-               #for x in self.liststore:
-               #       print x[0], x[2]
-
                if -1 < irow and self.liststore[irow][0] != "-1" and self.liststore[irow][0] is not None:
                        sql = "UPDATE items SET "+self.collist[icol]+" = ? WHERE uid = ?"
                        self.db.speichereSQL(sql, (new_text, self.liststore[irow][0]), rowid = self.liststore[irow][0])
@@ -135,43 +173,23 @@ class Liststorehandler(object):
                else:
                        _moduleLogger.warning("update_row: row does not exist")
                        return
-                       #if (self.emptyValueExists() == True)and(icol<2):
-                       #       #print "letzter Eintrag ohne inhalt"
-                       #       return
-
-               #print "liststore 2"
-               #for x in self.liststore:
-               #       print x[0], x[2]
 
        def checkout_rows(self):
                sql = "UPDATE items SET status = ? WHERE list = ? AND category LIKE ? AND status = ?"
-               self.db.speichereSQL(sql, ("-1", self.selection.get_list(), self.selection.get_category(True), "1"))
+               self.db.speichereSQL(sql, (self.SHOW_NEW, self.selection.get_list(), self.selection.get_category(True), self.SHOW_COMPLETE))
                for i in range(len(self.liststore)):
-                       if self.liststore[i][1] == "1":
-                               self.liststore[i][1] = "-1"
+                       if self.liststore[i][1] == self.SHOW_COMPLETE:
+                               self.liststore[i][1] = self.SHOW_NEW
 
        def add_row(self, title = ""):
-               #self.update_row(-1, 1, "-1")
-               #for x in self.liststore:
-               #       print x[0], x[2]
-               status = self.selection.get_status()
-               import uuid
+               status = self.__calculate_status()
                uid = str(uuid.uuid4())
                sql = "INSERT INTO items (uid, list, category, status, title) VALUES (?, ?, ?, ?, ?)"
                self.db.speichereSQL(sql, (uid, self.selection.get_list(), self.selection.get_category(), status, title), rowid = uid)
                _moduleLogger.info("Insertet row: status = "+status+" with uid "+str(uid))
-                       #self.liststore[irow][0] = str(uuid.uuid4())
 
                self.liststore.append([uid, status, title, " ", "", "", "", "", "", "", "", "", ""])
                self.selection.comboLists_check_for_update()
-               #       if (irow>-1):
-               #               self.liststore[irow][icol] = new_text
-               #               self.liststore[irow][0] = uid
-               #       else:
-               #               self.liststore.append([uid, "-1", "", " ", "", "", "", "", "", "", "", "", ""])
-                               #print "xy", self.liststore[len(self.liststore)-1][0]
-                       #Check if a new list/category is opened
-               #       self.selection.comboLists_check_for_update()
 
        def del_row(self, irow, row_iter):
                uid = self.liststore[irow][0]
@@ -191,7 +209,7 @@ class Liststorehandler(object):
        def rename_category(self, new_name):
                sql = "UPDATE items SET category = ? WHERE list = ? AND category = ?"
                self.db.speichereSQL(sql, (new_name, self.selection.get_list(), self.selection.get_category()))
-               self.selection.comboList_changed()
+               self.selection.update_categories()
                self.selection.set_category(new_name)
 
        def rename_list(self, new_name):