Beispiel #1
0
    def SearchableText(self):
        values = []
        index_attachments = self.getParentDatabase().getIndexAttachments()
        form = self.getForm()

        for itemname in self.items.keys():
            item_value = self.getItem(itemname)
            if type(item_value) is list:
                for v in item_value:
                    if type(v) is list:
                        values = values + [asUnicode(k) for k in v]
                    else:
                        values.append(asUnicode(v))
            else:
                values.append(asUnicode(item_value))
            # if selection or attachment field, we try to index rendered values too
            try:
                if form:
                    field = form.getFormField(itemname)
                    if field and field.getFieldType() in [
                            "SELECTION", "ATTACHMENT"
                    ]:
                        v = asUnicode(
                            self.getRenderedItem(
                                itemname,
                                form=form,
                                convertattachments=index_attachments))
                        if v:
                            values.append(v)
            except:
                pass
        return ' '.join(values)
Beispiel #2
0
    def tojson(self):
        """Returns a JSON representation of view data 
        """
        data = []
        categorized = self.getCategorized()
        
        columnids = [col.id for col in self.getColumns() if not getattr(col, 'HiddenColumn', False)]
        for b in self.getAllDocuments(getObject=False):
            row = [b.getPath().split('/')[-1]]
            for colid in columnids:
                v = getattr(b, self.getIndexKey(colid), '')
                if isinstance(v, list):
                    v = [asUnicode(e).encode('utf-8').replace('\r', '') for e in v]
                else:
                    v = asUnicode(v).encode('utf-8').replace('\r', '')
                row.append(v or ' ')

            if categorized:
                for cat in asList(row[1]):
                    entry = [c for c in row]
                    entry[1] = cat
                    data.append(entry)
            else:
                data.append(row)
        return json.dumps({ 'aaData': data })
Beispiel #3
0
 def __init__(self, context, exception_obj, formula, script_id):
     """
     """
     self.context = context
     self.context_url = context.absolute_url_path()
     self.formula = formula
     self.script_id = script_id
     self.message = asUnicode(self.traceErr())
Beispiel #4
0
    def SearchableText(self):
        """ Return value for the Plone catalog's `SearchableText` index.
        """
        values = []
        index_attachments = self.getParentDatabase().getIndexAttachments()
        form = self.getForm()

        for itemname in self.items.keys():
            item_value = self.getItem(itemname)
            if isinstance(item_value, list):
                for v in item_value:
                    if isinstance(v, list):
                        values = values + [asUnicode(k) for k in v]
                    else:
                        values.append(asUnicode(v))
            else:
                values.append(asUnicode(item_value))
            # if selection or attachment field, we try to index rendered
            # values too
            try:
                if form:
                    field = form.getFormField(itemname)
                    if field and field.getFieldType() in [
                            "SELECTION", "ATTACHMENT"
                    ]:
                        v = asUnicode(
                            self.getRenderedItem(
                                itemname,
                                form=form,
                                convertattachments=index_attachments))
                        if v:
                            values.append(v)
            except:
                _logger.error('Exception during indexing', exc_info=True)
                pass
        return ' '.join(values)
Beispiel #5
0
    def reportError(self, label, path=None, request=None):
        """
        """
        report = asUnicode(label)
        if not request:
            request = getattr(self.context, 'REQUEST', None)
        if request:
            if not path:
                if self.formula and hasattr(self.formula, 'absolute_url_path'):
                    path = self.formula.absolute_url_path()
            if path:
                report += " - Plomino formula %s" % path

            traceback = self.message.replace("<", "&lt;").replace(">", "&gt;")
            report += " - Plomino traceback " + traceback.replace(
                '\n', '\n<br/>')
            plone_tools = getToolByName(
                self.context.getParentDatabase().aq_inner, 'plone_utils')
            plone_tools.addPortalMessage(report, 'error', request)
    def importDesignFromXML(self,
                            xmlstring=None,
                            REQUEST=None,
                            from_folder=None,
                            replace=False):
        """
        """
        logger.info("Start design import")
        self.setStatus("Importing design")
        self.getIndex().no_refresh = True
        txn = transaction.get()
        xml_strings = []
        count = 0
        total = 0
        if from_folder:
            if not os.path.isdir(from_folder):
                raise PlominoDesignException, '%s does not exist' % path
            xml_files = (
                glob.glob(os.path.join(from_folder, '*.xml')) +
                glob.glob(os.path.join(from_folder, 'resources/*.xml')))
            total_elements = len(xml_files)
            for p in xml_files:
                fileobj = codecs.open(p, 'r', 'utf-8')
                xml_strings.append(fileobj.read())
        else:
            if REQUEST:
                f = REQUEST.get("file")
                xml_strings.append(asUnicode(f.read()))
            else:
                xml_strings.append(asUnicode(xmlstring))
            total_elements = None

        if replace:
            logger.info("Replace mode: removing current design")
            designelements = [o.id for o in self.getForms()] \
                                 + [o.id for o in self.getViews()] \
                                 + [o.id for o in self.getAgents()]
            ObjectManager.manage_delObjects(self, designelements)
            ObjectManager.manage_delObjects(self.resources,
                                            self.resources.objectIds())
            logger.info("Current design removed")

        for xmlstring in xml_strings:
            xmlstring = xmlstring.replace(">\n<", "><")
            xmldoc = parseString(xmlstring.encode('utf-8'))
            design = xmldoc.getElementsByTagName("design")[0]
            elements = [
                e for e in design.childNodes
                if e.nodeName in ('resource', 'element', 'dbsettings')
            ]

            if not total_elements:
                total_elements = len(elements)

            e = design.firstChild
            while e is not None:
                name = str(e.nodeName)
                if name in ('resource', 'element', 'dbsettings'):
                    if name == 'dbsettings':
                        logger.info("Import db settings")
                        self.importDbSettingsFromXML(e)
                    if name == 'element':
                        logger.info("Import " + e.getAttribute('id'))
                        self.importElementFromXML(self, e)
                    if name == 'resource':
                        logger.info("Import resource " + e.getAttribute('id'))
                        self.importResourceFromXML(self.resources, e)
                    count = count + 1
                    total = total + 1
                if count == 10:
                    self.setStatus("Importing design (%d%%)" %
                                   int(100 * total / total_elements))
                    logger.info("(%d elements committed, still running...)" %
                                total)
                    txn.savepoint(optimistic=True)
                    count = 0
                e = e.nextSibling

        logger.info("(%d elements imported)" % total)
        self.setStatus("Ready")
        txn.commit()
        self.getIndex().no_refresh = False
Beispiel #7
0
    def tojson(self, REQUEST=None):
        """ Returns a JSON representation of view data
        """
        data = []
        categorized = self.getCategorized()
        start = 1
        limit = -1
        search = None
        sort_index = None
        reverse = None
        if REQUEST:
            REQUEST.RESPONSE.setHeader(
                    'content-type', 'application/json; charset=utf-8')
            start = int(REQUEST.get('iDisplayStart', 1))
            iDisplayLength = REQUEST.get('iDisplayLength', None)
            if iDisplayLength:
                limit = int(iDisplayLength)
            search = REQUEST.get('sSearch', '').lower()
            if search:
                search = ' '.join([term+'*' for term in search.split(' ')])
            sort_column = REQUEST.get('iSortCol_0')
            if sort_column:
                sort_index = self.getIndexKey(
                        self.getColumns()[int(sort_column)-1].id)
            reverse = REQUEST.get('sSortDir_0', None)
            if reverse == 'desc':
                reverse = 0
            if reverse == 'asc':
                reverse = 1
        if limit < 1:
            limit = None

        if not REQUEST is None and 'request_query' in REQUEST:
            # query parameter in REQUEST is supposed to be a json object
            request_query = self.__query_loads__(REQUEST['request_query'])
        else:
            request_query = None

        results = self.getAllDocuments(
                start=start,
                limit=limit,
                getObject=False,
                fulltext_query=search,
                sortindex=sort_index,
                reverse=reverse,
                request_query=request_query)
        total = display_total = len(results)
        columns = [column for column in self.getColumns()
                if not getattr(column, 'HiddenColumn', False)]
        for brain in results:
            row = [brain.getPath().split('/')[-1]]
            for column in columns:
                column_value = getattr(brain, self.getIndexKey(column.id), '')
                rendered = column.getColumnRender(column_value)
                if isinstance(rendered, list):
                    rendered = [asUnicode(e).encode('utf-8').replace('\r', '') for e in rendered]
                else:
                    rendered = asUnicode(rendered).encode('utf-8').replace('\r', '')
                row.append(rendered or '&nbsp;')
            if categorized:
                for cat in asList(row[1]):
                    entry = [c for c in row]
                    entry[1] = cat
                    data.append(entry)
            else:
                data.append(row)
        return json.dumps(
                    {'iTotalRecords': total,
                    'iTotalDisplayRecords': display_total,
                    'aaData': data })
Beispiel #8
0
 def tojson(self, REQUEST=None):
     """ Returns a JSON representation of view data 
     """
     data = []
     categorized = self.getCategorized()
     start = 1
     limit = -1
     search = None
     sort_index = None
     reverse = None
     if REQUEST:
         start = int(REQUEST.get('iDisplayStart', 1))
         iDisplayLength = REQUEST.get('iDisplayLength', None)
         if iDisplayLength:
             limit = int(iDisplayLength)
         search = REQUEST.get('sSearch', '').lower()
         if search:
             search = ' '.join([term + '*' for term in search.split(' ')])
         sort_column = REQUEST.get('iSortCol_0')
         if sort_column:
             sort_index = self.getIndexKey(
                 self.getColumns()[int(sort_column) - 1].id)
         reverse = REQUEST.get('sSortDir_0', None)
         if reverse == 'desc':
             reverse = 0
         if reverse == 'asc':
             reverse = 1
     if limit < 1:
         limit = None
     results = self.getAllDocuments(start=start,
                                    limit=limit,
                                    getObject=False,
                                    fulltext_query=search,
                                    sortindex=sort_index,
                                    reverse=reverse)
     total = display_total = len(results)
     columnids = [
         col.id for col in self.getColumns()
         if not getattr(col, 'HiddenColumn', False)
     ]
     for b in results:
         row = [b.getPath().split('/')[-1]]
         for colid in columnids:
             v = getattr(b, self.getIndexKey(colid), '')
             if isinstance(v, list):
                 v = [
                     asUnicode(e).encode('utf-8').replace('\r', '')
                     for e in v
                 ]
             else:
                 v = asUnicode(v).encode('utf-8').replace('\r', '')
             row.append(v or '&nbsp;')
         if categorized:
             for cat in asList(row[1]):
                 entry = [c for c in row]
                 entry[1] = cat
                 data.append(entry)
         else:
             data.append(row)
     return json.dumps({
         'iTotalRecords': total,
         'iTotalDisplayRecords': display_total,
         'aaData': data
     })
Beispiel #9
0
    def tojson(self, REQUEST=None):
        """ Returns a JSON representation of view data
        """
        data = []
        categorized = self.getCategorized()
        start = 1
        limit = -1
        search = None
        sort_index = None
        reverse = None
        if REQUEST:
            REQUEST.RESPONSE.setHeader('content-type',
                                       'application/json; charset=utf-8')
            start = int(REQUEST.get('iDisplayStart', 1))
            iDisplayLength = REQUEST.get('iDisplayLength', None)
            if iDisplayLength:
                limit = int(iDisplayLength)
            search = REQUEST.get('sSearch', '').lower()
            if search:
                search = ' '.join([term + '*' for term in search.split(' ')])
            sort_column = REQUEST.get('iSortCol_0')
            if sort_column:
                sort_index = self.getIndexKey(
                    self.getColumns()[int(sort_column) - 1].id)
            reverse = REQUEST.get('sSortDir_0', None)
            if reverse == 'desc':
                reverse = 0
            if reverse == 'asc':
                reverse = 1
        if limit < 1:
            limit = None

        if not REQUEST is None and 'request_query' in REQUEST:
            # query parameter in REQUEST is supposed to be a json object
            request_query = self.__query_loads__(REQUEST['request_query'])
        else:
            request_query = None

        results = self.getAllDocuments(start=start,
                                       limit=limit,
                                       getObject=False,
                                       fulltext_query=search,
                                       sortindex=sort_index,
                                       reverse=reverse,
                                       request_query=request_query)
        total = display_total = len(results)
        columns = [
            column for column in self.getColumns()
            if not getattr(column, 'HiddenColumn', False)
        ]
        for brain in results:
            row = [brain.getPath().split('/')[-1]]
            for column in columns:
                column_value = getattr(brain, self.getIndexKey(column.id), '')
                rendered = column.getColumnRender(column_value)
                if isinstance(rendered, list):
                    rendered = [
                        asUnicode(e).encode('utf-8').replace('\r', '')
                        for e in rendered
                    ]
                else:
                    rendered = asUnicode(rendered).encode('utf-8').replace(
                        '\r', '')
                row.append(rendered or '&nbsp;')
            if categorized:
                for cat in asList(row[1]):
                    entry = [c for c in row]
                    entry[1] = cat
                    data.append(entry)
            else:
                data.append(row)
        return json.dumps({
            'iTotalRecords': total,
            'iTotalDisplayRecords': display_total,
            'aaData': data
        })