[billreminder] Some serious re-work of how to add or edit records to the database. It should be better at editing e



commit 62982fe98b45c513675915577e5535479a3d26ee
Author: Og B. Maciel <ogmaciel gnome org>
Date:   Wed Nov 18 15:41:51 2009 -0500

    Some serious re-work of how to add or edit records to the database. It should be better at editing existing records, though I need to spend more time testing it properly. Will most likely refactor dal.py.

 src/lib/actions.py |    3 +-
 src/lib/dal.py     |   56 +++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 49 insertions(+), 10 deletions(-)
---
diff --git a/src/lib/actions.py b/src/lib/actions.py
index 8ace98e..6a87f11 100644
--- a/src/lib/actions.py
+++ b/src/lib/actions.py
@@ -130,7 +130,8 @@ class Actions(object):
         return self.dal.add(dbobject)
 
     def edit(self, dbobject):
-        return self.dal.edit(dbobject)
+        return self.dal.add(dbobject)
+        #return self.dal.edit(dbobject)
 
     def delete(self, dbobject):
         return self.dal.delete(dbobject)
diff --git a/src/lib/dal.py b/src/lib/dal.py
index 396dbe6..a320933 100644
--- a/src/lib/dal.py
+++ b/src/lib/dal.py
@@ -4,7 +4,7 @@ import os
 import sys
 
 try:
-    from sqlalchemy.orm import sessionmaker
+    from sqlalchemy.orm import sessionmaker, eagerload
     from sqlalchemy import create_engine
     from sqlalchemy.orm import sessionmaker
 except ImportError:
@@ -35,14 +35,52 @@ class DAL(object):
 
         session = self.Session()
 
-        try:
-            session.add(dbobject)
-            session.commit()
-        except Exception, e:
-            session.rollback()
-            print str(e)
-        finally:
-            session.close()
+        if isinstance(dbobject, Bill):
+            try:
+                bill = session.query(Bill).options(eagerload('category')).filter_by(id=dbobject.id).one()
+                if bill:
+                    bill.payee = dbobject.payee
+                    bill.amount = dbobject.amount
+                    bill.dueDate = dbobject.dueDate
+                    bill.notes = dbobject.notes
+                    bill.paid = dbobject.paid
+                    if dbobject.category and bill.category[0].name != dbobject.category[0].name:
+                        bill.category = []
+                        try:
+                            category = session.query(Category).filter_by(name=dbobject.category[0].name).one()
+                            bill.category.append(category)
+                        except Exception, e:
+                            print "Failed to retrieve category \"%s\" for bill \"%s\": %s" \
+                                % (dbobject.name, dbobject.category[0].name, str(e))
+                else:
+                    session.add(dbobject)
+
+                if session.dirty:
+                    session.commit()
+
+            except Exception, e:
+                session.rollback()
+                print str(e)
+            finally:
+                session.close()
+
+        elif isinstance(dbobject, Category):
+            try:
+                category = session.query(Category).filter_by(id=dbobject.id).one()
+                if category:
+                    category.name = dbobject.name
+                    category.color = dbobject.color
+                else:
+                    session.add(dbobject)
+
+                if session.dirty:
+                    session.commit()
+
+            except Exception, e:
+                session.rollback()
+                print str(e)
+            finally:
+                session.close()
 
     def edit(self, dbobject):
 



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