def entity_hard_delete(kls, entity_id, session=None): if not session: session = get_session() try: with session.begin(subtransactions=True): query = model_query(kls, session) entity = query.filter_by(id=entity_id).first() if entity is None: raise exc.NotFound( _("%(name)s %(id)s not found") % { 'name': kls.__name__, 'id': entity_id }) session.delete(entity) except db_exc.DBReferenceError as re: raise exc.DBReferenceError(object_name=kls.__name__, value=re.constraint, key=re.key) except db_exc.DBConnectionError: raise exc.DBConnectionError() except db_exc.ColumnError: raise exc.ColumnError() except db_exc.DBDeadlock: raise exc.DBDeadLock() except db_exc.DBInvalidUnicodeParameter: raise exc.DBInvalidUnicodeParameter()
def entity_create(kls, values, session=None): entity = kls() entity.update(values.copy()) if not session: session = get_session() try: with session.begin(subtransactions=True): session.add(entity) session.expunge(entity) except db_exc.DBDuplicateEntry as de: raise exc.DBDuplicateEntry(object_name=kls.__name__, value=de.value) except db_exc.DBReferenceError as re: raise exc.DBReferenceError(object_name=kls.__name__, value=re.constraint, key=re.key) except db_exc.DBConnectionError: raise exc.DBConnectionError() except db_exc.ColumnError: raise exc.ColumnError() except db_exc.DBDeadlock: raise exc.DBDeadLock() except db_exc.DBInvalidUnicodeParameter: raise exc.DBInvalidUnicodeParameter return entity
def __entity_get(kls, entity_id, session): try: query = model_query(kls, session) return query.filter_by(id=entity_id).first() except db_exc.DBConnectionError: raise exc.DBConnectionError() except db_exc.ColumnError: raise exc.ColumnError() except db_exc.DBDeadlock: raise exc.DBDeadLock() except db_exc.DBInvalidUnicodeParameter: raise exc.DBInvalidUnicodeParameter()
def model_query(model, session=None): """Query helper. :param model: base model to query """ session = session or get_session() try: query = session.query(model) except db_exc.DBConnectionError: raise exc.DBConnectionError() except db_exc.ColumnError: raise exc.ColumnError() except db_exc.DBDeadlock: raise exc.DBDeadLock() except db_exc.DBInvalidUnicodeParameter: raise exc.DBInvalidUnicodeParameter() return query
def entity_update(kls, entity_id, values, session=None, filter_non_public=False): if not session: session = get_session() try: with session.begin(subtransactions=True): entity = __entity_get(kls, entity_id, session) if entity is None: raise exc.NotFound(_("%(name)s %(id)s not found") % {'name': kls.__name__, 'id': entity_id}) values_copy = values.copy() values_copy["id"] = entity_id entity.update(values_copy) session.add(entity) session.expunge(entity) except db_exc.DBDuplicateEntry as de: raise exc.DBDuplicateEntry(object_name=kls.__name__, value=de.value) except db_exc.DBReferenceError as re: raise exc.DBReferenceError(object_name=kls.__name__, value=re.constraint, key=re.key) except db_exc.DBConnectionError: raise exc.DBConnectionError() except db_exc.ColumnError: raise exc.ColumnError() except db_exc.DBDeadlock: raise exc.DBDeadLock() except db_exc.DBInvalidUnicodeParameter: raise exc.DBInvalidUnicodeParameter session = get_session() entity = __entity_get(kls, entity_id, session) if filter_non_public: entity = _filter_non_public_fields(entity, entity._public_fields) return entity