Beispiel #1
0
    def from_struct(self, struct):
        """
        Given a struct with metadata, create a Gramps object.

        self is class when called as a classmethod.
        """
        from gprime.lib import (Person, Family, Event, Source, Place, Citation,
                                Repository, Media, Note, Tag, Date)
        if isinstance(struct, dict):
            if "_class" in struct.keys():
                if struct["_class"] == "Person":
                    return Person.from_struct(struct)
                elif struct["_class"] == "Family":
                    return Family.from_struct(struct)
                elif struct["_class"] == "Event":
                    return Event.from_struct(struct)
                elif struct["_class"] == "Source":
                    return Source.from_struct(struct)
                elif struct["_class"] == "Place":
                    return Place.from_struct(struct)
                elif struct["_class"] == "Citation":
                    return Citation.from_struct(struct)
                elif struct["_class"] == "Repository":
                    return Repository.from_struct(struct)
                elif struct["_class"] == "Media":
                    return Media.from_struct(struct)
                elif struct["_class"] == "Note":
                    return Note.from_struct(struct)
                elif struct["_class"] == "Tag":
                    return Tag.from_struct(struct)
                elif struct["_class"] == "Date":
                    return Date.from_struct(struct, full=True)
        raise AttributeError("invalid struct: %s" % struct)
Beispiel #2
0
def importData(db, filename, user):
    db.disable_signals()
    try:
        with DbTxn(_("JSON import"), db, batch=True) as trans:
            with OpenFileOrStdin(filename, encoding="utf-8") as fp:
                line = fp.readline()
                while line:
                    data = json.loads(line)
                    if data["_class"] == "Person":
                        obj = Person.from_struct(data)
                        db.add_person(obj, trans)
                    elif data["_class"] == "Family":
                        obj = Family.from_struct(data)
                        db.add_family(obj, trans)
                    elif data["_class"] == "Event":
                        obj = Event.from_struct(data)
                        db.add_event(obj, trans)
                    elif data["_class"] == "Media":
                        obj = Media.from_struct(data)
                        db.add_media(obj, trans)
                    elif data["_class"] == "Repository":
                        obj = Repository.from_struct(data)
                        db.add_repository(obj, trans)
                    elif data["_class"] == "Tag":
                        obj = Tag.from_struct(data)
                        db.add_tag(obj, trans)
                    elif data["_class"] == "Source":
                        obj = Source.from_struct(data)
                        db.add_source(obj, trans)
                    elif data["_class"] == "Citation":
                        obj = Citation.from_struct(data)
                        db.add_citation(obj, trans)
                    elif data["_class"] == "Note":
                        obj = Note.from_struct(data)
                        db.add_note(obj, trans)
                    elif data["_class"] == "Place":
                        obj = Place.from_struct(data)
                        db.add_place(obj, trans)
                    else:
                        LOG.warn("ignored: " + data)
                    line = fp.readline()
    except EnvironmentError as err:
        user.notify_error(_("%s could not be opened\n") % filename, str(err))

    db.enable_signals()
    db.request_rebuild()
Beispiel #3
0
 def post(self, path):
     """
     """
     _ = self.app.get_translate_func(self.current_user)
     page = int(self.get_argument("page", 1) or 1)
     search = self.get_argument("search", "")
     if path.count("/") == 0:  # handle
         handle, action = path, "view"
     elif path.count("/") == 1:  # handle/add
         handle, action = path.split("/", 1)
     else:
         self.clear()
         self.set_status(404)
         self.finish("<html><body>No such person</body></html>")
         return
     json_data = json.loads(html.unescape(self.get_argument("json_data")))
     instance = Person.from_struct(json_data)
     update_json = self.get_argument("update_json", None)
     if update_json:
         # edit the instance
         self.update_instance(instance, update_json)
         form = PersonForm(self, instance=instance)
         form.load_data()
         self.render(
             "person.html",
             **self.get_template_dict(tview=_("person detail"),
                                      action=action,
                                      page=page,
                                      search=search,
                                      form=form))
     else:
         self.send_message("Updated person. <a href=\"FIXME\">Undo</a>")
         form = PersonForm(self, instance=instance)
         form.save()
         handle = instance.handle
         self.redirect(
             self.app.make_url("/person/%(handle)s" % {"handle": handle}))