Esempio n. 1
0
 def delete_all(cls):
     try:
         db_session.query(cls).delete()
         db_session.commit()
     except Exception as e:
         current_app.logger.info(e)
         db_session.rollback()
     return
Esempio n. 2
0
 def save(self):
     try:
         db_session.add(self)
         db_session.commit()
     except Exception as e:
         current_app.logger.error(e)
         db_session.rollback()
     return self
Esempio n. 3
0
 def get_by(cls, **kw):
     try:
         result = cls.query.filter_by(**kw).first()
         db_session.commit()
         return result
     except Exception as e:
         current_app.logger.info(e)
         db_session.rollback()
         return None
Esempio n. 4
0
 def get(cls, id):
     try:
         result = cls.query.get(id)
         db_session.commit()
         return result
     except Exception as e:
         current_app.logger.info(e)
         db_session.rollback()
         return None
Esempio n. 5
0
 def first(cls):
     try:
         result = db_session.query(cls).first()
         db_session.commit()
         return result
     except Exception as e:
         current_app.logger.info(e)
         db_session.rollback()
         return None
Esempio n. 6
0
 def last(cls):
     try:
         result = db_session.query(cls).order_by(
             cls.created_at.desc()).first()
         db_session.commit()
         return result
     except Exception as e:
         current_app.logger.info(e)
         db_session.rollback()
         return None
Esempio n. 7
0
 def delete(self) -> bool:
     try:
         db_session.delete(self)
         db_session.commit()
     except Exception as e:
         current_app.logger.error(e)
         db_session.rollback()
         return False
     else:
         return True
Esempio n. 8
0
 def create(cls, **kw):
     r = cls(**kw)
     try:
         db_session.add(r)
         db_session.commit()
     except Exception as e:
         current_app.logger.error(e)
         db_session.rollback()
         return None
     else:
         return r
Esempio n. 9
0
 def get_or_create(cls, **kw):
     r = cls.get_by(**kw)
     if not r:
         r = cls(**kw)
         try:
             db_session.add(r)
             db_session.commit()
         except Exception as e:
             current_app.logger.info(e)
             db_session.rollback()
     return r
Esempio n. 10
0
 def update(self, **kwargs):
     '''
         Updates the model's fields and returns the model instance if sucessful
     '''
     try:
         db_session.query(
             self.__class__).filter_by(id=self.id).update(kwargs)
         db_session.commit()
     except Exception as e:
         current_app.logger.error(e)
         db_session.rollback()
         return None
     else:
         return self