예제 #1
0
def get_place_profile_for_object(
    db_handle: DbReadBase,
    place: Place,
    locale: GrampsLocale = glocale,
    parent_places: bool = True,
) -> Dict[str, Any]:
    """Get place profile given a Place."""
    latitude, longitude = conv_lat_lon(place.lat, place.long, format="D.D8")
    profile = {
        "gramps_id":
        place.gramps_id,
        "type":
        _format_place_type(place.get_type(), locale=locale),
        "name":
        place.get_name().value,
        "alternate_names":
        [place_name.value for place_name in place.get_alternative_names()],
        "lat":
        float(latitude) if (latitude and longitude) else None,
        "long":
        float(longitude) if (latitude and longitude) else None,
    }
    if parent_places:
        parent_places_handles = []
        _place = place
        handle = None
        while True:
            for placeref in _place.get_placeref_list():
                handle = placeref.ref
                break
            if handle is None or handle in parent_places_handles:
                break
            _place = db_handle.get_place_from_handle(handle)
            if _place is None:
                break
            parent_places_handles.append(handle)
        profile["parent_places"] = [
            get_place_profile_for_object(
                db_handle=db_handle,
                place=db_handle.get_place_from_handle(parent_place),
                locale=locale,
                parent_places=False,
            ) for parent_place in parent_places_handles
        ]
    return profile
예제 #2
0
    def __get_level0(self):
        countries = []
        with self.dbstate.db.get_place_cursor() as cursor:
            pair = cursor.first()
            while pair:
                (handle, data) = pair
                print(pair)
                place = Place()
                place.unserialize(data)
                placetype = place.get_type().value
                print(placetype)
                #                if getattr(PlaceType, place['type']).upper() == PlaceType.COUNTRY:
                if placetype == PlaceType.COUNTRY:
                    print("Country found")
                    countries.append(place)
                pair = cursor.next()
#            print(countries)
        return countries
예제 #3
0
class PlaceSidebarFilter(SidebarFilter):

    def __init__(self, dbstate, uistate, clicked):
        self.clicked_func = clicked

        self.filter_id = widgets.BasicEntry()
        self.filter_name = widgets.BasicEntry()
        self.filter_place = Place()
        self.filter_place.set_type((PlaceType.CUSTOM, ''))
        self.ptype = Gtk.ComboBox(has_entry=True)
        if dbstate.is_open():
            self.custom_types = dbstate.db.get_place_types()
        else:
            self.custom_types = []

        self.place_menu = widgets.MonitoredDataType(
            self.ptype,
            self.filter_place.set_type,
            self.filter_place.get_type,
            False, # read-only
            self.custom_types
        )
        self.filter_code = widgets.BasicEntry()
        self.filter_enclosed = widgets.PlaceEntry(dbstate, uistate, [])
        self.filter_note = widgets.BasicEntry()

        self.filter_regex = Gtk.CheckButton(label=_('Use regular expressions'))
        self.tag = Gtk.ComboBox()
        self.generic = Gtk.ComboBox()

        SidebarFilter.__init__(self, dbstate, uistate, "Place")

    def create_widget(self):
        cell = Gtk.CellRendererText()
        cell.set_property('width', self._FILTER_WIDTH)
        cell.set_property('ellipsize', self._FILTER_ELLIPSIZE)
        self.generic.pack_start(cell, True)
        self.generic.add_attribute(cell, 'text', 0)
        self.on_filters_changed('Place')

        cell = Gtk.CellRendererText()
        cell.set_property('width', self._FILTER_WIDTH)
        cell.set_property('ellipsize', self._FILTER_ELLIPSIZE)
        self.tag.pack_start(cell, True)
        self.tag.add_attribute(cell, 'text', 0)

        self.add_text_entry(_('ID'), self.filter_id)
        self.add_text_entry(_('Name'), self.filter_name)
        self.add_entry(_('Type'), self.ptype)
        self.add_text_entry(_('Code'), self.filter_code)
        self.add_text_entry(_('Enclosed By'), self.filter_enclosed)
        self.add_text_entry(_('Note'), self.filter_note)
        self.add_entry(_('Tag'), self.tag)
        self.add_filter_entry(_('Custom filter'), self.generic)
        self.add_regex_entry(self.filter_regex)

    def clear(self, obj):
        self.filter_id.set_text('')
        self.filter_name.set_text('')
        self.filter_code.set_text('')
        self.filter_enclosed.set_text('')
        self.filter_note.set_text('')
        self.ptype.get_child().set_text('')
        self.tag.set_active(0)
        self.generic.set_active(0)

    def get_filter(self):
        gid = str(self.filter_id.get_text()).strip()
        name = str(self.filter_name.get_text()).strip()
        ptype = self.filter_place.get_type().xml_str()
        code = str(self.filter_code.get_text()).strip()
        enclosed = str(self.filter_enclosed.get_text()).strip()
        note = str(self.filter_note.get_text()).strip()
        regex = self.filter_regex.get_active()
        tag = self.tag.get_active() > 0
        gen = self.generic.get_active() > 0

        empty = not (gid or name or ptype or code or enclosed or note or regex
                     or tag or gen)
        if empty:
            generic_filter = None
        else:
            generic_filter = GenericPlaceFilter()
            if gid:
                rule = RegExpIdOf([gid], use_regex=regex)
                generic_filter.add_rule(rule)

            if enclosed:
                rule = IsEnclosedBy([enclosed, '0'])
                generic_filter.add_rule(rule)

            rule = HasData([name, ptype, code], use_regex=regex)
            generic_filter.add_rule(rule)

            if note:
                rule = HasNoteRegexp([note], use_regex=regex)
                generic_filter.add_rule(rule)

            # check the Tag
            if tag:
                model = self.tag.get_model()
                node = self.tag.get_active_iter()
                attr = model.get_value(node, 0)
                rule = HasTag([attr])
                generic_filter.add_rule(rule)

            if self.generic.get_active() != 0:
                model = self.generic.get_model()
                node = self.generic.get_active_iter()
                obj = str(model.get_value(node, 0))
                rule = MatchesFilter([obj])
                generic_filter.add_rule(rule)

        return generic_filter

    def on_filters_changed(self, name_space):
        if name_space == 'Place':
            all_filter = GenericPlaceFilter()
            all_filter.set_name(_("None"))
            all_filter.add_rule(rules.place.AllPlaces([]))
            self.generic.set_model(build_filter_model('Place', [all_filter]))
            self.generic.set_active(0)

    def on_tags_changed(self, tag_list):
        """
        Update the list of tags in the tag filter.
        """
        model = Gtk.ListStore(str)
        model.append(('',))
        for tag_name in tag_list:
            model.append((tag_name,))
        self.tag.set_model(model)
        self.tag.set_active(0)