[billreminder] Create default categories only for new installs.



commit b4b4f50f8ded31b41d608fa2fda2f30790f77e08
Author: Og B. Maciel <ogmaciel gnome org>
Date:   Fri Dec 24 12:11:34 2010 -0500

    Create default categories only for new installs.

 src/lib/actions.py |   26 ++------------------------
 src/lib/dal.py     |   37 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 25 deletions(-)
---
diff --git a/src/lib/actions.py b/src/lib/actions.py
index 95abc19..0ee181f 100644
--- a/src/lib/actions.py
+++ b/src/lib/actions.py
@@ -10,7 +10,8 @@ import datetime
 from db.entities import Bill, Category
 from sqlalchemy.sql import func
 from sqlalchemy.orm import eagerload, outerjoin
-from lib import common, scheduler
+from lib import common
+from lib import scheduler
 from lib.utils import force_string
 from lib.utils import verify_dbus_service
 
@@ -22,29 +23,6 @@ class Actions(object):
 
         self.dal = databaselayer
 
-    def default_categories(self):
-        categories = [
-                ("Utilities", '#f8bcffff0db4'),
-                ("Food & Dining", '#cccc00000000'),
-                ("Mortgage", '#4e4e9a9a0606'),
-                ("Rent", '#c4c4a0a00000'),
-                ("Medical", '#34346565a4a4'),
-                ("Educational", '#757550507b7b'),
-                ("Donation", '#060698209a9a'),
-                ("Credit Card", '#d3d3d7d7cfcf'),
-                ("Gifts", '#555557575353'),
-                ("Books", '#efef29292929'),
-                ("Online Services", '#8a8ae2e23434'),
-                ("Insurance", '#fcfce9e94f4f'),
-                ("Auto & Transport", '#72729f9fcfcf'),
-                ("Home", '#adad7f7fa8a8'),
-                ("Gas & Fuel", '#3434e2e2e2e2'),
-                ("Electronics", '#eeeeeeeeecec'),
-            ]
-
-        for category in categories:
-            self.add(Category(category[0], category[1]))
-
     def get_interval_bills(self, start=None, end=None, paid=None):
         """
         """
diff --git a/src/lib/dal.py b/src/lib/dal.py
index a0cb5e0..85a3d3d 100644
--- a/src/lib/dal.py
+++ b/src/lib/dal.py
@@ -21,9 +21,17 @@ from xdg.BaseDirectory import *
 class DAL(object):
 
     def __init__(self):
-        data_dir = os.path.join(xdg_data_home, APPNAME)
+
+        new_setup = False
+
+        # This is where the database file should live
+        data_dir = os.path.join(xdg_data_home, APPNAME.lower())
+        # Check that this is a new setup and there's no database yet.
         if not os.path.isdir(data_dir):
+            # Create the directory where the database file will live
             os.mkdir(data_dir)
+            # Safe to assume that this is a new setup.
+            new_setup = True
 
         self.engine = create_engine('sqlite:///%s' % os.path.join(data_dir, DB_NAME))
         self.Session = sessionmaker(bind=self.engine)
@@ -31,6 +39,10 @@ class DAL(object):
         # Creates all database tables
         Bill.metadata.create_all(self.engine)
 
+        # Let us make sure to create some default values.
+        if new_setup:
+            self.default_categories()
+
     def add(self, dbobject):
 
         session = self.Session()
@@ -113,3 +125,26 @@ class DAL(object):
             print str(e)
         finally:
             session.close()
+
+    def default_categories(self):
+        categories = [
+                (_("Utilities"), '#f8bcffff0db4'),
+                (_("Food & Dining"), '#cccc00000000'),
+                (_("Mortgage"), '#4e4e9a9a0606'),
+                (_("Rent"), '#c4c4a0a00000'),
+                (_("Medical"), '#34346565a4a4'),
+                (_("Educational"), '#757550507b7b'),
+                (_("Donations"), '#060698209a9a'),
+                (_("Credit Card"), '#d3d3d7d7cfcf'),
+                (_("Gifts"), '#555557575353'),
+                (_("Books"), '#efef29292929'),
+                (_("Online Services"), '#8a8ae2e23434'),
+                (_("Insurance"), '#fcfce9e94f4f'),
+                (_("Auto & Transport"), '#72729f9fcfcf'),
+                (_("Home"), '#adad7f7fa8a8'),
+                (_("Gas & Fuel"), '#3434e2e2e2e2'),
+                (_("Electronics"), '#eeeeeeeeecec'),
+            ]
+
+        for category in categories:
+            self.add(Category(category[0], category[1]))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]