Ejemplo n.º 1
0
    def add(self, first, second, relation_type, **kwargs):
        """Add a new relation between given first and second records."""
        self._validate_relation_type(relation_type)
        self._validate_relation_between_records(
            first=first, second=second, relation_name=relation_type.name
        )

        sr = SiblingsRelation(relation_type)
        sr.add(first_pid=first.pid, second_pid=second.pid)

        # check for allowed relation metadata (e.g. `note`)
        has_allowed_metadata = any(
            [kwargs.get(metadata) for metadata in self.allowed_metadata]
        )

        if has_allowed_metadata:
            # filter and keep only allowed metadata
            allowed = {
                k: v for k, v in kwargs.items() if k in self.allowed_metadata
            }
            # store relation metadata in the first record
            RecordRelationsExtraMetadata.add_extra_metadata_to(
                first,
                relation_type.name,
                second.pid.pid_value,
                second.pid.pid_type,
                **allowed,
            )
        return first
Ejemplo n.º 2
0
    def get(self):
        """Get all relations of this record, adding any relations metadata."""
        relations = {}

        for relation_type in current_app.config["PARENT_CHILD_RELATION_TYPES"]:
            pcr = ParentChildRelation(relation_type)
            name = relation_type.name

            # self.record is a child
            for parent_pid in pcr.parents_of(self.record.pid):
                child_pid = self.record.pid
                r = self._build_child(parent_pid, child_pid, name)

                relations.setdefault(name, [])
                relations[name].append(r)

        for relation_type in current_app.config["SIBLINGS_RELATION_TYPES"]:
            sr = SiblingsRelation(relation_type)
            name = relation_type.name

            for related_pid in sr.all(self.record.pid):
                r = self._build_sibling(related_pid, name)

                relations.setdefault(name, [])
                relations[name].append(r)

        return relations
Ejemplo n.º 3
0
    def get(self):
        """Get all sibling relations with the current record."""
        relations = {}
        for relation_type in SIBLINGS_RELATION_TYPES:
            sr = SiblingsRelation(relation_type)
            name = relation_type.name

            for sibling_pid in sr.all(self.record.pid):
                r = self._build_relation_obj(sibling_pid, name)
                relations.setdefault(name, [])
                relations[name].append(r)
        return relations
Ejemplo n.º 4
0
    def remove(self, first, second, relation_type):
        """Remove the relation between the first and the second."""
        self._validate_relation_type(relation_type)
        sr = SiblingsRelation(relation_type)
        sr.remove(pid=second.pid)

        # remove any metadata for this relation, if any
        # both first and second could have metadata for the relation
        RecordRelationsExtraMetadata.remove_extra_metadata_from(
            first, relation_type.name, second.pid.pid_value, second._pid_type)
        RecordRelationsExtraMetadata.remove_extra_metadata_from(
            second, relation_type.name, first.pid.pid_value, first._pid_type)
        return first, second