def populate_subs(nodes: Dict[int, Node]) -> None: forms = {} for row in Db.get_web_forms(): forms[row['id']] = { 'id': row['id'], 'name': row['name'], 'label': g.classes[row['name']].label if row['name'] in g.classes else uc_first(_(row['name'].replace('_', ''))), 'extendable': row['extendable'] } hierarchies = {row['id']: row for row in Db.get_hierarchies()} for node in nodes.values(): if node.root: super_ = nodes[node.root[0]] super_.subs.append(node.id) node.root = Node.get_root_path(nodes, node, node.root[0], node.root) node.standard = False node.locked = nodes[node.root[0]].locked else: hierarchy = hierarchies[node.id] node.value_type = hierarchy['value_type'] node.directional = hierarchy['directional'] node.multiple = hierarchy['multiple'] node.standard = hierarchy['standard'] node.locked = hierarchy['locked'] node.forms = { form_id: forms[form_id] for form_id in hierarchy['form_ids'] }
def update_hierarchy(node: Node, form: FlaskForm) -> None: multiple = False if node.multiple or (hasattr(form, 'multiple') and form.multiple and form.multiple.data): multiple = True Db.update_hierarchy({ 'id': node.id, 'name': form.name.data, 'multiple': multiple }) Node.add_forms_to_hierarchy(node, form)
def insert_hierarchy(node: Node, form: FlaskForm, value_type: bool) -> None: multiple = False if value_type or (hasattr(form, 'multiple') and form.multiple and form.multiple.data): multiple = True Db.insert_hierarchy({ 'id': node.id, 'name': node.name, 'multiple': multiple, 'value_type': value_type }) Node.add_forms_to_hierarchy(node, form)
def get_all_nodes() -> Dict[int, Node]: """ Get and return all type and place nodes""" nodes = {} for row in Db.get_nodes('type', 'P127') + Db.get_nodes( 'administrative_unit', 'P89'): node = Node(row) nodes[node.id] = node node.count = row['count'] if row['count'] else row['count_property'] node.count_subs = 0 node.subs = [] node.locked = False node.root = [row['super_id']] if row['super_id'] else [] Node.populate_subs(nodes) return nodes
def move_entities(old_node: Node, new_type_id: int, checkbox_values: str) -> None: root = g.nodes[old_node.root[-1]] entity_ids = ast.literal_eval(checkbox_values) delete_ids = [] if new_type_id: # A new type was selected if root.multiple: cleaned_entity_ids = [] for entity in Entity.get_by_ids(entity_ids, nodes=True): if any(node.id == int(new_type_id) for node in entity.nodes): delete_ids.append( entity.id) # If already linked add to delete ids else: cleaned_entity_ids.append(entity.id) entity_ids = cleaned_entity_ids if entity_ids: data = { 'old_type_id': old_node.id, 'new_type_id': new_type_id, 'entity_ids': tuple(entity_ids) } if root.name in app.config['PROPERTY_TYPES']: Db.move_link_type(data) else: Db.move_entity_type(data) else: delete_ids = entity_ids # No new type was selected so delete all links if delete_ids: if root.name in app.config['PROPERTY_TYPES']: Db.remove_link_type(old_node.id, delete_ids) else: Db.remove_entity_type(old_node.id, delete_ids)
def get_form_choices(root: Optional[Node] = None) -> List[Tuple[int, str]]: choices = [] for row in Db.get_form_choices(): if g.classes[row['name']].view != 'type' and (not root or row['id'] not in root.forms): choices.append((row['id'], g.classes[row['name']].label)) return choices
def remove_by_entity_and_node(entity_id: int, node_id: int) -> None: Db.remove_by_entity_and_node(entity_id, node_id)
def remove_form_from_hierarchy(form_id: int, hierarchy_id: int) -> None: Db.remove_form_from_hierarchy(form_id, hierarchy_id)
def get_form_count(root_node: Node, form_id: int) -> Optional[int]: # Check if nodes are linked to entities before offering to remove a node from form node_ids = Node.get_all_sub_ids(root_node) if not node_ids: return return Db.get_form_count(form_id, node_ids)
def add_forms_to_hierarchy(node: Node, form: FlaskForm) -> None: Db.add_form_to_hierarchy(node.id, form.forms.data)
def get_nodes_for_form(form_name: str) -> Dict[int, Node]: return {id_: g.nodes[id_] for id_ in Db.get_nodes_for_form(form_name)}