I added utility functions for this type of thing which we should probably move to something like dimagi utils and then call there.