Skip to content

Commit

Permalink
✨ A patch has been created in response to an issue at collective.elas…
Browse files Browse the repository at this point in the history
  • Loading branch information
nazrulworld committed Jun 13, 2022
1 parent 1e38994 commit 39afe45
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 6 deletions.
6 changes: 4 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ Changelog
=========


0.7.7 (unreleased)
0.8.0 (unreleased)
------------------

- Nothing changed yet.
New features

- A patch has been added in response to `issue#91 <https://github.com/collective/collective.elasticsearch/issues/91>`_ on ``collective.elasticsearch``.


0.7.6 (2021-05-04)
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.. image:: https://img.shields.io/pypi/status/plone.app.fhirfield.svg
:target: https://pypi.python.org/pypi/plone.app.fhirfield/
.. image:: https://img.shields.io/pypi/status/collective.fhirpath.svg
:target: https://pypi.org/project/collective.fhirpath/
:alt: Egg Status

.. image:: https://img.shields.io/travis/nazrulworld/collective.fhirpath/master.svg
:target: http://travis-ci.org/nazrulworld/collective.fhirpath
:target: https://app.travis-ci.com/github/nazrulworld/collective.fhirpath
:alt: Travis Build Status

.. image:: https://coveralls.io/repos/github/nazrulworld/collective.fhirpath/badge.svg?branch=master
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

setup(
name="collective.fhirpath",
version="0.7.7.dev0",
version="0.8.0.dev0",
description="Plone powered provider for fhirpath",
long_description=long_description,
# Get more from https://pypi.org/classifiers/
Expand Down
135 changes: 135 additions & 0 deletions src/collective/fhirpath/patch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# -*- coding: utf-8 -*-
from .utils import uuidToObject
from collective.elasticsearch.hook import get_index_data
from collective.elasticsearch.hook import get_wrapped_object
from collective.elasticsearch.hook import logger as es_logger
from collective.elasticsearch.indexes import getIndex
from collective.elasticsearch.mapping import MappingAdapter
from plone import api
from plone.registry.interfaces import IRegistry
from plone.uuid.interfaces import IUUID
from zope.component import getUtility
from zope.component.hooks import getSite
from zope.component.hooks import setSite

import warnings

Expand Down Expand Up @@ -63,6 +72,118 @@ def MappingAdapter_get_index_creation_body(self):
return dict(settings=settings)


def index_batch(remove, index, positions, es=None): # noqa: C901
"""Issue: https://github.com/collective/collective.elasticsearch/issues/91
Cannot access object (lack of permission) index_batch()->uuidToObject(uid).
"""
if es is None:
from collective.elasticsearch.es import ElasticSearchCatalog

es = ElasticSearchCatalog(api.portal.get_tool("portal_catalog"))

setSite(api.portal.get())
conn = es.connection
bulk_size = es.get_setting("bulk_size", 50)

if len(remove) > 0:
bulk_data = []
for uid in remove:
bulk_data.append(
{"delete": {"_index": es.index_name, "_type": es.doc_type, "_id": uid}}
)
result = es.connection.bulk(
index=es.index_name, doc_type=es.doc_type, body=bulk_data
)

if "errors" in result and result["errors"] is True:
es_logger.error("Error in bulk indexing removal: %s" % result)

if len(index) > 0:
if type(index) in (list, tuple, set):
# does not contain objects, must be async, convert to dict
index = dict([(k, None) for k in index])
bulk_data = []

for uid, obj in index.items():
# If content has been moved (ie by a contentrule) then the object
# passed here is the original object, not the moved one.
# So if there is a uuid, we use this to get the correct object.
# See https://github.com/collective/collective.elasticsearch/issues/65 # noqa
if uid is not None:
obj = uuidToObject(uid, unrestricted=True)

if obj is None:
obj = uuidToObject(uid, unrestricted=True)
if obj is None:
continue
bulk_data.extend(
[
{
"index": {
"_index": es.index_name,
"_type": es.doc_type,
"_id": uid,
}
},
get_index_data(obj, es),
]
)
if len(bulk_data) % bulk_size == 0:
result = conn.bulk(
index=es.index_name, doc_type=es.doc_type, body=bulk_data
)

if "errors" in result and result["errors"] is True:
es_logger.error("Error in bulk indexing: %s" % result)

bulk_data = []

if len(bulk_data) > 0:
result = conn.bulk(
index=es.index_name, doc_type=es.doc_type, body=bulk_data
)

if "errors" in result and result["errors"] is True:
es_logger.error("Error in bulk indexing: %s" % result)

if len(positions) > 0:
bulk_data = []
index = getIndex(es.catalogtool._catalog, "getObjPositionInParent")
for uid, ids in positions.items():
if uid == "/":
parent = getSite()
else:
parent = uuidToObject(uid, unrestricted=True)
if parent is None:
es_logger.warn("could not find object to index positions")
continue
for _id in ids:
ob = parent[_id]
wrapped_object = get_wrapped_object(ob, es)
try:
value = index.get_value(wrapped_object)
except Exception:
continue
bulk_data.extend(
[
{
"update": {
"_index": es.index_name,
"_type": es.doc_type,
"_id": IUUID(ob),
}
},
{"doc": {"getObjPositionInParent": value}},
]
)
if len(bulk_data) % bulk_size == 0:
conn.bulk(index=es.index_name, doc_type=es.doc_type, body=bulk_data)
bulk_data = []

if len(bulk_data) > 0:
conn.bulk(index=es.index_name, doc_type=es.doc_type, body=bulk_data)


# *** Monkey Patch ***
def do():
""" """
Expand All @@ -75,3 +196,17 @@ def do():
"get_index_creation_body",
MappingAdapter_get_index_creation_body,
)

from collective.elasticsearch import hook

if getattr(index_batch, "__patched__", None) is None:
module_ = hook.index_batch.__module__
qname_ = hook.index_batch.__qualname__
setattr(index_batch, "__patched__", True)
setattr(index_batch, "__module__", module_)
setattr(index_batch, "__qualname__", qname_)
setattr(
hook,
"index_batch",
index_batch,
)
14 changes: 14 additions & 0 deletions src/collective/fhirpath/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from plone import api
from plone.app.fhirfield.interfaces import IFhirResource
from plone.app.fhirfield.interfaces import IFhirResourceValue
from plone.app.uuid.utils import uuidToCatalogBrain
from plone.app.uuid.utils import uuidToObject as org_uuidToObject
from plone.behavior.interfaces import IBehavior
from plone.restapi.exceptions import DeserializationError
from plone.restapi.services import _no_content_marker
Expand Down Expand Up @@ -153,3 +155,15 @@ def render(self):
return content.json(**dumps_params)

return json_dumps(content, **dumps_params)


def uuidToObject(uid, unrestricted=False):
""" """
if unrestricted is False:
return org_uuidToObject(uid)

brain = uuidToCatalogBrain(uid)
if brain is None:
return None

return brain._unrestrictedGetObject()

0 comments on commit 39afe45

Please sign in to comment.