Model repeat writer refactor

Refactor the writer, similar to what was done in https://github.com/dimagi/Vellum/pull/284

Best reviewed commit-by-commit.