Beispiel #1
0
 def get_by_system(system_class: str, parser: Dict[str, Any]) \
         -> List[Entity]:
     if system_class not in g.classes:
         raise InvalidSystemClassError
     return [
         Entity(row)
         for row in Db.get_by_system_class(system_class, parser)
     ]
Beispiel #2
0
 def get_by_system_class(classes: str, parser: Dict[str,
                                                    Any]) -> List[Entity]:
     parameters = {
         'class': tuple(classes if isinstance(classes, list) else [classes])
     }
     sql = Db.build_sql(nodes=True, aliases=True) + """
         WHERE e.system_class
         IN %(class)s {clause}
         GROUP BY e.id ORDER BY {order} {sort};""".format(
         clause=Filter.get_filter(parameters=parameters, parser=parser),
         order=', '.join(parser['column']),
         sort=parser['sort'])
     g.cursor.execute(sql, parameters)
     return [Entity(row) for row in g.cursor.fetchall()]
Beispiel #3
0
 def get_by_class_code_api(code: Union[str, List[str]],
                           parser: Dict[str, Any]) -> List[Entity]:
     parameters = {
         'codes': tuple(code if isinstance(code, list) else [code])
     }
     sql = Db.build_sql(nodes=True) + """
         WHERE class_code IN %(codes)s {clause} 
         GROUP BY e.id
         ORDER BY {order} {sort};""".format(
         clause=Filter.get_filter(parameters=parameters, parser=parser),
         order=', '.join(parser['column']),
         sort=parser['sort'])
     g.cursor.execute(sql, parameters)
     return [Entity(row) for row in g.cursor.fetchall()]
Beispiel #4
0
def search(data: dict[str, Any]) -> list[Entity]:
    if not data['term']:
        return []
    if 'person' in data['classes'] \
            or 'place' in data['classes'] \
            or 'group' in data['classes']:
        data['classes'].append('appellation')
    entities = []
    for row in Db.search(data['term'], data['classes'], data['desc'],
                         data['own'], current_user.id):
        if row['openatlas_class_name'] == 'appellation':
            entity = Link.get_linked_entity_safe(row['id'], 'P1', True)
            if entity.class_.name not in data['classes']:
                continue
        else:
            entity = Entity(row)
        if entity and check_dates(entity, data):
            entities.append(entity)
    return list({d.id: d for d in entities}.values())  # Remove duplicates
Beispiel #5
0
    def get_all_nodes():
        """Get and return all type and place nodes"""
        sql = """
            SELECT
                e.id,
                e.name,
                e.class_code,
                e.description,
                e.system_type,
                e.created,
                e.modified,
                es.id AS super_id,
                COUNT(l2.id) AS count,
                COUNT(lp.id) AS count_property
            FROM model.entity e                

            -- get super
            LEFT JOIN model.link l ON e.id = l.domain_id AND l.property_code = %(property_code)s
            LEFT JOIN model.entity es ON l.range_id = es.id

            -- get count
            LEFT JOIN model.link l2 ON e.id = l2.range_id AND l2.property_code IN ('P2', 'P89')
            LEFT JOIN model.link_property lp ON e.id = lp.range_id AND lp.property_code = 'P2'
            
            WHERE e.class_code = %(class_code)s
                AND (e.system_type IS NULL OR e.system_type != 'place location')
            GROUP BY e.id, es.id                        
            ORDER BY e.name;"""
        g.cursor.execute(sql, {'class_code': 'E55', 'property_code': 'P127'})
        types = g.cursor.fetchall()
        g.cursor.execute(sql, {'class_code': 'E53', 'property_code': 'P89'})
        places = g.cursor.fetchall()
        nodes = OrderedDict()
        for row in types + places:
            node = Entity(row)
            nodes[node.id] = node
            node.count = row.count + row.count_property
            node.count_subs = 0
            node.subs = []
            node.root = [row.super_id] if row.super_id else []
        NodeMapper.populate_subs(nodes)
        return nodes
Beispiel #6
0
def search(form: FlaskForm) -> ValuesView[Entity]:
    if not form.term.data:
        return {}.values()
    classes = form.classes.data
    if 'person' in classes:
        classes.append('actor_appellation')
    if 'place' in classes:
        classes.append('appellation')

    # Repopulate date fields with autocompleted values
    from_date = Date.form_to_datetime64(form.begin_year.data,
                                        form.begin_month.data,
                                        form.begin_day.data)
    to_date = Date.form_to_datetime64(form.end_year.data,
                                      form.end_month.data,
                                      form.end_day.data,
                                      to_date=True)
    if from_date:
        string = str(from_date)
        if string.startswith('-') or string.startswith('0000'):
            string = string[1:]
        parts = string.split('-')
        form.begin_month.raw_data = None
        form.begin_day.raw_data = None
        form.begin_month.data = int(parts[1])
        form.begin_day.data = int(parts[2])
    if to_date:
        string = str(to_date)
        if string.startswith('-') or string.startswith('0000'):
            string = string[1:]  # pragma: no cover
        parts = string.split('-')
        form.end_month.raw_data = None
        form.end_day.raw_data = None
        form.end_month.data = int(parts[1])
        form.end_day.data = int(parts[2])

    # Get search results
    entities = []
    for row in Db.search(form.term.data, tuple(form.classes.data),
                         form.desc.data, form.own.data, current_user.id):
        if row['system_class'] == 'actor_appellation':  # If found in actor alias
            entity = Link.get_linked_entity(row['id'], 'P131', True)
        elif row['system_class'] == 'appellation':  # If found in place alias
            entity = Link.get_linked_entity(row['id'], 'P1', True)
        else:
            entity = Entity(row)

        if not entity:  # pragma: no cover
            continue

        if not from_date and not to_date:
            entities.append(entity)
            continue

        # Date criteria present but entity has no dates
        if not entity.begin_from and not entity.begin_to and not entity.end_from \
                and not entity.end_to:
            if form.include_dateless.data:  # Include dateless entities
                entities.append(entity)
            continue

        # Check date criteria
        dates = [
            entity.begin_from, entity.begin_to, entity.end_from, entity.end_to
        ]
        begin_check_ok = False
        if not from_date:
            begin_check_ok = True  # pragma: no cover
        else:
            for date in dates:
                if date and date >= from_date:
                    begin_check_ok = True

        end_check_ok = False
        if not to_date:
            end_check_ok = True  # pragma: no cover
        else:
            for date in dates:
                if date and date <= to_date:
                    end_check_ok = True

        if begin_check_ok and end_check_ok:
            entities.append(entity)
    return {d.id: d
            for d in entities}.values()  # Remove duplicates before returning
Beispiel #7
0
 def get_by_view(code_: str, parser: dict[str, Any]) -> list[Entity]:
     if code_ not in g.view_class_mapping:
         raise InvalidCodeError  # pragma: no cover
     sys_class = Db.get_by_system_class(g.view_class_mapping[code_], parser)
     return [Entity(row) for row in sys_class]
Beispiel #8
0
 def get_by_class(class_code: str, parser: dict[str, Any]) -> list[Entity]:
     if class_code not in g.cidoc_classes:
         raise InvalidCidocClassCode  # pragma: no cover
     return [Entity(row) for row in Db.get_by_class_code(class_code, parser)]