X-Git-Url: http://git.datanom.net/pwp.git/blobdiff_plain/e5424f290a1b4f0f9b200e720d19091b63ed3eb2..fc01a3eb12731e8e2512e77b426ab5b33503edd1:/app/DB/baseorm.py diff --git a/app/DB/baseorm.py b/app/DB/baseorm.py index ef7962e..1d34536 100644 --- a/app/DB/baseorm.py +++ b/app/DB/baseorm.py @@ -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)