Exemple #1
0
 def get_pid_type(self, type_id):
     """Get the PID type for a vocabulary type."""
     # Get type based on name.
     vocab_type = VocabularyType.query.filter_by(id=type_id).one_or_none()
     if vocab_type is None:
         raise PIDDoesNotExistError(None, None)
     return vocab_type.pid_type
    def resolve(self, identity, id_, id_type):
        """Get the record with a given identifier.

        This method assumes that the are no duplicates in the system
        (i.e. only one name record can have a pair of identifier:scheme).
        """
        es_query = Q(
            'bool',
            minimum_should_match=1,
            must=[
                Q('term', identifiers__identifier=id_),
                Q('term', identifiers__scheme=id_type),
            ]
        )

        # max_records = 1, we assume there cannot be duplicates
        # the loading process needs to make sure of that
        results = self._read_many(identity, es_query, max_records=1)
        # cant use the results_item because it returns dicts intead of records
        total = results.hits.total if lt_es7 else results.hits.total["value"]
        if total == 0:
            # Not a PID but trated as such
            raise PIDDoesNotExistError(pid_type=id_type, pid_value=id_)

        # (0 < #hits <= max_records) = 1
        record = self.record_cls.loads(results[0].to_dict())
        self.require_permission(identity, "read", record=record)

        return self.result_item(
            self,
            identity,
            record,
            links_tpl=self.links_item_tpl,
        )
Exemple #3
0
    def resolve(self, pid_value):
        """Resolve identifier.

        :params pid_value: Either a tuple ``(type_id, pid_value)`` or just a
            ``pid_value`` if the type context has been initialized using
            ``with_type_ctx()``.
        """
        type_id = self.type_id
        if type_id is None:
            type_id, pid_value = pid_value

        # Get type based on name.
        vocab_type = VocabularyType.query.filter_by(id=type_id).one_or_none()
        if vocab_type is None:
            raise PIDDoesNotExistError(None, pid_value)

        # Create resolver
        resolver = self.field._resolver_cls(
            pid_type=vocab_type.pid_type,
            object_type=self.field._object_type,
            getter=self.record_cls.get_record
        )

        # Resolve
        pid, record = resolver.resolve(pid_value)

        # Store pid in cache on record.
        self.field._set_cache(record, pid)

        return record
Exemple #4
0
    def get_record_by_pid_value(cls,
                                pid_value,
                                pid_type=None,
                                original_record=False,
                                with_deleted=True):
        """Get record by provided PID value.

        Args:
            pid_value(str): pid value to query
            pid_type(str): pid_type to query
            original_record(bool): if record was redirected this flag determines whether this method should return
              redirected record (if False) or original record (if True)
            with_deleted(bool): when set to True returns also deleted records

        Returns: requested record
        """
        if not pid_type:
            pid_type = cls.pid_type
        record_uuid = cls.get_uuid_from_pid_value(pid_value, pid_type,
                                                  original_record)
        with_deleted = original_record or with_deleted
        try:
            return cls.get_record(record_uuid, with_deleted=with_deleted)
        except NoResultFound:
            raise PIDDoesNotExistError(pid_type, pid_value)
Exemple #5
0
    def get_by_record(self,
                      record_id,
                      pid_type=None,
                      pid_value=None,
                      object_type="rec"):
        """Get a persistent identifier for this provider.

        :param record_id: the record UUID.
        :param pid_type: Persistent identifier type.
        :param pid_value: Persistent identifier value.
        :param object_type: Persistent identifier object_type.
        :returns: A :class:`invenio_pidstore.models.base.PersistentIdentifier`
            instance.
        """
        query = PersistentIdentifier.query.filter_by(pid_type=pid_type
                                                     or self.pid_type,
                                                     object_type=object_type,
                                                     object_uuid=record_id)
        if pid_value:
            query.filter_by(pid_value=pid_value)

        try:
            return query.one()
        except NoResultFound:
            raise PIDDoesNotExistError(pid_type, None)
Exemple #6
0
 def get_record(cls, object_uuid, with_deleted=False):
     """Get record instance from ElasticSearch."""
     try:
         return cls(get_es_record_by_uuid(object_uuid))
     except RecordGetterError as e:
         if isinstance(e.cause, NotFoundError):
             # Raise this error so the interface will render a 404 page
             # rather than a 500
             raise PIDDoesNotExistError('es_record', object_uuid)
         else:
             raise
Exemple #7
0
def getrecord_fetcher(record_id):
    """Fetch record data as dict with identity check for serialization."""
    recid = PersistentIdentifier.get_by_object(pid_type='recid',
                                               object_uuid=record_id,
                                               object_type='rec')

    try:
        result = current_rdm_records.records_service.read(
            g.identity, recid.pid_value)
    except PermissionDeniedError:
        # if it is a restricted record.
        raise PIDDoesNotExistError('recid', None)

    return result.to_dict()
Exemple #8
0
    def to_python(self, value):
        """Resolve PID value."""
        args = value.split('/')
        community, pid_value = args[0], args[-1]
        lazy_pid = super().to_python(pid_value)
        pid, record = lazy_pid.data

        if community != current_oarepo_communities.get_primary_community_field(record) \
            and community not in (current_oarepo_communities.get_communities_field(record) or []):
            raise PIDDoesNotExistRESTError(pid_error=PIDDoesNotExistError(
                pid_type=pid.pid_type, pid_value=pid.pid_value))

        pid.pid_value = CommunityPIDValue(
            pid.pid_value,
            current_oarepo_communities.get_primary_community_field(record))
        return lazy_pid
Exemple #9
0
def side_effect_get_record_by_pid_value(pid_value, pid_type):
    raise PIDDoesNotExistError(pid_value, pid_type)