Newer
Older
# -*- coding: utf-8 -*-
# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""cubicweb-elasticsearch entity's classes"""
from cubicweb import view
from cubicweb.predicates import is_instance
class IFullTextIndexSerializable(view.EntityAdapter):
"""Adapter to serialize an entity to a bare python structure that may be
directly serialized to e.g. JSON.
"""
__regid__ = 'IFullTextIndexSerializable'
__select__ = is_instance('Any')
def serialize(self, complete=False):
entity = self.entity
if complete:
entity.complete()
data = {
'cw_etype': entity.cw_etype,
'cw_source': entity.cw_metainformation()['source']['uri'],
'eid': entity.eid,
}
for rschema in entity.e_schema.indexable_attributes():
attr = rschema.type
try:
value = entity.cw_attr_cache[attr]
except KeyError:
# Bytes
continue
data[attr] = value
for rschema, tschema in entity.e_schema.attribute_definitions():
if tschema.type in ('Int', 'Float'):
attr = rschema.type
try:
value = entity.cw_attr_cache[attr]
except KeyError:
continue
data[attr] = value
return data
def registration_callback(vreg):
vreg.register(IFullTextIndexSerializable)