import itertools from ppms import ppms from ppms.period import aggregate_periods_from_start_to_end, PeriodCreator from ppms.ressource import is_department def get_all_ressources_of_department(department_id, max_recursions= -1, _current_level=0, skip_departments=True): '''Get all children (direct+indirect) of a department You can specify the amount of recursions to perform by setting the max_recursions parameter to a fixed integer amount or -1 to drill down from top to bottom. Notes: * TCalc only drills down 1 level when aggregating. * This method only returns resources, unless skip_departments is passed as False the _current_level parameter is used for the recursion and should not be passed manually. ''' query = ppms.get_query("000646") % department_id resources = [] if max_recursions == _current_level: return [] resultset = ppms.db_select(query) for row in resultset: res_id = row[0] if is_department(res_id) and skip_departments: resources.extend(get_all_ressources_of_department(res_id, max_recursions, _current_level + 1, skip_departments=skip_departments)) else: resources.append(res_id) return resources query = """UPDATE DT468 SET DI001337 = 0, DI002454 = 0 WHERE DI001341 = 0 OR DI001340 = 0""" ppms.db_modify(query) all_resources = list(itertools.chain(*ppms.db_select("SELECT DI001218 FROM DT467"))) resources_to_aggregate = all_resources[:] # Only call the period aggregation for the lowest level children # as those will aggregate to the top anyway for resource_id in all_resources: child_resources = get_all_ressources_of_department(resource_id, skip_departments=False) for child_resource in child_resources: if child_resource in resources_to_aggregate: resources_to_aggregate.remove(resource_id) break for resource_id in resources_to_aggregate: rec_467 = ppms.search_record(467, [resource_id], ['start_period', 'end_period'], True) start_period = rec_467.start_period.get_value() end_period = rec_467.end_period.get_value() with PeriodCreator() as period: period.add(resource_id, start_period, end_period) aggregate_periods_from_start_to_end(resource_id)