示例#1
0
 def log_instance_deletion(mapper: Mapper, connection: Connection,
                           target: Base) -> None:
     controller.log(
         "info",
         (f"User '{getattr(current_user, 'name', 'admin')}' DELETED "
          f"{target.type} '{target.name}'."),
     )
示例#2
0
 def log_instance_creation(target: Base, args: tuple, kwargs: dict) -> None:
     if "type" not in target.__dict__ or "log" in target.type:
         return
     controller.log(
         "info",
         (f"User '{getattr(current_user, 'name', 'admin')}' CREATED "
          f"{target.__dict__['type']} '{kwargs['name']}'"),
     )
示例#3
0
 def decorated_function(*args: Any, **kwargs: Any) -> Any:
     if not current_user.is_authenticated:
         controller.log(
             "warning",
             (f"Unauthorized {request.method} request from "
              f"'{request.remote_addr}' calling the endpoint '{request.url}'"
              ),
         )
         abort(403)
     else:
         return function(*args, **kwargs)
示例#4
0
 def log_instance_update(mapper: Mapper, connection: Connection,
                         target: Base) -> None:
     state, changes = inspect(target), []
     for attr in state.attrs:
         hist = state.get_history(attr.key, True)
         if not hist.has_changes() or attr.key in dont_track_changes:
             continue
         changes.append(f"{attr.key}: {hist.deleted} => {hist.added}")
     if changes:
         controller.log(
             "info",
             (f"User '{getattr(current_user, 'name', 'admin')}' UPDATED "
              f"{target.type} '{target.name}' ({' | '.join(changes)})."),
         )
示例#5
0
 def log_instance_update(mapper: Mapper, connection: Connection,
                         target: Base) -> None:
     state, changelog = inspect(target), []
     for attr in state.attrs:
         if attr.key in private_properties or attr.key in dont_track_changes:
             continue
         hist = state.get_history(attr.key, True)
         if not hist.has_changes():
             continue
         change = f"{attr.key}: "
         if type(getattr(target, attr.key)) == InstrumentedList:
             if hist.deleted:
                 change += f"DELETED: {hist.deleted}"
             if hist.added:
                 change += f"{' / ' if hist.deleted else ''}ADDED: {hist.added}"
         else:
             change += (f"'{hist.deleted[0] if hist.deleted else None}' => "
                        f"'{hist.added[0] if hist.added else None}'")
         changelog.append(change)
     if changelog:
         name, changes = getattr(target, "name",
                                 target.id), " | ".join(changelog)
         controller.log("info",
                        f"UPDATE: {target.type} '{name}': ({changes})")
示例#6
0
def test_create_logs(user_client: FlaskClient) -> None:
    number_of_logs = len(fetch_all("Changelog"))
    for i in range(10):
        controller.log("warning", str(i))
        Session.commit()
    assert len(fetch_all("Changelog")) == number_of_logs + 10
示例#7
0
 def log_instance_deletion(mapper: Mapper, connection: Connection,
                           target: Base) -> None:
     name = getattr(target, "name", target.id)
     controller.log("info", f"DELETION: {target.type} '{name}'")
示例#8
0
 def log_instance_creation(target: Base, args: tuple, kwargs: dict) -> None:
     if "type" not in target.__dict__ or "log" in target.type:
         return
     controller.log(
         "info", f"CREATION: {target.__dict__['type']} '{kwargs['name']}'")