So, I haven't read this closely enough to understand what it's doing fully, but since you were asking about being stupid about query performance -- database query inside nested for-loop raises a red-flag for me. Also below, in lines 249 and 252.

If it runs in reasonable time as-is, and there isn't an obvious way to un-nest the queries using related lookups, then I wouldn't worry about it.