def delete(territory_id): with get_cursor() as cursor: territory = TerritoryCRUD.get(cursor, territory_id) state = StateCRUD.get(cursor, territory.state_id) if len(state.representations ) == 1 and not state.representations[0].name: its_territories = TerritoryCRUD.get_by_state( cursor, territory.state_id) if len(its_territories) == 1: StateCRUD.delete(cursor, state.state_id) return { "deleted_state": state.state_id, "deleted_territory": territory_id } TerritoryCRUD.delete(cursor, territory_id) return {"deleted_state": None, "deleted_territory": territory_id}
def __merge_with_cursor(cursor, to_merge_id, sovereign_state_id): assert isinstance(to_merge_id, int) assert isinstance(sovereign_state_id, int) to_merge = StateCRUD.get(cursor, to_merge_id) sovereign = StateCRUD.get(cursor, sovereign_state_id) if to_merge.validity_start < sovereign.validity_start or to_merge.validity_end > sovereign.validity_end: raise MergeStateConflictException( f'The state to merge period {(to_merge.validity_start, to_merge.validity_end)} overflows the period of the sovereign state {(sovereign.validity_start, sovereign.validity_end)}', [ t.territory_id for t in TerritoryCRUD.get_by_state(cursor, to_merge_id) ]) territories_to_change = TerritoryCRUD.get_by_state(cursor, to_merge_id) for territory in territories_to_change: # Color integrity check its_color = territory.color if not its_color: candidate_colors = [ r.color for r in to_merge.representations if r.period_intersects( territory.validity_start, territory.validity_end) ] # All representation matching with the territory have similar color => territory color can be determined (else too complex nothing is done) if all( colours_roughly_equal(candidate_colors[0], c) for c in candidate_colors[1:]): its_color = candidate_colors[0] if its_color: sovereign_color = [ r.color for r in sovereign.representations if r.period_intersects( territory.validity_start, territory.validity_end) ] if all( colours_roughly_equal(c, its_color) for c in sovereign_color): territory.color = None # Destination color is different => set the territory color to preserve it else: territory.color = its_color TerritoryCRUD.edit(cursor, territory, change_color=True) territory.state_id = sovereign_state_id TerritoryCRUD.edit(cursor, territory, change_state_id=True) StateCRUD.delete(cursor, to_merge_id) return sovereign_state_id
def delete(state_id): assert isinstance(state_id, int) with get_cursor() as cursor: return StateCRUD.delete(cursor, state_id)