Fix #942790 (When generating a CSV Catalog, all fields of type "rating" are doubled)

This commit is contained in:
Kovid Goyal 2012-02-29 09:04:23 +05:30
parent e4f12eff88
commit 19586de6be
2 changed files with 101 additions and 6 deletions

View File

@ -5,11 +5,13 @@ __license__ = 'GPL v3'
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import sys
from PyQt4.Qt import (QDialog, QIcon, QApplication, QSize, QKeySequence,
QAction, Qt, QTextBrowser, QDialogButtonBox, QVBoxLayout)
QAction, Qt, QTextBrowser, QDialogButtonBox, QVBoxLayout, QGridLayout,
QLabel, QPlainTextEdit, QTextDocument)
from calibre.constants import __version__
from calibre.constants import __version__, isfrozen
from calibre.gui2.dialogs.message_box_ui import Ui_Dialog
class MessageBox(QDialog, Ui_Dialog): # {{{
@ -248,9 +250,95 @@ class ErrorNotification(MessageBox): # {{{
_proceed_memory.remove(self)
# }}}
class JobError(QDialog): # {{{
WIDTH = 600
def __init__(self, gui):
QDialog.__init__(self, gui)
self.gui = gui
self.queue = []
self._layout = l = QGridLayout()
self.setLayout(l)
self.icon = QIcon(I('dialog_error.png'))
self.setWindowIcon(self.icon)
self.icon_label = QLabel()
self.icon_label.setPixmap(self.icon.pixmap(128, 128))
self.icon_label.setMaximumSize(QSize(128, 128))
self.msg_label = QLabel('<p>&nbsp;')
self.msg_label.setWordWrap(True)
self.msg_label.setTextFormat(Qt.RichText)
self.det_msg = QPlainTextEdit(self)
self.det_msg.setVisible(False)
self.bb = QDialogButtonBox(QDialogButtonBox.Close, parent=self)
self.bb.accepted.connect(self.accept)
self.bb.rejected.connect(self.reject)
self.ctc_button = self.bb.addButton(_('&Copy to clipboard'),
self.bb.ActionRole)
self.ctc_button.clicked.connect(self.copy_to_clipboard)
self.show_det_msg = _('Show &details')
self.hide_det_msg = _('Hide &details')
self.det_msg_toggle = self.bb.addButton(self.show_det_msg, self.bb.ActionRole)
self.det_msg_toggle.clicked.connect(self.toggle_det_msg)
self.det_msg_toggle.setToolTip(
_('Show detailed information about this error'))
l.addWidget(self.icon_label, 0, 0, 1, 1)
l.addWidget(self.msg_label, 0, 1, 1, 1, Qt.AlignLeft|Qt.AlignTop)
l.addWidget(self.det_msg, 1, 0, 1, 2)
l.addWidget(self.bb, 2, 0, 1, 2, Qt.AlignRight|Qt.AlignBottom)
self.setModal(False)
self.base_height = max(200, self.sizeHint().height() + 20)
self.do_resize()
def copy_to_clipboard(self, *args):
d = QTextDocument()
d.setHtml(self.msg_label.text())
QApplication.clipboard().setText(
u'calibre, version %s (%s, isfrozen: %s)\n%s: %s\n\n%s' %
(__version__, sys.platform, isfrozen,
unicode(self.windowTitle()), unicode(d.toPlainText()),
unicode(self.det_msg.toPlainText())))
if hasattr(self, 'ctc_button'):
self.ctc_button.setText(_('Copied'))
def toggle_det_msg(self, *args):
vis = unicode(self.det_msg_toggle.text()) == self.hide_det_msg
self.det_msg_toggle.setText(self.show_det_msg if vis else
self.hide_det_msg)
self.det_msg.setVisible(not vis)
self.do_resize()
def do_resize(self):
h = self.base_height
if self.det_msg.isVisible():
h += 250
self.resize(QSize(self.WIDTH, h))
def showEvent(self, ev):
ret = QDialog.showEvent(self, ev)
self.bb.button(self.bb.Close).setFocus(Qt.OtherFocusReason)
return ret
# }}}
if __name__ == '__main__':
app = QApplication([])
from calibre.gui2 import question_dialog
print question_dialog(None, 'title', 'msg <a href="http://google.com">goog</a> ',
det_msg='det '*1000,
show_copy_button=True)
from calibre.gui2.preferences import init_gui
gui = init_gui()
d = JobError(gui)
d.show()
app.exec_()
gui.shutdown()
# if __name__ == '__main__':
# app = QApplication([])
# from calibre.gui2 import question_dialog
# print question_dialog(None, 'title', 'msg <a href="http://google.com">goog</a> ',
# det_msg='det '*1000,
# show_copy_button=True)

View File

@ -93,6 +93,8 @@ class CSV_XML(CatalogPlugin):
for entry in data:
entry['ondevice'] = db.catalog_plugin_on_device_temp_mapping[entry['id']]['ondevice']
fm = {x:db.field_metadata.get(x, {}) for x in fields}
if self.fmt == 'csv':
outfile = codecs.open(path_to_output, 'w', 'utf8')
@ -131,6 +133,8 @@ class CSV_XML(CatalogPlugin):
elif field == 'comments':
item = item.replace(u'\r\n',u' ')
item = item.replace(u'\n',u' ')
elif fm.get(field, {}).get('datatype', None) == 'rating' and item:
item = u'%.2g'%(item/2.0)
# Convert HTML to markdown text
if type(item) is unicode:
@ -168,6 +172,9 @@ class CSV_XML(CatalogPlugin):
if not val:
continue
if not isinstance(val, (str, unicode)):
if (fm.get(field, {}).get('datatype', None) ==
'rating' and val):
val = u'%.2g'%(val/2.0)
val = unicode(val)
item = getattr(E, field)(val)
record.append(item)