示例#1
0
def delete(bag):
    if 'company_id' in g.session and not is_admin():
        bag['company_id'] = g.session['company_id']

    # if not is_admin() and "company_id" not in bag:
    #     raise CbsException(USER_NO_ACCESS)

    table_name = bag["type"]
    table = getattr(db, table_name)

    if table is None or not issubclass(table, (db.Base, db.CouchSync)):
        raise CbsException(TABLE_NOT_FOUND)

    if not is_admin():
        item_query = g.tran.query(table).filter_by(_deleted="infinity",
                                                   _id=bag["_id"])
        if table == db.Companies:
            item_query = item_query.filter(table._id == bag["company_id"],
                                           table.user_id == g.user.id)
            if issubclass(table, db.CompanySync):
                item_query = item_query.filter(
                    table.company_id == bag["company_id"])
        elif table == db.Companyemployees:
            item_query = item_query.filter(table.user_id == bag["user_id"])
            if issubclass(table, db.CompanySync):
                item_query = item_query.filter(
                    table.company_id == bag["company_id"])
        else:
            item_query = item_query.first()
            if item_query is None:
                raise CbsException(USER_NO_ACCESS)

    pg_db = PostgresDatabase()
    _id, _rev = pg_db.remove(bag["_id"], bag["_rev"])
    return {"ok": True, "id": _id, "rev": _rev}
示例#2
0
def remove(bag):
    table_name = bag["type"]
    table = getattr(db, table_name)

    if table is None or not issubclass(table, (db.Base, db.CouchSync)):
        raise CbsException(TABLE_NOT_FOUND)

    if not is_admin():
        item_query = g.tran.query(table).filter_by(_deleted="infinity",
                                                   _id=bag["_id"])
        item_query = item_query.first()
        if item_query is None:
            raise CbsException(USER_NO_ACCESS)

    pg_db = PostgresDatabase()
    _id, _rev = pg_db.remove(bag["_id"], bag["_rev"])
    return {"ok": True, "id": _id, "rev": _rev}