Beispiel #1
0
 def test(self):
     family = db.get_family_from_gramps_id("F0001")
     s = Struct(family.to_struct(), db)
     self.assertEqual(s["gramps_id"], "F0001")
     s["gramps_id"] = "TEST"
     self.assertEqual(s["gramps_id"], "TEST")
     self.assertEqual(s.father_handle.primary_name.first_name, "Allen Carl")
     s["father_handle.primary_name.first_name"] = "Edward"
     self.assertEqual(s["father_handle.primary_name.first_name"], "Edward")
def event_table(form, user, action, link=None, **kwargs):
    retval = ""
    has_data = False
    cssid = "tab-events"
    table = Table(form, "tab_table")
    event = Event()
    eventref = EventRef()
    table.set_columns(
        ("", 11),
        (event.get_label("description", form._), 19),
        (event.get_label("type", form._), 10),
        (event.get_label("gramps_id", form._),11),
        (event.get_label("date", form._), 20),
        (event.get_label("place", form._), 19),
        (eventref.get_label("role", form._), 10),
    )
    s = Struct.wrap(form.instance, form.database)
    count = 0
    for event_ref in s.event_ref_list: # eventrefs
        table.append_row(event_ref.ref.description,
                         event_ref.ref.type.string,
                         event_ref.ref.gramps_id,
                         event_ref.ref.date.from_struct(),
                         event_ref.ref.place.name.value,
                         event_ref.role.string)
        has_data = True
        count += 1
    retval += """<div style="background-color: lightgray; padding: 2px 0px 0px 2px"/>"""
    if action == "view":
        retval += make_icon_button(form._("Add New Event"), (link % kwargs).replace("$act", "add"), icon="+") # )
        retval += make_icon_button(form._("Add Existing Event"), (link % kwargs).replace("$act", "share"), icon="p") # )
    else:
        retval += """&nbsp;""" # to keep tabs same height
    retval += """</div>"""
    retval += table.get_html()
    #if act == "view":
        #count = 1
        #retval = retval.replace("{{", """<div style="background-color: lightgray; padding: 2px 0px 0px 2px">""")
        #retval = retval.replace("}}", """</div>""")
        #for (djevent, event_ref) in event_list:
        #    item = form.instance.__class__.__name__.lower()
        #    retval = retval.replace("[[x%d]]" % count, make_icon_button("x", "/%s/%s/remove/eventref/%d" % (item, form.instance.handle, count)))
        #    retval = retval.replace("[[^%d]]" % count, make_icon_button("^", "/%s/%s/up/eventref/%d" % (item, form.instance.handle, count)))
        #    retval = retval.replace("[[v%d]]" % count, make_icon_button("v", "/%s/%s/down/eventref/%d" % (item, form.instance.handle, count)))
        #    count += 1
    if has_data:
        retval += """ <SCRIPT LANGUAGE="JavaScript">setHasData("%s", 1)</SCRIPT>\n""" % cssid
    return retval
Beispiel #3
0
def importData(dbase, filename, user):
    """Function called by Gramps to import data on persons in CSV format."""
    dbase.disable_signals()
    try:
        with DbTxn(_("JSON import"), dbase, batch=True) as trans:
            with OpenFileOrStdin(filename, encoding="utf-8") as fp:
                line = fp.readline()
                while line:
                    json = ast.literal_eval(line)
                    obj = Struct.from_struct(json)
                    if json["_class"] == "Person":
                        dbase.add_person(obj, trans)
                    elif json["_class"] == "Family":
                        dbase.add_family(obj, trans)
                    elif json["_class"] == "Event":
                        dbase.add_event(obj, trans)
                    elif json["_class"] == "MediaObject":
                        dbase.add_object(obj, trans)
                    elif json["_class"] == "Repository":
                        dbase.add_repository(obj, trans)
                    elif json["_class"] == "Tag":
                        dbase.add_tag(obj, trans)
                    elif json["_class"] == "Source":
                        dbase.add_source(obj, trans)
                    elif json["_class"] == "Citation":
                        dbase.add_citation(obj, trans)
                    elif json["_class"] == "Note":
                        dbase.add_note(obj, trans)
                    elif json["_class"] == "Place":
                        dbase.add_place(obj, trans)
                    else:
                        LOG.warn("ignored: " + json)
                    line = fp.readline()
    except EnvironmentError as err:
        user.notify_error(_("%s could not be opened\n") % filename, str(err))

    dbase.enable_signals()
    dbase.request_rebuild()
Beispiel #4
0
def importData(dbase, filename, user):
    """Function called by Gramps to import data on persons in CSV format."""
    dbase.disable_signals()
    try:
        with DbTxn(_("JSON import"), dbase, batch=True) as trans:
            with OpenFileOrStdin(filename, encoding="utf-8") as fp:
                line = fp.readline()
                while line:
                    json = ast.literal_eval(line)
                    obj = Struct.instance_from_struct(json)
                    if json["_class"] == "Person":
                        dbase.add_person(obj, trans)
                    elif json["_class"] == "Family":
                        dbase.add_family(obj, trans)
                    elif json["_class"] == "Event":
                        dbase.add_event(obj, trans)
                    elif json["_class"] == "Media":
                        dbase.add_media(obj, trans)
                    elif json["_class"] == "Repository":
                        dbase.add_repository(obj, trans)
                    elif json["_class"] == "Tag":
                        dbase.add_tag(obj, trans)
                    elif json["_class"] == "Source":
                        dbase.add_source(obj, trans)
                    elif json["_class"] == "Citation":
                        dbase.add_citation(obj, trans)
                    elif json["_class"] == "Note":
                        dbase.add_note(obj, trans)
                    elif json["_class"] == "Place":
                        dbase.add_place(obj, trans)
                    else:
                        LOG.warn("ignored: " + json)
                    line = fp.readline()
    except EnvironmentError as err:
        user.notify_error(_("%s could not be opened\n") % filename, str(err))

    dbase.enable_signals()
    dbase.request_rebuild()
Beispiel #5
0
 def do_query(self, items, table):
     """
     Perform the query on the items in the named table.
     """
     # table: a class that has .row(1, 2, 3, ...)
     with self.database.get_transaction_class()("QueryQuickview",
                                                self.database,
                                                batch=True) as trans:
         ROWNUM = 0
         env = self.make_env()
         for item in items:
             if item is None:
                 continue
             row = []
             row_env = []
             # "col[0]" in WHERE clause will return first column of selection:
             env["col"] = row_env
             env["ROWNUM"] = ROWNUM
             env["object"] = item
             struct = Struct(item.to_struct(), self.database)
             env.set_struct(struct)
             for col in self.columns:
                 try:
                     value = eval(col, env)
                 except:
                     value = None
                 row.append(value)
                 # allow col[#] reference:
                 row_env.append(value)
                 # an alias?
                 if col in self.aliases:
                     env[self.aliases[col]] = value
             # Should we include this row?
             if self.where:
                 try:
                     result = eval(self.where, env)
                 except:
                     continue
             else:
                 if self.action in ["DELETE", "UPDATE"]:
                     result = True
                 else:
                     result = any([col != None
                                   for col in row])  # are they all None?
             # If result, then append the row
             if result:
                 if (self.limit is None) or (self.limit[0] <= ROWNUM <
                                             self.limit[1]):
                     if self.action == "SELECT":
                         if not self.flat:
                             # Join by rows:
                             products = []
                             columns = []
                             count = 0
                             for col in row:
                                 if ((isinstance(col, Struct)
                                      and isinstance(col.struct, list)
                                      and len(col.struct) > 0)
                                         or (isinstance(col, list)
                                             and len(col) > 0)):
                                     products.append(col)
                                     columns.append(count)
                                 count += 1
                             if len(products) > 0:
                                 current = self.clean(row, self.columns)
                                 for items in itertools.product(*products):
                                     for i in range(len(items)):
                                         current[
                                             columns[i]] = self.stringify(
                                                 items[i])
                                     table.row(
                                         *current,
                                         link=(item.__class__.__name__,
                                               item.handle))
                                     self.select += 1
                             else:
                                 table.row(*self.clean(row, self.columns),
                                           link=(item.__class__.__name__,
                                                 item.handle))
                                 self.select += 1
                         else:
                             table.row(*self.clean(row, self.columns),
                                       link=(item.__class__.__name__,
                                             item.handle))
                             self.select += 1
                     elif self.action == "UPDATE":
                         # update table set col=val, col=val where expr;
                         table.row(*self.clean(row, self.columns),
                                   link=(item.__class__.__name__,
                                         item.handle))
                         self.select += 1
                         for i in range(len(self.setcolumns)):
                             struct.setitem(self.setcolumns[i],
                                            eval(self.values[i], env),
                                            trans=trans)
                     elif self.action == "DELETE":
                         table.row(*self.clean(row, self.columns))
                         self.select += 1
                         self.database.remove_instance(item, trans)
                     else:
                         raise AttributeError("unknown command: '%s'",
                                              self.action)
                 ROWNUM += 1
                 if (self.limit is not None) and (ROWNUM >= self.limit[1]):
                     break
 def do_query(self, items, table):
     """
     Perform the query on the items in the named table.
     """
     # table: a class that has .row(1, 2, 3, ...)
     with self.database.get_transaction_class()("QueryQuickview", self.database, batch=True) as trans:
         ROWNUM = 0
         env = self.make_env()
         for item in items:
             if item is None:
                 continue
             row = []
             row_env = []
             # "col[0]" in WHERE clause will return first column of selection:
             env["col"] = row_env
             env["ROWNUM"] = ROWNUM
             env["object"] = item
             struct = Struct(item.to_struct(), self.database)
             env.set_struct(struct)
             for col in self.columns:
                 try:
                     value = eval(col, env)
                 except:
                     value = None
                 row.append(value)
                 # allow col[#] reference:
                 row_env.append(value)
                 # an alias?
                 if col in self.aliases:
                     env[self.aliases[col]] = value
             # Should we include this row?
             if self.where:
                 try:
                     result = eval(self.where, env)
                 except:
                     continue
             else:
                 if self.action in ["DELETE", "UPDATE"]:
                     result = True
                 else:
                     result = any([col != None for col in row]) # are they all None?
             # If result, then append the row
             if result:
                 if (self.limit is None) or (self.limit[0] <= ROWNUM < self.limit[1]):
                     if self.action == "SELECT":
                         if not self.flat:
                             # Join by rows:
                             products = []
                             columns = []
                             count = 0
                             for col in row:
                                 if ((isinstance(col, Struct) and isinstance(col.struct, list) and len(col.struct) > 0) or
                                     (isinstance(col, list) and len(col) > 0)):
                                     products.append(col)
                                     columns.append(count)
                                 count += 1
                             if len(products) > 0:
                                 current = self.clean(row, self.columns)
                                 for items in itertools.product(*products):
                                     for i in range(len(items)):
                                         current[columns[i]] = self.stringify(items[i])
                                     table.row(*current, link=(item.__class__.__name__, item.handle))
                                     self.select += 1
                             else:
                                 table.row(*self.clean(row, self.columns), link=(item.__class__.__name__, item.handle))
                                 self.select += 1
                         else:
                             table.row(*self.clean(row, self.columns), link=(item.__class__.__name__, item.handle))
                             self.select += 1
                     elif self.action == "UPDATE":
                         # update table set col=val, col=val where expr;
                         table.row(*self.clean(row, self.columns), link=(item.__class__.__name__, item.handle))
                         self.select += 1
                         for i in range(len(self.setcolumns)):
                             struct.setitem(self.setcolumns[i], eval(self.values[i], env), trans=trans)
                     elif self.action == "DELETE":
                         table.row(*self.clean(row, self.columns))
                         self.select += 1
                         self.database.remove_instance(item, trans)
                     else:
                         raise AttributeError("unknown command: '%s'", self.action)
                 ROWNUM += 1
                 if (self.limit is not None) and (ROWNUM >= self.limit[1]):
                     break