# Path: cms/dbutil.py
# def get_mongodb(request):
# def get_collection(request, name):
# def get_gridfs(request, collection='fs'):
# def encode_keys(d):
# def get_es_conn(request):
# def get_es_index_name(request):
# def serve_gridfs_file(file):
# def serve_gridfs_file_for_id(request, id, collection='fs'):
# 
# Path: cms/htmlutil.py
# def html_to_text(html, show_link_urls=1, skip_tags=[], unknown_entity_replacement=None):
#     output_unicode = False
#     if type(html) == unicode:
#         html = html.encode('utf-8')
#         output_unicode = True
# 
#     textout = cStringIO.StringIO()
#     formtext = formatter.AbstractFormatter(formatter.DumbWriter(textout))
#     parser = HTML_to_text_parser(formtext, show_link_urls, skip_tags, unknown_entity_replacement)
# 
#     # Annoyingly HTMLParser doesn't handle self-closing tags without a space
#     # (such as "<br/>") properly.
#     # So let's add any missing spaces (for example, "<br />").
#     html = SELF_CLOSING_FIX_RE.sub(r'\1 />', html)
# 
#     parser.feed(html)
#     parser.close()
#     text = textout.getvalue().strip()
#     del textout, formtext, parser
#     if output_unicode: return unicode(text, 'utf-8')
#     else: return text


from object import Object
from cms import dbutil
from bson.objectid import ObjectId
from cms.htmlutil import html_to_text
from pyramid import security
from interfaces import IContent, ITrash
import colander, deform
import widgets
import permissions
import repoze.workflow
import zope.interface
import pyes
import logging

    def index(self):
        dbutil.get_es_conn(self.request).index(self._get_es_document(), dbutil.get_es_index_name(self.request), self._get_es_doctype(), str(self._id))

    def unindex(self):
        try:
            dbutil.get_es_conn(self.request).delete(dbutil.get_es_index_name(self.request), self._get_es_doctype(), str(self._id))
        except pyes.exceptions.NotFoundException, e:
            pass

    def get_es_other_text(self):
        return '\n'.join(self._get_text_values_for_schema_node(self.get_schema(), self.get_schema_values()))

    def _get_text_values_for_schema_node(self, node, value):
        result = []
        if not value: return result
        if type(node.typ) == colander.Mapping:
            for cnode in node.children:
                name = cnode.name
                val = value.get(name, None)
                if val:
                    result += self._get_text_values_for_schema_node(cnode, val)
        elif type(node.typ) == colander.Sequence:
            if node.children:
                cnode = node.children[0]
                for val in value:
                    result += self._get_text_values_for_schema_node(cnode, val)
        elif type(node.typ) == colander.String:
            if getattr(node, 'include_in_other_text', True):
                if type(node.widget) == deform.widget.RichTextWidget:

# Path: cms/dbutil.py
# def get_mongodb(request):
# def get_collection(request, name):
# def get_gridfs(request, collection='fs'):
# def encode_keys(d):
# def get_es_conn(request):
# def get_es_index_name(request):
# def serve_gridfs_file(file):
# def serve_gridfs_file_for_id(request, id, collection='fs'):
# 
# Path: cms/htmlutil.py
# def html_to_text(html, show_link_urls=1, skip_tags=[], unknown_entity_replacement=None):
#     output_unicode = False
#     if type(html) == unicode:
#         html = html.encode('utf-8')
#         output_unicode = True
# 
#     textout = cStringIO.StringIO()
#     formtext = formatter.AbstractFormatter(formatter.DumbWriter(textout))
#     parser = HTML_to_text_parser(formtext, show_link_urls, skip_tags, unknown_entity_replacement)
# 
#     # Annoyingly HTMLParser doesn't handle self-closing tags without a space
#     # (such as "<br/>") properly.
#     # So let's add any missing spaces (for example, "<br />").
#     html = SELF_CLOSING_FIX_RE.sub(r'\1 />', html)
# 
#     parser.feed(html)
#     parser.close()
#     text = textout.getvalue().strip()
#     del textout, formtext, parser
#     if output_unicode: return unicode(text, 'utf-8')
#     else: return text


# Path: cms/resources/content.py
from object import Object
from cms import dbutil
from bson.objectid import ObjectId
from cms.htmlutil import html_to_text
from pyramid import security
from interfaces import IContent, ITrash
import colander, deform
import widgets
import permissions
import repoze.workflow
import zope.interface
import pyes
import logging


    def index(self):
        dbutil.get_es_conn(self.request).index(self._get_es_document(), dbutil.get_es_index_name(self.request), self._get_es_doctype(), str(self._id))

    def unindex(self):
        try:
            dbutil.get_es_conn(self.request).delete(dbutil.get_es_index_name(self.request), self._get_es_doctype(), str(self._id))
        except pyes.exceptions.NotFoundException, e:
            pass

    def get_es_other_text(self):
        return '\n'.join(self._get_text_values_for_schema_node(self.get_schema(), self.get_schema_values()))

    def _get_text_values_for_schema_node(self, node, value):
        result = []
        if not value: return result
        if type(node.typ) == colander.Mapping:
            for cnode in node.children:
                name = cnode.name
                val = value.get(name, None)
                if val:
                    result += self._get_text_values_for_schema_node(cnode, val)
        elif type(node.typ) == colander.Sequence:
            if node.children:
                cnode = node.children[0]
                for val in value:
                    result += self._get_text_values_for_schema_node(cnode, val)
        elif type(node.typ) == colander.String:
            if getattr(node, 'include_in_other_text', True):
                if type(node.widget) == deform.widget.RichTextWidget:

                    value = html_to_text(value, 0)