Adding import/export capability
authorEd Page <eopage@byu.net>
Sat, 24 Apr 2010 00:19:30 +0000 (19:19 -0500)
committerEd Page <eopage@byu.net>
Sat, 24 Apr 2010 00:19:30 +0000 (19:19 -0500)
src/libliststorehandler.py
src/libspeichern.py
src/multilist_gtk.py

index 698732b..893a354 100644 (file)
@@ -21,6 +21,8 @@ Copyright (C) 2008 Christoph Würstle
 """
 
 import ConfigParser
+import csv
+import uuid
 import logging
 
 import gtk
@@ -71,6 +73,27 @@ class Liststorehandler(object):
                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
@@ -81,7 +104,7 @@ class Liststorehandler(object):
 
        def get_unitsstore(self):
                if self.unitsstore is None:
-                       self.unitsstore = gtk.ListStore(str, str, str, str, str,  str, str, str, str, str, str, str, str)
+                       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
@@ -125,9 +148,8 @@ class Liststorehandler(object):
                if rows is not None:
                        for row in rows:
                                uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 = row
-                               if unit == None:
-                                       pass
-                                       #unit = ""
+                               if unit is None:
+                                       unit = ""
                                self.liststore.append([uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2])
 
                return self.liststore
@@ -159,7 +181,6 @@ class Liststorehandler(object):
 
        def add_row(self, title = ""):
                status = self.__calculate_status()
-               import uuid
                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)
index ebe815b..49b2b76 100644 (file)
@@ -20,13 +20,15 @@ along with Multilist.  If not, see <http://www.gnu.org/licenses/>.
 Copyright (C) 2008 Christoph Würstle
 """
 
+from __future__ import with_statement
+
+import sys
+import os
 import time
 import sqlite3
 import shelve
-import sys
 import string
 import shutil
-import os
 import logging
 
 try:
index 3eaf97e..0a050e1 100755 (executable)
@@ -116,6 +116,14 @@ class Multilist(hildonize.get_app_class()):
                        menu_items.connect("activate", self.sync_notes, None)
                        fileMenu.append(menu_items)
 
+                       menu_items = gtk.MenuItem(_("Import"))
+                       menu_items.connect("activate", self._on_import, None)
+                       fileMenu.append(menu_items)
+
+                       menu_items = gtk.MenuItem(_("Export"))
+                       menu_items.connect("activate", self._on_export, None)
+                       fileMenu.append(menu_items)
+
                        menu_items = gtk.MenuItem(_("Quit"))
                        menu_items.connect("activate", self._on_destroy, None)
                        fileMenu.append(menu_items)
@@ -225,6 +233,14 @@ class Multilist(hildonize.get_app_class()):
                        button.connect("clicked", self._on_click_menu_filter, self.liststorehandler.SHOW_COMPLETE)
                        button.set_mode(False)
 
+                       button = gtk.Button(_("Import"))
+                       button.connect("clicked", self._on_import)
+                       menuBar.append(button)
+
+                       button = gtk.Button(_("Export"))
+                       button.connect("clicked", self._on_export)
+                       menuBar.append(button)
+
                        renameListButton= gtk.Button(_("Rename List"))
                        renameListButton.connect("clicked", self.bottombar.rename_list)
                        menuBar.append(renameListButton)
@@ -414,6 +430,46 @@ class Multilist(hildonize.get_app_class()):
                self.selection.comboCategory_changed()
                self.liststorehandler.update_list()
 
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def _on_import(self, *args):
+               csvFilter = gtk.FileFilter()
+               csvFilter.set_name("Import Lists")
+               csvFilter.add_pattern("*.csv")
+               importFileChooser = gtk.FileChooserDialog(
+                       title="Contacts",
+                       action=gtk.FILE_CHOOSER_ACTION_OPEN,
+                       parent=self.window,
+               )
+               importFileChooser.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
+               importFileChooser.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
+
+               importFileChooser.set_property("filter", csvFilter)
+               userResponse = importFileChooser.run()
+               importFileChooser.hide()
+               if userResponse == gtk.RESPONSE_OK:
+                       filename = importFileChooser.get_filename()
+                       self.liststorehandler.append_data(filename)
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def _on_export(self, *args):
+               csvFilter = gtk.FileFilter()
+               csvFilter.set_name("Export Lists")
+               csvFilter.add_pattern("*.csv")
+               importFileChooser = gtk.FileChooserDialog(
+                       title="Contacts",
+                       action=gtk.FILE_CHOOSER_ACTION_SAVE,
+                       parent=self.window,
+               )
+               importFileChooser.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
+               importFileChooser.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
+
+               importFileChooser.set_property("filter", csvFilter)
+               userResponse = importFileChooser.run()
+               importFileChooser.hide()
+               if userResponse == gtk.RESPONSE_OK:
+                       filename = importFileChooser.get_filename()
+                       self.liststorehandler.export_data(filename)
+
        def _prepare_sync_dialog(self):
                self.sync_dialog = gtk.Dialog(_("Sync"), None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))