]> git.datanom.net - pwp.git/blobdiff - app/DB/baseorm.py
Half way through migration away from sqlalchemy
[pwp.git] / app / DB / baseorm.py
index ef7962e8b659f8f66ba8b50e845fa1e1633f8caa..1d34536455cadbd9ecd2b09b35733fb77d9f6a9d 100644 (file)
@@ -1,6 +1,9 @@
 import abc
 
-class OrmSubject(metaclass=abc.ABCMeta):
+class QueryException(Exception):
+    pass
+
+class OrmSubject(object, metaclass=abc.ABCMeta):
     @abc.abstractmethod
     def addObserver(self, ormObserver):
         pass
@@ -13,7 +16,7 @@ class OrmSubject(metaclass=abc.ABCMeta):
     def notify(self, subject):
         pass
 
-class Observer(metaclass=abc.ABCMeta):
+class Observer(object, metaclass=abc.ABCMeta):
     @abc.abstractmethod
     def update(self, subject):
         pass
@@ -22,6 +25,7 @@ class BaseOrm(OrmSubject):
 
     def __init__(self):
         self.__observers = []
+        self.__oldValues = {}
 
     def addObserver(self, ormObserver):
         print("Added: {0}".format(ormObserver))
@@ -34,20 +38,44 @@ class BaseOrm(OrmSubject):
         for observer in self.__observers:
             observer.update(object)
 
+    @classmethod
+    def query(cls, **kwargs):
+        objects = []
+        from app import db
+        if kwargs:
+            if hasattr(cls, '__tablename__'):
+                table = cls.__tablename__
+            else:
+                table = cls.__name__
+            o = db.query(table, **kwargs)
+            if table.lower() == 'user':
+                for user in o:
+                    objects.append(cls(user[1], user[2], user[3], user[4], user[0]))
+            elif table.lower() == 'portfolio':
+                for portfolio in o:
+                    user = db.query('user', id = portfolio[2])[0]
+                    objects.append(cls(portfolio[1], user, portfolio[3], portfolio[4], portfolio[0]))
+        else:
+            raise QueryException("{0}: Missing at least one query parameter".format(cls))
+        return objects
+    
+    def commit(self):
+        self.notify(self)
+        self.__oldValues = {}
+        
+    def rollback(self):
+        for k, v in self.__oldValues.items():
+            setattr(self, k, v) 
+        self.__oldValues = {}
+    
     @property
     def id(self):
         return self.__id
 
     @id.setter
     def id(self, id):
-        notify = True
         try:
-            old = self.id
-            notify = False
-        except AttributeError as e:
+            self.__oldValues['id'] = self.id
+        except AttributeError:
             pass
-#            print(e)
-#            notify = False
         self.__id = id
-        if notify:
-            self.notify(self)
This page took 0.044357 seconds and 5 git commands to generate.