Ejemplo n.º 1
0
 def __init__(self, database, namespace, locale=glocale):
     """Initialize sort class."""
     self.database = database
     self.locale = locale
     self.name_display = NameDisplay(xlocale=self.locale)
     self.place_display = PlaceDisplay()
     self.namespace = namespace
     self.query_method = self.database.method("get_%s_from_handle",
                                              self.namespace)
Ejemplo n.º 2
0
class Report(object):
    """
    The Report base class.  This is a base class for generating
    customized reports.  It cannot be used as is, but it can be easily
    sub-classed to create a functional report generator.
    """

    def __init__(self, database, options_class, user):
        self.database = database
        self.options_class = options_class

        self.doc = options_class.get_document()

        creator = database.get_researcher().get_name()
        self.doc.set_creator(creator)

        output = options_class.get_output()
        if output:
            self.standalone = True
            self.doc.open(options_class.get_output())
        else:
            self.standalone = False

    def begin_report(self):
        pass

    def set_locale(self, language):
        """
        Set the translator to one selected with
        stdoptions.add_localization_option().
        """
        if language == GrampsLocale.DEFAULT_TRANSLATION_STR:
            language = None
        locale = GrampsLocale(lang=language)
        self._ = locale.translation.gettext
        self._get_date = locale.get_date
        self._get_type = locale.get_type
        self._dd = locale.date_displayer
        self._name_display = NameDisplay(locale) # a legacy/historical name
        self._name_display.set_name_format(self.database.name_formats)
        fmt_default = config.get('preferences.name-format')
        self._name_display.set_default_format(fmt_default)
        return locale

    def write_report(self):
        pass

    def end_report(self):
        if self.standalone:
            self.doc.close()
Ejemplo n.º 3
0
 def set_locale(self, language):
     """
     Set the translator to one selected with
     stdoptions.add_localization_option().
     """
     if language == GrampsLocale.DEFAULT_TRANSLATION_STR:
         language = None
     locale = GrampsLocale(lang=language)
     self._ = locale.translation.gettext
     self._get_date = locale.get_date
     self._get_type = locale.get_type
     self._ldd = locale.date_displayer
     self._name_display = NameDisplay(locale)  # a legacy/historical name
     self._name_display.set_name_format(self.database.name_formats)
     fmt_default = config.get('preferences.name-format')
     self._name_display.set_default_format(fmt_default)
     return locale
Ejemplo n.º 4
0
 def set_locale(self, language):
     """
     Set the translator to one selected with
     stdoptions.add_localization_option().
     """
     if language == GrampsLocale.DEFAULT_TRANSLATION_STR:
         language = None
     locale = GrampsLocale(lang=language)
     self._ = locale.translation.gettext
     self._get_date = locale.get_date
     self._get_type = locale.get_type
     self._dd = locale.date_displayer
     self._name_display = NameDisplay(locale) # a legacy/historical name
     self._name_display.set_name_format(self.database.name_formats)
     fmt_default = config.get('preferences.name-format')
     self._name_display.set_default_format(fmt_default)
     return locale
Ejemplo n.º 5
0
"""Gramps utility functions."""

from typing import List, Optional

from gramps.gen.const import GRAMPS_LOCALE
from gramps.gen.db.base import DbReadBase
from gramps.gen.display.name import NameDisplay
from gramps.gen.errors import HandleError
from gramps.gen.lib import Event, Person
from gramps.gen.lib.primaryobj import BasicPrimaryObject as GrampsObject

from gramps_webapi.types import GrampsId, Handle

nd = NameDisplay()
dd = GRAMPS_LOCALE.date_displayer


def get_event_date_from_handle(db: DbReadBase,
                               handle: Handle) -> Optional[str]:
    """Return a formatted date for the event."""
    try:
        date = db.get_event_from_handle(handle).get_date_object()
    except AttributeError:
        return None
    return dd.display(date) or None


def get_event_place_from_handle(db: DbReadBase,
                                handle: Handle) -> Optional[GrampsId]:
    """Get an event's place."""
    return get_event_place_grampsid(db, db.get_event_from_handle(handle))
Ejemplo n.º 6
0
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

# Gramps imports:
from gramps.gen.lib.person import Person
from gramps.gen.display.name import NameDisplay

# Gramps Connect imports:
from .forms import Form

# Globals:
nd = NameDisplay().display


class PersonForm(Form):
    """
    A form for listing, viewing, and editing a Person object.
    """
    _class = Person
    view = "person"
    tview = "People"

    # Fields for editor:
    edit_fields = [
        "primary_name.type",
        "primary_name.title",
        "primary_name.nick",
Ejemplo n.º 7
0
def get_person_profile_for_object(db_handle: DbReadBase,
                                  person: Person,
                                  args: List,
                                  locale: GrampsLocale = glocale) -> Person:
    """Get person profile given a Person."""
    options = []
    if "all" in args or "ratings" in args:
        options.append("ratings")
    name_display = NameDisplay(xlocale=locale)
    birth, birth_event = get_birth_profile(db_handle,
                                           person,
                                           args=options,
                                           locale=locale)
    death, death_event = get_death_profile(db_handle,
                                           person,
                                           args=options,
                                           locale=locale)
    if "all" in args or "age" in args:
        options.append("age")
        if birth_event is not None:
            birth["age"] = locale.translation.sgettext("0 days")
            if death_event is not None:
                death["age"] = (Span(birth_event.date,
                                     death_event.date).format(
                                         precision=3,
                                         dlocale=locale).strip("()"))
    profile = {
        "handle": person.handle,
        "gramps_id": person.gramps_id,
        "sex": get_sex_profile(person),
        "birth": birth,
        "death": death,
        "name_given": name_display.display_given(person),
        "name_surname": person.primary_name.get_surname(),
    }
    if "all" in args or "span" in args:
        options.append("span")
    if "all" in args or "events" in args:
        options.append("events")
        if "age" not in args and "all" not in args:
            birth_event = None
        profile["events"] = [
            get_event_profile_for_handle(
                db_handle,
                event_ref.ref,
                args=options,
                base_event=birth_event,
                label="age",
                locale=locale,
                role=locale.translation.sgettext(
                    event_ref.get_role().xml_str()),
            ) for event_ref in person.event_ref_list
        ]
    if "all" in args or "families" in args:
        primary_parent_family_handle = person.get_main_parents_family_handle()
        profile["primary_parent_family"] = get_family_profile_for_handle(
            db_handle, primary_parent_family_handle, options, locale=locale)
        profile["other_parent_families"] = []
        for handle in person.parent_family_list:
            if handle != primary_parent_family_handle:
                profile["other_parent_families"].append(
                    get_family_profile_for_handle(db_handle,
                                                  handle,
                                                  options,
                                                  locale=locale))
        profile["families"] = [
            get_family_profile_for_handle(db_handle,
                                          handle,
                                          options,
                                          locale=locale)
            for handle in person.family_list
        ]
    return profile
Ejemplo n.º 8
0
class Sort:
    """Class for extracting sort keys."""
    def __init__(self, database, namespace, locale=glocale):
        """Initialize sort class."""
        self.database = database
        self.locale = locale
        self.name_display = NameDisplay(xlocale=self.locale)
        self.place_display = PlaceDisplay()
        self.namespace = namespace
        self.query_method = self.database.method("get_%s_from_handle",
                                                 self.namespace)

    # Generic object key methods

    def by_id_key(self, handle):
        """Compare by Gramps Id."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.gramps_id)

    def by_change_key(self, handle):
        """Compare by last change."""
        obj = self.query_method(handle)
        return obj.change

    def by_private_key(self, handle):
        """Compare based on private attribute."""
        obj = self.query_method(handle)
        return int(obj.private)

    def by_date_key(self, handle):
        """Compare by date."""
        obj = self.query_method(handle)
        return obj.get_date_object().get_sort_value()

    def by_type_key(self, handle):
        """Compare by type."""
        obj = self.query_method(handle)
        return self.locale.sort_key(
            self.locale.translation.sgettext(str(obj.get_type())))

    # Specific object key methods

    def by_person_surname_key(self, handle):
        """Compare by surname, if equal uses given name and suffix."""
        obj = self.query_method(handle)
        name = obj.get_primary_name()
        fsn = name.get_surname()
        ffn = name.get_first_name()
        fsu = name.get_suffix()
        return self.locale.sort_key(fsn + ffn + fsu)

    def by_person_sorted_name_key(self, handle):
        """Compare by displayed names."""
        obj = self.query_method(handle)
        return self.locale.sort_key(self.name_display.sorted(obj))

    def by_person_soundex_key(self, handle):
        """Compare by soundex."""
        obj = self.query_method(handle)
        return soundex(obj.get_primary_name().get_surname())

    def by_person_birthdate_key(self, handle):
        """Compare by birth date, if equal sorts by name."""
        obj = self.query_method(handle)
        birth = get_birth_or_fallback(self.database, obj)
        if birth:
            date = birth.get_date_object()
        else:
            date = Date()
        return "%08d" % date.get_sort_value() + str(
            self.by_person_surname_key(handle))

    def by_person_deathdate_key(self, handle):
        """Compare by death date, if equal sorts by name."""
        obj = self.query_method(handle)
        death = get_death_or_fallback(self.database, obj)
        if death:
            date = death.get_date_object()
        else:
            date = Date()
        return "%08d" % date.get_sort_value() + str(
            self.by_person_surname_key(handle))

    def by_person_gender_key(self, handle):
        """Compare by gender."""
        obj = self.query_method(handle)
        return obj.gender

    def by_family_surname_key(self, handle):
        """Compare by family surname, if equal uses given and suffix."""
        obj = self.query_method(handle)
        if obj.father_handle is not None:
            person = self.database.get_person_from_handle(obj.father_handle)
        elif obj.mother_handle is not None:
            person = self.database.get_person_from_handle(obj.mother_handle)
        name = person.get_primary_name()
        fsn = name.get_surname()
        ffn = name.get_first_name()
        fsu = name.get_suffix()
        return self.locale.sort_key(fsn + ffn + fsu)

    def by_family_soundex_key(self, handle):
        """Compare by family soundex."""
        obj = self.query_method(handle)
        if obj.father_handle is not None:
            person = self.database.get_person_from_handle(obj.father_handle)
        elif obj.mother_handle is not None:
            person = self.database.get_person_from_handle(obj.mother_handle)
        return soundex(person.get_primary_name().get_surname())

    def by_family_type_key(self, handle):
        """Compare by relationship type."""
        obj = self.query_method(handle)
        return self.locale.sort_key(
            self.locale.translation.sgettext(str(obj.type)))

    def by_event_place_key(self, handle):
        """Compare by event place."""
        obj = self.query_method(handle)
        return self.locale.sort_key(
            self.place_display.display_event(self.database, obj))

    def by_event_description_key(self, handle):
        """Compare by event description."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.get_description())

    def by_place_title_key(self, handle):
        """Compare by place title."""
        obj = self.query_method(handle)
        return self.locale.sort_key(
            self.place_display.display(self.database, obj))

    def by_place_latitude_key(self, handle):
        """Compare by place latitude."""
        obj = self.query_method(handle)
        return obj.lat

    def by_place_longitude_key(self, handle):
        """Compare by place longitude."""
        obj = self.query_method(handle)
        return obj.long

    def by_citation_confidence_key(self, handle):
        """Compare by citation confidence."""
        obj = self.query_method(handle)
        return obj.confidence

    def by_source_title_key(self, handle):
        """Compare by source title."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.title)

    def by_source_author_key(self, handle):
        """Compare by source title."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.author)

    def by_source_pubinfo_key(self, handle):
        """Compare by source publication info."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.pubinfo)

    def by_source_abbrev_key(self, handle):
        """Compare by source abbreviation."""
        obj = self.query_method(handle)
        return obj.abbrev

    def by_repository_name_key(self, handle):
        """Compare by repository name."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.name)

    def by_media_title_key(self, handle):
        """Compare by media title."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.desc)

    def by_media_path_key(self, handle):
        """Compare by media path."""
        obj = self.query_method(handle)
        return obj.path

    def by_media_mimetype_key(self, handle):
        """Compare by media mime type."""
        obj = self.query_method(handle)
        return obj.mime

    def by_note_text_key(self, handle):
        """Compare by note text."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.text.string)

    def by_tag_name_key(self, handle):
        """Compare by tag name."""
        obj = self.query_method(handle)
        return self.locale.sort_key(obj.name)

    def by_tag_color_key(self, handle):
        """Compare by tag color."""
        obj = self.query_method(handle)
        return obj.color

    def by_tag_priority_key(self, handle):
        """Compare by tag priority."""
        obj = self.query_method(handle)
        return obj.priority