Moved translation funtion to get_label/get_labels

This commit is contained in:
Doug Blank 2015-12-27 09:37:01 -05:00
parent 57e349a280
commit e0c6468c77
9 changed files with 59 additions and 12 deletions

View File

@ -681,7 +681,7 @@ class Date(object):
"newyear": self.newyear} "newyear": self.newyear}
@classmethod @classmethod
def from_struct(cls, struct): def from_struct(cls, struct, full=False):
""" """
Given a struct data representation, return a serialized object. Given a struct data representation, return a serialized object.
@ -695,7 +695,7 @@ class Date(object):
struct.get("text", default.text), struct.get("text", default.text),
struct.get("sortval", default.sortval), struct.get("sortval", default.sortval),
struct.get("newyear", default.newyear)) struct.get("newyear", default.newyear))
if retval == (0, 0, 0, (0, 0, 0, False), '', 0, 0): if not full and retval == (0, 0, 0, (0, 0, 0, False), '', 0, 0):
return None return None
else: else:
return retval return retval

View File

@ -152,6 +152,25 @@ class Event(CitationBase, NoteBase, MediaBase, AttributeBase,
"tag_list": TagBase.to_struct(self), "tag_list": TagBase.to_struct(self),
"private": self.private} "private": self.private}
@classmethod
def get_labels(cls, _):
return {
"_class": _("Event"),
"handle": _("Handle"),
"gramps_id": _("Gramps ID"),
"type": _("Type"),
"date": _("Date"),
"description": _("Description"),
"place": _("Place"),
"citation_list": _("Citations"),
"note_list": _("Notes"),
"media_list": _("Media"),
"attribute_list": _("Attributes"),
"change": _("Last changed"),
"tag_list": _("Tags"),
"private": _("Private"),
}
@classmethod @classmethod
def from_struct(cls, struct): def from_struct(cls, struct):
""" """

View File

@ -108,6 +108,16 @@ class EventRef(PrivacyBase, NoteBase, AttributeBase, RefBase,
"role": self.__role.to_struct() "role": self.__role.to_struct()
} }
@classmethod
def get_labels(cls, _):
return {
"private": _("Private"),
"note_list": _("Notes"),
"attribute_list": _("Attributes"),
"ref": _("Event"),
"role": _("Role"),
}
@classmethod @classmethod
def from_struct(cls, struct): def from_struct(cls, struct):
""" """

View File

@ -191,7 +191,7 @@ class Name(SecondaryObject, PrivacyBase, SurnameBase, CitationBase, NoteBase,
struct.get("famnick", default.famnick)) struct.get("famnick", default.famnick))
@classmethod @classmethod
def get_labels(cls): def get_labels(cls, _):
return { return {
"_class": _("Name"), "_class": _("Name"),
"private": _("Private"), "private": _("Private"),

View File

@ -208,7 +208,7 @@ class Person(CitationBase, NoteBase, AttributeBase, MediaBase,
} }
@classmethod @classmethod
def get_labels(cls): def get_labels(cls, _):
return { return {
"_class": _("Person"), "_class": _("Person"),
"handle": _("Handle"), "handle": _("Handle"),

View File

@ -71,7 +71,7 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
else: else:
self.gramps_id = None self.gramps_id = None
def get_label(self, field): def get_label(self, field, _):
""" """
Get the associated label given a field name of this object. Get the associated label given a field name of this object.
""" """
@ -82,7 +82,7 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
path = getattr(path, part) path = getattr(path, part)
else: else:
path = path[int(part)] path = path[int(part)]
labels = path.get_labels() labels = path.get_labels(_)
if chain[-1] in labels: if chain[-1] in labels:
return labels[chain[-1]] return labels[chain[-1]]
else: else:

View File

@ -50,3 +50,21 @@ class SecondaryObject(BaseObject):
Should be overwritten by objects that inherit from this class. Should be overwritten by objects that inherit from this class.
""" """
pass pass
def get_label(self, field, _):
"""
Get the associated label given a field name of this object.
"""
chain = field.split(".")
path = self
for part in chain[:-1]:
if hasattr(path, part):
path = getattr(path, part)
else:
path = path[int(part)]
labels = path.get_labels(_)
if chain[-1] in labels:
return labels[chain[-1]]
else:
raise Exception("%s has no such label: '%s'" % (self, field))

View File

@ -308,12 +308,13 @@ class Struct(object):
add_func = self.db._tables[name]["add_func"] add_func = self.db._tables[name]["add_func"]
add_func(new_obj, trans) add_func(new_obj, trans)
def from_struct(struct): def from_struct(self):
""" """
Given a struct with metadata, create a Gramps object. Given a struct with metadata, create a Gramps object.
""" """
from gramps.gen.lib import (Person, Family, Event, Source, Place, Citation, from gramps.gen.lib import (Person, Family, Event, Source, Place, Citation,
Repository, MediaObject, Note, Tag) Repository, MediaObject, Note, Tag, Date)
struct = self.struct
if isinstance(struct, dict): if isinstance(struct, dict):
if "_class" in struct.keys(): if "_class" in struct.keys():
if struct["_class"] == "Person": if struct["_class"] == "Person":
@ -336,6 +337,8 @@ class Struct(object):
return Note.create(Note.from_struct(struct)) return Note.create(Note.from_struct(struct))
elif struct["_class"] == "Tag": elif struct["_class"] == "Tag":
return Tag.create(Tag.from_struct(struct)) return Tag.create(Tag.from_struct(struct))
elif struct["_class"] == "Date":
return Date().unserialize(Date.from_struct(struct, full=True))
raise AttributeError("invalid struct: %s" % struct) raise AttributeError("invalid struct: %s" % struct)
def __str__(self): def __str__(self):

View File

@ -28,9 +28,6 @@ Surname class for Gramps.
# Gramps modules # Gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
from .secondaryobj import SecondaryObject from .secondaryobj import SecondaryObject
from .nameorigintype import NameOriginType from .nameorigintype import NameOriginType
from .const import IDENTICAL, EQUAL, DIFFERENT from .const import IDENTICAL, EQUAL, DIFFERENT
@ -102,7 +99,7 @@ class Surname(SecondaryObject):
"connector": self.connector} "connector": self.connector}
@classmethod @classmethod
def get_labels(cls): def get_labels(cls, _):
return { return {
"_class": _("Surname"), "_class": _("Surname"),
"surname": _("Surname"), "surname": _("Surname"),