/gramps/gen/lib/childref.py

https://github.com/gramps-project/gramps · Python · 228 lines · 102 code · 17 blank · 109 comment · 11 complexity · 95c7294e958cc34bf8dc3c633e197d3b MD5 · raw file

  1. #
  2. # Gramps - a GTK+/GNOME based genealogy program
  3. #
  4. # Copyright (C) 2006-2007 Donald N. Allingham
  5. # Copyright (C) 2010 Michiel D. Nauta
  6. # Copyright (C) 2011 Tim G L Lyons
  7. # Copyright (C) 2013 Doug Blank <doug.blank@gmail.com>
  8. # Copyright (C) 2017 Nick Hall
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. """
  25. Child Reference class for Gramps.
  26. """
  27. #-------------------------------------------------------------------------
  28. #
  29. # Gramps modules
  30. #
  31. #-------------------------------------------------------------------------
  32. from .secondaryobj import SecondaryObject
  33. from .privacybase import PrivacyBase
  34. from .citationbase import CitationBase
  35. from .notebase import NoteBase
  36. from .refbase import RefBase
  37. from .childreftype import ChildRefType
  38. from .const import IDENTICAL, EQUAL, DIFFERENT
  39. from ..const import GRAMPS_LOCALE as glocale
  40. _ = glocale.translation.gettext
  41. #-------------------------------------------------------------------------
  42. #
  43. # Person References for Person/Family
  44. #
  45. #-------------------------------------------------------------------------
  46. class ChildRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase):
  47. """
  48. Person reference class.
  49. This class is for keeping information about how the person relates
  50. to another person from the database, if not through family.
  51. Examples would be: godparent, friend, etc.
  52. """
  53. def __init__(self, source=None):
  54. PrivacyBase.__init__(self, source)
  55. CitationBase.__init__(self, source)
  56. NoteBase.__init__(self, source)
  57. RefBase.__init__(self, source)
  58. if source:
  59. self.frel = ChildRefType(source.frel)
  60. self.mrel = ChildRefType(source.mrel)
  61. else:
  62. self.frel = ChildRefType()
  63. self.mrel = ChildRefType()
  64. def serialize(self):
  65. """
  66. Convert the object to a serialized tuple of data.
  67. """
  68. return (PrivacyBase.serialize(self),
  69. CitationBase.serialize(self),
  70. NoteBase.serialize(self),
  71. RefBase.serialize(self),
  72. self.frel.serialize(),
  73. self.mrel.serialize())
  74. def unserialize(self, data):
  75. """
  76. Convert a serialized tuple of data to an object.
  77. """
  78. (privacy, citation_list, note_list, ref, frel, mrel) = data
  79. PrivacyBase.unserialize(self, privacy)
  80. CitationBase.unserialize(self, citation_list)
  81. NoteBase.unserialize(self, note_list)
  82. RefBase.unserialize(self, ref)
  83. self.frel = ChildRefType()
  84. self.frel.unserialize(frel)
  85. self.mrel = ChildRefType()
  86. self.mrel.unserialize(mrel)
  87. return self
  88. @classmethod
  89. def get_schema(cls):
  90. """
  91. Returns the JSON Schema for this class.
  92. :returns: Returns a dict containing the schema.
  93. :rtype: dict
  94. """
  95. return {
  96. "type": "object",
  97. "title": _("Child Reference"),
  98. "properties": {
  99. "_class": {"enum": [cls.__name__]},
  100. "private": {"type": "boolean",
  101. "title": _("Private")},
  102. "citation_list": {"type": "array",
  103. "items": {"type": "string",
  104. "maxLength": 50},
  105. "title": _("Citations")},
  106. "note_list": {"type": "array",
  107. "items": {"type": "string",
  108. "maxLength": 50},
  109. "title": _("Notes")},
  110. "ref": {"type": "string",
  111. "maxLength": 50,
  112. "title": _("Handle")},
  113. "frel": ChildRefType.get_schema(),
  114. "mrel": ChildRefType.get_schema()
  115. }
  116. }
  117. def get_text_data_list(self):
  118. """
  119. Return the list of all textual attributes of the object.
  120. :returns: Returns the list of all textual attributes of the object.
  121. :rtype: list
  122. """
  123. return [str(self.frel), str(self.mrel)]
  124. def get_text_data_child_list(self):
  125. """
  126. Return the list of child objects that may carry textual data.
  127. :returns: Returns the list of child objects that may carry textual data.
  128. :rtype: list
  129. """
  130. return []
  131. def get_note_child_list(self):
  132. """
  133. Return the list of child secondary objects that may refer notes.
  134. :returns: Returns the list of child secondary child objects that may
  135. refer notes.
  136. :rtype: list
  137. """
  138. return []
  139. def get_referenced_handles(self):
  140. """
  141. Return the list of (classname, handle) tuples for all directly
  142. referenced primary objects.
  143. :returns: List of (classname, handle) tuples for referenced objects.
  144. :rtype: list
  145. """
  146. ret = self.get_referenced_note_handles() + \
  147. self.get_referenced_citation_handles()
  148. if self.ref:
  149. ret += [('Person', self.ref)]
  150. return ret
  151. def get_handle_referents(self):
  152. """
  153. Return the list of child objects which may, directly or through their
  154. children, reference primary objects..
  155. :returns: Returns the list of objects referencing primary objects.
  156. :rtype: list
  157. """
  158. return []
  159. def is_equivalent(self, other):
  160. """
  161. Return if this child reference is equivalent, that is agrees in hlink,
  162. to other.
  163. :param other: The childref to compare this one to.
  164. :type other: ChildRef
  165. :returns: Constant indicating degree of equivalence.
  166. :rtype: int
  167. """
  168. if self.ref != other.ref:
  169. return DIFFERENT
  170. else:
  171. if self.is_equal(other):
  172. return IDENTICAL
  173. else:
  174. return EQUAL
  175. def merge(self, acquisition):
  176. """
  177. Merge the content of acquisition into this child reference.
  178. Lost: hlink, mrel and frel of acquisition.
  179. :param acquisition: The childref to merge with the present childref.
  180. :type acquisition: ChildRef
  181. """
  182. self._merge_privacy(acquisition)
  183. self._merge_note_list(acquisition)
  184. self._merge_citation_list(acquisition)
  185. if (self.mrel != acquisition.mrel) or (self.frel != acquisition.frel):
  186. if self.mrel == ChildRefType.UNKNOWN:
  187. self.set_mother_relation(acquisition.mrel)
  188. if self.frel == ChildRefType.UNKNOWN:
  189. self.set_father_relation(acquisition.frel)
  190. def set_mother_relation(self, rel):
  191. """Set relation between the person and mother."""
  192. self.mrel.set(rel)
  193. def get_mother_relation(self):
  194. """Return the relation between the person and mother."""
  195. return self.mrel
  196. def set_father_relation(self, frel):
  197. """Set relation between the person and father."""
  198. self.frel.set(frel)
  199. def get_father_relation(self):
  200. """Return the relation between the person and father."""
  201. return self.frel