Altering data from the flds column in the notes table every time it is accessed

I’m writing an addon that needs to alter the data from the flds column (in bulk, I don’t want to parse it) in the notes table in the db every time it is accessed, dependent on whether that row has a certain value in the flags column before returning it. I don’t want to actually alter the database. I would want to do this every time it is accessed: by the reviewer, the editor, the browser etc… .

Is there a single part of the rust code that that does this every time, and how would I go about monkey patching that? Or is there a filter/hook I could use? If there was a way of doing it in python, that would be great as I have pretty much no experience in rust.

What are you trying to do exactly? Maybe there is a simpler way to do it.

I don’t think you can monkey-patch Rust code, but maybe the following hooks will be useful:

These hooks alone don’t cover all accesses to the notes, but they are the closer that I could find.

You can then update the note in the hooks using something like this:

note = mw.col.get_note(1643586438839)
if note.flags == 1:
    note["Front"] = "foo"

update_note() and note_will_flush() are probably not useful if you don’t want to write to the DB (and if you’re making frequent changes, it would be good to avoid writing to the DB, as you’d end up having to sync changes too frequently). But the other hooks @abdo suggested sound like a good starting point, based on the information you’ve provided so far.


Sorry I don’t think I was clear enough, basically, I want to filter/change the data before it is passed to the frontend. So every time the flds column is accessed, some function is performed on the result before passing it on. I think [this] code is ran when a note is accessed.

class Note(DeprecatedNamesMixin):
    def _load_from_backend_note(self, note: notes_pb2.Note) -> None:
        self.fields = list(note.fields)

note.fields seems to be of type List[String] but I just want the raw string with the \x1f separators.

The issue is, I can’t find where the note.fields attribute is defined so that I can monkey patch it. It says it’s in notes_pb2 but I don’t understand where to find that file.

I would not recommend going down that path. If this is infrequently changing data the user will want stored in their notes, then it’s better written into the notes either in bulk, or using the note_will_flush() hook. If this is frequently changing data intended only for display, and should not be accessible in searches and so on, then you should instrument the routines that display the data instead of modifying the note itself.

1 Like

def get_cell(self, index: QModelIndex) -> Cell:
    self.get_row(index).cells[index.column()].text = """example()"""

    return self.get_row(index).cells[index.column()]

aqt.browser.table.model.DataModel.get_cell = wrap(aqt.browser.table.model.DataModel.get_cell, get_cell)