Skip to content

Commit

Permalink
Fix annotation data not getting indexed properly. Getting index data …
Browse files Browse the repository at this point in the history
…needs (#49)

to be async.
  • Loading branch information
vangheem committed Apr 11, 2017
1 parent 177d255 commit 51fa40e
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 32 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
1.0.0a8 (unreleased)
--------------------

- Nothing changed yet.
- Fix annotation data not getting indexed properly. Getting index data needs
to be async.
[vangheem]


1.0.0a7 (2017-04-10)
--------------------

- be able to configure __allow_access__ with service function by using
the `allow_access` configuration option
[vangheem]

- rename modified to modification_date and created to creation_date
[vangheem]
Expand Down
14 changes: 10 additions & 4 deletions guillotina/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ async def async_get(self, key, default=None):
element = annotations.get(key, default)
if element is None:
# Get from DB
obj = await self.obj._p_jar.get_annotation(self.obj, key)
if obj:
annotations[key] = obj
return annotations[key]
if self.obj._p_jar is not None:
try:
obj = await self.obj._p_jar.get_annotation(self.obj, key)
except KeyError:
obj = None
if obj:
annotations[key] = obj
if key in annotations:
return annotations[key]
return default

async def async_keys(self):
return await self.obj._p_jar.get_annotation_keys(self.obj._p_oid)
Expand Down
2 changes: 0 additions & 2 deletions guillotina/api/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ async def __call__(self):
str(e),
status=400)

await notify(ObjectModifiedEvent(self.context, data))

return Response(response={}, status=204)


Expand Down
8 changes: 4 additions & 4 deletions guillotina/behaviors/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def __init__(self, context):

async def load(self, create=False):
annotations_container = IAnnotations(self.__dict__['context'])
annotations = {}
try:
annotations = await annotations_container.async_get(self.__annotations_data_key)
except KeyError:
annotations = await annotations_container.async_get(self.__annotations_data_key)
if annotations is None:
if create:
annotations = AnnotationData()
await annotations_container.async_set(self.__annotations_data_key, annotations)
else:
annotations = {} # fallback, assumed only for reading here...
self.__dict__['data'] = annotations

def __getattr__(self, name):
Expand Down
11 changes: 7 additions & 4 deletions guillotina/catalog/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from guillotina.directives import merged_tagged_value_list
from guillotina.directives import metadata
from guillotina.exceptions import NoIndexField
from guillotina.interfaces import IAsyncBehavior
from guillotina.interfaces import ICatalogDataAdapter
from guillotina.interfaces import ICatalogUtility
from guillotina.interfaces import IResource
Expand All @@ -16,7 +17,6 @@
from guillotina.security.security_code import role_permission_manager
from guillotina.security.utils import get_principals_with_access_content
from guillotina.security.utils import get_roles_with_access_content
from guillotina.utils import apply_coroutine
from zope.interface import implementer


Expand Down Expand Up @@ -72,11 +72,11 @@ async def remove_catalog(self, container):
"""
pass

def get_data(self, content):
async def get_data(self, content):
data = {}
adapter = queryAdapter(content, ICatalogDataAdapter)
if adapter:
data.update(adapter())
data.update(await adapter())
return data


Expand Down Expand Up @@ -119,12 +119,15 @@ def get_data(self, ob, iface, field_name):
value = getattr(ob, field_name, None)
return json_compatible(value)

def __call__(self):
async def __call__(self):
# For each type
values = {}

for schema in iter_schemata_for_type(self.content.type_name):
behavior = schema(self.content)
if IAsyncBehavior.implementedBy(behavior.__class__):
# providedBy not working here?
await behavior.load(create=False)
for index_name, index_data in merged_tagged_value_dict(schema, index.key).items():
try:
if 'accessor' in index_data:
Expand Down
4 changes: 2 additions & 2 deletions guillotina/catalog/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def remove_object(obj, event):

@configure.subscriber(for_=(IResource, IObjectAddedEvent))
@configure.subscriber(for_=(IResource, IObjectModifiedEvent))
def add_object(obj, event):
async def add_object(obj, event):
uid = getattr(obj, 'uuid', None)
if uid is None:
return
Expand All @@ -100,7 +100,7 @@ def add_object(obj, event):
return
search = queryUtility(ICatalogUtility)
if search:
hook.index[uid] = search.get_data(obj)
hook.index[uid] = await search.get_data(obj)


@configure.subscriber(for_=(IContainer, IObjectAddedEvent))
Expand Down
2 changes: 1 addition & 1 deletion guillotina/db/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ def type(self):
async def get_json(self):
adapter = queryAdapter(self._obj, ICatalogDataAdapter)
if adapter is not None:
return adapter()
return await adapter()
20 changes: 14 additions & 6 deletions guillotina/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def __init__(self, object, old_parent, old_name, new_parent, new_name, data=None
self.data = data


@implementer(IObjectAddedEvent)
class ObjectAddedEvent(ObjectMovedEvent):
class BaseAddedEvent(ObjectMovedEvent):
"""An object has been added to a container"""

def __init__(self, object, new_parent=None, new_name=None, data=None):
Expand All @@ -45,13 +44,17 @@ def __init__(self, object, new_parent=None, new_name=None, data=None):
ObjectMovedEvent.__init__(self, object, None, None, new_parent, new_name, data=data)


@implementer(IObjectAddedEvent)
class ObjectAddedEvent(BaseAddedEvent):
"""An object has been added to a container"""


@implementer(IBeforeObjectAddedEvent)
class BeforeObjectAddedEvent(ObjectAddedEvent):
class BeforeObjectAddedEvent(BaseAddedEvent):
pass


@implementer(IObjectRemovedEvent)
class ObjectRemovedEvent(ObjectMovedEvent):
class BaseObjectRemovedEvent(ObjectMovedEvent):
"""An object has been removed from a container"""

def __init__(self, object, old_parent=None, old_name=None, data=None):
Expand All @@ -62,8 +65,13 @@ def __init__(self, object, old_parent=None, old_name=None, data=None):
ObjectMovedEvent.__init__(self, object, old_parent, old_name, None, None)


@implementer(IObjectRemovedEvent)
class ObjectRemovedEvent(BaseObjectRemovedEvent):
"""An object has been removed from a container"""


@implementer(IBeforeObjectRemovedEvent)
class BeforeObjectRemovedEvent(ObjectRemovedEvent):
class BeforeObjectRemovedEvent(BaseObjectRemovedEvent):
pass


Expand Down
7 changes: 2 additions & 5 deletions guillotina/json/deserialize_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def __init__(self, context, request):
self.permission_cache = {}

async def __call__(self, data, validate_all=False):

modified = False
errors = []

factory = get_cached_factory(self.context.type_name)
Expand All @@ -49,9 +47,8 @@ async def __call__(self, data, validate_all=False):
if errors:
raise DeserializationError(errors)

if modified:
self.context._p_register()
await notify(ObjectModifiedEvent(self.context, data))
self.context._p_register()
await notify(ObjectModifiedEvent(self.context, data))

return self.context

Expand Down
2 changes: 1 addition & 1 deletion guillotina/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def test_get_index_data(self, dummy_request):
ob = await create_content('Item', id='foobar')

data = ICatalogDataAdapter(ob)
fields = data()
fields = await data()
assert 'type_name' in fields
assert 'uuid' in fields
assert 'path' in fields
Expand Down
3 changes: 2 additions & 1 deletion guillotina/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from zope.interface import alsoProvides
from zope.interface import implementer

import docker
import os
import uuid

Expand Down Expand Up @@ -84,6 +83,7 @@ def _p_register(ob):


def run_docker_postgresql(label='testingaiopg'):
import docker
docker_client = docker.from_env(version='1.23')

# Clean up possible other docker containers
Expand Down Expand Up @@ -149,6 +149,7 @@ def run_docker_postgresql(label='testingaiopg'):


def cleanup_postgres_docker(label='testingaiopg'):
import docker
docker_client = docker.from_env(version='1.23')
# Clean up possible other docker containers
test_containers = docker_client.containers.list(
Expand Down

0 comments on commit 51fa40e

Please sign in to comment.