2016-01-11 17:19:06 +00:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2016-02-26 12:00:16 +00:00
|
|
|
from app import db
|
2016-01-11 17:19:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Should I use SQLAlchemyError?
|
|
|
|
|
class DAOException(SQLAlchemyError):
|
|
|
|
|
pass
|
2016-02-26 12:00:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DAOClass(object):
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = None
|
|
|
|
|
|
2016-02-26 17:11:30 +00:00
|
|
|
def create_instance(self, inst, _commit=True):
|
2016-02-26 12:00:16 +00:00
|
|
|
db.session.add(inst)
|
2016-02-26 17:11:30 +00:00
|
|
|
if _commit:
|
|
|
|
|
db.session.commit()
|
2016-02-26 12:00:16 +00:00
|
|
|
|
2016-02-29 11:50:43 +00:00
|
|
|
def update_instance(self, inst, update_dict, _commit=True):
|
2016-02-26 12:00:16 +00:00
|
|
|
# Make sure the id is not included in the update_dict
|
|
|
|
|
update_dict.pop('id')
|
|
|
|
|
self.Meta.model.query.filter_by(id=inst.id).update(update_dict)
|
2016-02-29 11:50:43 +00:00
|
|
|
if _commit:
|
|
|
|
|
db.session.commit()
|
2016-02-26 12:00:16 +00:00
|
|
|
|
2016-02-29 11:50:43 +00:00
|
|
|
def delete_instance(self, inst, _commit=True):
|
2016-02-26 12:00:16 +00:00
|
|
|
db.session.delete(inst)
|
2016-02-29 11:50:43 +00:00
|
|
|
if _commit:
|
|
|
|
|
db.session.commit()
|