Esempio n. 1
0
def get_extended_attributes(
    db_handle: DbReadBase, obj: GrampsObject, args: Optional[Dict] = None
) -> Dict:
    """Get extended attributes for a GrampsObject."""
    args = args or {}
    result = {}
    do_all = False
    if "all" in args["extend"]:
        do_all = True
    if (do_all or "child_ref_list" in args["extend"]) and hasattr(
        obj, "child_ref_list"
    ):
        result["children"] = [
            db_handle.get_person_from_handle(child_ref.ref)
            for child_ref in obj.child_ref_list
        ]
    if (do_all or "citation_list" in args["extend"]) and hasattr(obj, "citation_list"):
        result["citations"] = [
            db_handle.get_citation_from_handle(handle) for handle in obj.citation_list
        ]
    if (do_all or "event_ref_list" in args["extend"]) and hasattr(
        obj, "event_ref_list"
    ):
        result["events"] = [
            db_handle.get_event_from_handle(event_ref.ref)
            for event_ref in obj.event_ref_list
        ]
    if (do_all or "media_list" in args["extend"]) and hasattr(obj, "media_list"):
        result["media"] = [
            db_handle.get_media_from_handle(media_ref.ref)
            for media_ref in obj.media_list
        ]
    if (do_all or "note_list" in args["extend"]) and hasattr(obj, "note_list"):
        result["notes"] = [
            db_handle.get_note_from_handle(handle) for handle in obj.note_list
        ]
    if (do_all or "person_ref_list" in args["extend"]) and hasattr(
        obj, "person_ref_list"
    ):
        result["people"] = [
            db_handle.get_person_from_handle(person_ref.ref)
            for person_ref in obj.person_ref_list
        ]
    if (do_all or "reporef_list" in args["extend"]) and hasattr(obj, "reporef_list"):
        result["repositories"] = [
            db_handle.get_repository_from_handle(repo_ref.ref)
            for repo_ref in obj.reporef_list
        ]
    if (do_all or "tag_list" in args["extend"]) and hasattr(obj, "tag_list"):
        result["tags"] = [
            db_handle.get_tag_from_handle(handle) for handle in obj.tag_list
        ]
    if (do_all or "backlinks" in args["extend"]) and hasattr(obj, "backlinks"):
        result["backlinks"] = {}
        for class_name, backlinks in obj.backlinks.items():
            result["backlinks"][class_name] = [
                db_handle.method("get_%s_from_handle", class_name.upper())(handle)
                for handle in backlinks
            ]
    return result
Esempio n. 2
0
def get_soundex(db_handle: DbReadBase, obj: GrampsObject,
                gramps_class_name: str) -> str:
    """Return soundex code."""
    if gramps_class_name == "Family":
        if obj.father_handle is not None:
            person = db_handle.get_person_from_handle(obj.father_handle)
        elif obj.mother_handle is not None:
            person = db_handle.get_person_from_handle(obj.mother_handle)
    else:
        person = obj
    return soundex(person.get_primary_name().get_surname())
Esempio n. 3
0
def get_person_by_handle(db_handle: DbReadBase,
                         handle: Handle) -> Union[Person, Dict]:
    """Safe get person by handle."""
    try:
        return db_handle.get_person_from_handle(handle)
    except HandleError:
        return {}
Esempio n. 4
0
def get_person_by_handle(db_handle: DbReadBase,
                         handle: Handle) -> Optional[Person]:
    """Safe get person by handle."""
    try:
        return db_handle.get_person_from_handle(handle)
    except HandleError:
        return None
Esempio n. 5
0
def get_person_profile_for_handle(
    db_handle: DbReadBase, handle: Handle, args: List, locale: GrampsLocale = glocale
) -> Union[Person, Dict]:
    """Get person profile given a handle."""
    try:
        obj = db_handle.get_person_from_handle(handle)
    except HandleError:
        return {}
    return get_person_profile_for_object(db_handle, obj, args, locale=locale)
Esempio n. 6
0
def prepare_options(db_handle: DbReadBase, args: Dict):
    """Prepare the export options."""
    options = ExportOptions()
    options.private = args["private"]
    options.living = LIVING_FILTERS[args["living"]]
    options.current_year = args["current_year"]
    options.years_after_death = args["years_after_death"]
    options.reference = args["reference"]
    options.include_individuals = int(args["include_individuals"])
    options.include_marriages = int(args["include_marriages"])
    options.include_children = int(args["include_children"])
    options.include_places = int(args["include_places"])
    options.include_witnesses = int(args["include_witnesses"])
    options.include_media = int(args["include_media"])
    options.translate_headers = int(args["translate_headers"])
    options.compression = int(args["compress"])
    if args["person"] is None:
        if args["gramps_id"] is not None or args["handle"] is not None:
            abort(422)
    else:
        if args["gramps_id"] is not None:
            gramps_id = args["gramps_id"]
            if db_handle.get_person_from_gramps_id(gramps_id) is None:
                abort(422)
        else:
            try:
                person = db_handle.get_person_from_handle(args["handle"])
            except HandleError:
                abort(422)
            gramps_id = person.gramps_id
        try:
            options.set_person_filter(args["person"], gramps_id)
        except ValueError:
            abort(422)
    if args["event"] is not None:
        try:
            options.set_event_filter(args["event"])
        except ValueError:
            abort(422)
    if args["note"] is not None:
        try:
            options.set_note_filter(args["note"])
        except ValueError:
            abort(422)
    try:
        options.set_proxy_order(args["sequence"])
    except ValueError:
        abort(422)
    if args["locale"] is not None:
        options.locale = get_locale_for_language(args["locale"])
        if options.locale is None:
            abort(422)
    return options
Esempio n. 7
0
def get_person_profile_for_handle(
    db_handle: DbReadBase,
    handle: Handle,
    with_family: bool = True,
    with_events: bool = True,
) -> Optional[Person]:
    """Get person profile given a handle."""
    try:
        obj = db_handle.get_person_from_handle(handle)
    except HandleError:
        return None
    return get_person_profile_for_object(db_handle, obj, with_family,
                                         with_events)
Esempio n. 8
0
def get_event_participants_for_handle(
    db_handle: DbReadBase,
    handle: Handle,
    locale: GrampsLocale = glocale,
) -> Dict:
    """Get event participants given a handle."""
    result = {"people": [], "families": []}
    seen = set()  # to avoid duplicates
    for class_name, backref_handle in db_handle.find_backlink_handles(
        handle, include_classes=["Person", "Family"]
    ):
        if backref_handle in seen:
            continue
        seen.add(backref_handle)
        if class_name == "Person":
            person = db_handle.get_person_from_handle(backref_handle)
            if not person:
                continue
            for event_ref in person.get_event_ref_list():
                if handle == event_ref.ref:
                    result["people"].append(
                        {
                            "role": locale.translation.sgettext(
                                event_ref.get_role().xml_str()
                            ),
                            "person": get_person_profile_for_handle(
                                db_handle, backref_handle, args=[], locale=locale
                            ),
                        }
                    )
        elif class_name == "Family":
            family = db_handle.get_family_from_handle(backref_handle)
            if not family:
                continue
            for event_ref in family.get_event_ref_list():
                if handle == event_ref.ref:
                    result["families"].append(
                        {
                            "role": locale.translation.sgettext(
                                event_ref.get_role().xml_str()
                            ),
                            "family": get_family_profile_for_handle(
                                db_handle, backref_handle, args=[], locale=locale
                            ),
                        }
                    )
    return result
Esempio n. 9
0
def get_person_filter(db_handle: DbReadBase,
                      args: Dict) -> Union[GenericFilter, None]:
    """Return the specified person filter."""
    if args["person"] is None:
        if args["gramps_id"] is not None or args["handle"] is not None:
            abort(422)
        return None

    if args["gramps_id"]:
        gramps_id = args["gramps_id"]
        if db_handle.get_person_from_gramps_id(gramps_id) is None:
            abort(422)
    else:
        try:
            person = db_handle.get_person_from_handle(args["handle"])
        except HandleError:
            abort(422)
        gramps_id = person.gramps_id

    person_filter = filters.GenericFilter()
    if args["person"] == "Descendants":
        person_filter.set_name(_("Descendants of %s") % gramps_id)
        person_filter.add_rule(
            filters.rules.person.IsDescendantOf([gramps_id, 1]))
    elif args["person"] == "DescendantFamilies":
        person_filter.set_name(_("Descendant Families of %s") % gramps_id)
        person_filter.add_rule(
            filters.rules.person.IsDescendantFamilyOf([gramps_id, 1]))
    elif args["person"] == "Ancestors":
        person_filter.set_name(_("Ancestors of %s") % gramps_id)
        person_filter.add_rule(
            filters.rules.person.IsAncestorOf([gramps_id, 1]))
    elif args["person"] == "CommonAncestor":
        person_filter.set_name(
            _("People with common ancestor with %s") % gramps_id)
        person_filter.add_rule(
            filters.rules.person.HasCommonAncestorWith([gramps_id]))
    else:
        person_filter = None
        filters.reload_custom_filters()
        for filter_class in filters.CustomFilters.get_filters("Person"):
            if args["person"] == filter_class.get_name():
                person_filter = filter_class
                break
    if person_filter is None:
        abort(422)
    return person_filter