def flattened_queryset_timeblocks(queryset, start_key, end_key): from limbo.timeblock.logic import TimeBlock queryset = queryset.order_by(start_key) timeblocks = [] current_tb = None for obj in queryset: start = model_attribute(obj, start_key) end = model_attribute(obj, end_key) if not end: end = datetime.datetime.now() timeblock = TimeBlock(start, end) if current_tb: if timeblock.overlaps(current_tb): current_tb += timeblock else: timeblocks.append(current_tb) current_tb = timeblock else: current_tb = timeblock if current_tb: timeblocks.append(current_tb) return timeblocks