def update_entry(entry, *, commit=True, **kwargs): session = Session() for key, value in kwargs.items(): setattr(entry, key, value) if commit: session.commit() return entry
def create_entry(model_class, *, commit=True, **kwargs): session = Session() entry = model_class(**kwargs) session.add(entry) if commit: session.commit() return entry
def db_session_context(): session = Session() try: yield session except Exception: session.rollback() raise else: try: session.commit() except Exception: session.rollback() raise
def delete_entry(model_class, uid, *, commit=True, **kwargs): session = Session() session.query(model_class).filter_by(uid=uid, **kwargs).delete() if commit: session.commit()