예제 #1
0
 def renderItem(self, table, itemid, nodeName='item'):
     itemNode = self._createElement(nodeName)
     for column in table.columns:
         node = None
         data = self.getItemForId(table.name, itemid, column.name)
         if column._type == 'blob':
             data = '<img src="data:image/png;base64,' + base64.b64encode(data) + '"/>'
             dom = parseString(data)
             node = self._createElement(column.name)
             node.appendChild(dom.documentElement)
         else:
             data = unicode(data)
             if column._type == 'text' and  data.startswith("<!DOCTYPE HTML"):
                 mo = self.htmlBodyRegexp.search(data)
                 if mo is not None:
                     data = mo.group(1)
                     data = '<div class="__from_qt__">'+data+"</div>"
                     # fix issue with two or more 'style' attributes by calling PurifyHtmlParser
                     # we do not change the database because QTextEdit seems to revert these changes
                     shandle = StringIO.StringIO()
                     parser = PurifyHTMLParser()
                     parser.set_output_file(shandle)
                     parser.feed(data)
                     parser.close()
                     data = shandle.getvalue()
                     shandle.close()
                     # fixing done
                     try:
                         dom = parseString(data.encode('UTF-8'))
                     except ExpatError:
                         print "Trouble with %s, ID %s, field %s" %  (table.name, itemid, column.name)
                         raise 
                     for imgElement in dom.documentElement.getElementsByTagName('img'):
                         imgId = imgElement.getAttribute('src')
                         mo = self.imgIdRegexp.search(imgId)
                         if mo is not None:
                             imgId = int(mo.group(1))
                             try:
                                 imgData = nafdb.getItemForId('images', imgId, 'image')
                                 if imgData is None:
                                     logging.error("broken image, id=%d", imgId)
                                     imgData = 'data:image/png;base64,' + BROKEN_IMAGE_DATA
                                     imgTitle = "broken image"
                                 else:
                                     imgData = 'data:image/png;base64,' + base64.b64encode(imgData)
                                     imgTitle = nafdb.getItemForId('images', imgId, 'title')
                                 imgElement.removeAttribute('src')
                                 imgElement.setAttribute('src', imgData)
                                 imgElement.setAttribute('alt', imgTitle)
                                 imgElement.setAttribute('title', imgTitle)
                             except:
                                 logging.error(traceback.format_exc())
                     node = self._createElement(column.name)
                     node.appendChild(dom.documentElement)
         if node is None:
             node = self._createTextElement(column.name, data)
         itemNode.appendChild(node)
     return itemNode
예제 #2
0
 def setEditItem(self, tableName, columnName, itemId):
     ##print "setEditItem", tableName, columnName, itemId
     self.tableName = tableName
     self.columnName = columnName
     self.itemId = itemId
     title = nafdb.getItemForId(tableName, itemId, "title")
     columnDisplayName = nafdb.getColumnDisplayName(tableName, columnName)
     self.setTitleFunc(itemId, title, columnDisplayName)
     self.textEdit.setHtml(nafdb.getItemForId(tableName, itemId, columnName) or "")
예제 #3
0
 def setEditItem(self, tableName, columnName, itemId):
     ##print "setEditItem", tableName, columnName, itemId
     self.tableName = tableName
     self.columnName = columnName
     self.itemId = itemId
     title = nafdb.getItemForId(tableName, itemId, 'title')
     columnDisplayName = nafdb.getColumnDisplayName(tableName, columnName)
     self.setTitleFunc(itemId, title, columnDisplayName)
     self.textEdit.setHtml(
         nafdb.getItemForId(tableName, itemId, columnName) or '')
예제 #4
0
 def _getItem(self, id, columnIndex):
     logging.info("id=%d", id)
     columnName = self.columns[columnIndex]
     try:
         item = nafdb.getItemForId(self.tableName, id, columnName, self._translate)
         return item
     except TypeError:
         logging.error("id=%d, columnIndex=%d, columnName=%s" % (id, columnIndex, columnName))
         return "?"
예제 #5
0
 def _getItem(self, id, columnIndex):
     logging.info("id=%d", id)
     columnName = self.columns[columnIndex]
     try:
         item = nafdb.getItemForId(self.tableName, id, columnName, self._translate)
         return item
     except TypeError:
         logging.error("id=%d, columnIndex=%d, columnName=%s" %(id, columnIndex, columnName))
         return '?'
예제 #6
0
 def data(self, index, role):
     row = index.row()
     id = self.idmap[row]
     if role == Qt.CheckStateRole and self.itemsCheckable == Qt.ItemIsUserCheckable and index.column() == 0:
         return QtCore.QVariant([Qt.Unchecked, Qt.Checked][id in self.relatedidmap])
     if role not in (Qt.DisplayRole, Qt.EditRole):
         return QtCore.QVariant()
     columnName = self.columnNames[index.column()]
     logging.debug("row=%s, id=%s, columnName=%s"%  (str(row), str(id), str(columnName)))
     return nafdb.getItemForId(self.tableName, id, columnName, self._translate)
예제 #7
0
 def data(self, index, role):
     row = index.row()
     id = self.idmap[row]
     if role == Qt.CheckStateRole and self.itemsCheckable == Qt.ItemIsUserCheckable and index.column(
     ) == 0:
         return QtCore.QVariant([Qt.Unchecked,
                                 Qt.Checked][id in self.relatedidmap])
     if role not in (Qt.DisplayRole, Qt.EditRole):
         return QtCore.QVariant()
     columnName = self.columnNames[index.column()]
     logging.debug("row=%s, id=%s, columnName=%s" %
                   (str(row), str(id), str(columnName)))
     return nafdb.getItemForId(self.tableName, id, columnName,
                               self._translate)
예제 #8
0
 def renderItem(self, table, itemid, nodeName='item'):
     itemNode = self._createElement(nodeName)
     for column in table.columns:
         node = None
         data = self.getItemForId(table.name, itemid, column.name)
         if column._type == 'blob':
             data = '<img src="data:image/png;base64,' + base64.b64encode(
                 data) + '"/>'
             dom = parseString(data)
             node = self._createElement(column.name)
             node.appendChild(dom.documentElement)
         else:
             data = unicode(data)
             if column._type == 'text' and data.startswith(
                     "<!DOCTYPE HTML"):
                 mo = self.htmlBodyRegexp.search(data)
                 if mo is not None:
                     data = mo.group(1)
                     data = '<div class="__from_qt__">' + data + "</div>"
                     # fix issue with two or more 'style' attributes by calling PurifyHtmlParser
                     # we do not change the database because QTextEdit seems to revert these changes
                     shandle = StringIO.StringIO()
                     parser = PurifyHTMLParser()
                     parser.set_output_file(shandle)
                     parser.feed(data)
                     parser.close()
                     data = shandle.getvalue()
                     shandle.close()
                     # fixing done
                     try:
                         dom = parseString(data.encode('UTF-8'))
                     except ExpatError:
                         print "Trouble with %s, ID %s, field %s" % (
                             table.name, itemid, column.name)
                         raise
                     for imgElement in dom.documentElement.getElementsByTagName(
                             'img'):
                         imgId = imgElement.getAttribute('src')
                         mo = self.imgIdRegexp.search(imgId)
                         if mo is not None:
                             imgId = int(mo.group(1))
                             try:
                                 imgData = nafdb.getItemForId(
                                     'images', imgId, 'image')
                                 if imgData is None:
                                     logging.error("broken image, id=%d",
                                                   imgId)
                                     imgData = 'data:image/png;base64,' + BROKEN_IMAGE_DATA
                                     imgTitle = "broken image"
                                 else:
                                     imgData = 'data:image/png;base64,' + base64.b64encode(
                                         imgData)
                                     imgTitle = nafdb.getItemForId(
                                         'images', imgId, 'title')
                                 imgElement.removeAttribute('src')
                                 imgElement.setAttribute('src', imgData)
                                 imgElement.setAttribute('alt', imgTitle)
                                 imgElement.setAttribute('title', imgTitle)
                             except:
                                 logging.error(traceback.format_exc())
                     node = self._createElement(column.name)
                     node.appendChild(dom.documentElement)
         if node is None:
             node = self._createTextElement(column.name, data)
         itemNode.appendChild(node)
     return itemNode