Exemplo n.º 1
0
    def search(self,
               identity,
               params=None,
               es_preference=None,
               type=None,
               **kwargs):
        """Search for vocabulary entries."""
        self.require_permission(identity, 'search')

        # If not found, NoResultFound is raised (caught by the resource).
        vocabulary_type = VocabularyType.query.filter_by(id=type).one()

        # Prepare and execute the search
        params = params or {}
        search_result = self._search('search',
                                     identity,
                                     params,
                                     es_preference,
                                     extra_filter=Q(
                                         'term', type__id=vocabulary_type.id),
                                     **kwargs).execute()

        return self.result_list(
            self,
            identity,
            search_result,
            params,
            links_tpl=LinksTemplate(self.config.links_search,
                                    context={
                                        "args": params,
                                        "type": vocabulary_type.id,
                                    }),
            links_item_tpl=self.links_item_tpl,
        )
Exemplo n.º 2
0
    def search_drafts(self,
                      identity,
                      params=None,
                      es_preference=None,
                      **kwargs):
        """Search for drafts records matching the querystring."""
        self.require_permission(identity, 'search_drafts')

        # Prepare and execute the search
        params = params or {}

        search_result = self._search(
            'search_drafts',
            identity,
            params,
            es_preference,
            record_cls=self.draft_cls,
            search_opts=self.config.search_drafts,
            # `has_draft` systemfield is not defined here. This is not ideal
            # but it helps avoid overriding the method. See how is used in
            # https://github.com/inveniosoftware/invenio-rdm-records
            extra_filter=Q('term', has_draft=False),
            permission_action='read_draft',
            **kwargs).execute()

        return self.result_list(
            self,
            identity,
            search_result,
            params,
            links_tpl=LinksTemplate(self.config.links_search_drafts,
                                    context={"args": params}),
            links_item_tpl=self.links_item_tpl,
        )
Exemplo n.º 3
0
    def to_dict(self):
        # Create pagination
        total = len(self._item_list)

        pagination = Pagination(
            self._params["size"],
            self._params["page"],
            total,
        )

        # Dump each item and slice list to pagination window
        hits = []
        for item in self._item_list[pagination.from_idx:pagination.to_idx]:
            item = self._item_result_cls(
                item,
                self._identity,
                self._links_item_tpl,
            )
            hits.append(item.to_dict())

        # Dump full list
        return {
            "hits": {
                "hits": hits,
                "total": total,
            },
            "links": LinksTemplate(
                self._links_tpl,
                context={"args": self._params}
            ).expand(pagination)
        }
Exemplo n.º 4
0
 def to_dict(self):
     return {
         "id": self._item.id,
         "title": self._item.title,
         "priority": self._item.priority,
         "is_owner": self._identity.id == self._item.user_id,
         # Links are injected with URI templates (see config below)
         "links": LinksTemplate(self._links_tpl).expand(self._item)
     }
Exemplo n.º 5
0
    def search_versions(self,
                        id_,
                        identity,
                        params=None,
                        es_preference=None,
                        **kwargs):
        """Search for record's versions."""
        record = self.record_cls.pid.resolve(id_, registered_only=False)
        self.require_permission(identity, "read", record=record)

        # Prepare and execute the search
        params = params or {}

        search_result = self._search(
            'search_versions',
            identity,
            params,
            es_preference,
            record_cls=self.record_cls,
            search_opts=self.config.search_versions,
            extra_filter=Q('term',
                           **{'parent.id': str(record.parent.pid.pid_value)}),
            permission_action='read',
            **kwargs).execute()

        return self.result_list(
            self,
            identity,
            search_result,
            params,
            links_tpl=LinksTemplate(self.config.links_search_versions,
                                    context={
                                        "id": id_,
                                        "args": params
                                    }),
            links_item_tpl=self.links_item_tpl,
        )
Exemplo n.º 6
0
    def search_community_records(self, identity, community_uuid, params=None,
                                 es_preference=None, **kwargs):
        """Search for records published in the given community."""
        self.require_permission(identity, "read")

        # Prepare and execute the search
        params = params or {}

        search_result = self._search(
            "search",
            identity,
            params,
            es_preference,
            record_cls=self.record_cls,
            search_opts=self.config.search,
            extra_filter=Q(
                "term", **{"parent.communities.ids": str(community_uuid)}
            ),
            permission_action="read",
            **kwargs,
        ).execute()

        return self.result_list(
            self,
            identity,
            search_result,
            params,
            links_tpl=LinksTemplate(
                self.config.links_search_community_records,
                context={
                    "args": params,
                    "id": community_uuid,
                }
            ),
            links_item_tpl=self.links_item_tpl,
        )